ECE 3430 Introduction to Microcomputer Systems University of

  • Slides: 18
Download presentation
ECE 3430 – Introduction to Microcomputer Systems University of Colorado at Colorado Springs Lecture

ECE 3430 – Introduction to Microcomputer Systems University of Colorado at Colorado Springs Lecture #9 Agenda Today 1) 2) 3) 4) Lecture #9 The Reset Vector Formal Intro to Flow Charts Multi-Precision Arithmetic BCD Addition ECE 3430 – Intro to Microcomputer Systems Fall 2015 1

Reset Vector – When a CPU is reset, what happens? - A RESET is

Reset Vector – When a CPU is reset, what happens? - A RESET is actually an interrupt (interrupts covered later). - Most internal registers are cleared on reset. RAM memory maintains the values last stored to them. - The CPU needs to know where to begin executing code (i. e. , what value to initialize the program counter to) after the reset is released. - The code that is executed first should reside in non-volatile memory (after a reset or power up). - A set of memory locations are pre-defined to hold the initial address to be loaded into the program counter upon reset. Since MSP 432 addresses are 32 -bits wide, there are four reserved memory locations. These locations form what is referred to as the reset vector. - The lab tools set up the reset vector for you. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2015 2

Reset Vector In your PC/laptop, your microprocessor jumps to designated memory locations in EEPROM

Reset Vector In your PC/laptop, your microprocessor jumps to designated memory locations in EEPROM and determines where to adjust the program counter to. The program counter points to more code stored in EEPROM on the motherboard--this code is referred to as the basic input/output system (BIOS). The BIOS initializes your machine and turns control over to an operating system (if any). Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2015 3

Formal Into to Flow Charts – Graphical method for designing program flow. This is

Formal Into to Flow Charts – Graphical method for designing program flow. This is used for “Designing” programs and also for “Documentation”. Terminal - beginning and end of program (start/stop) Process - what the CPU is “doing” at this point in the execution In/Out - represents data IN or OUT of the program Decision - question in the code--the direction of the code depends on the answer to the question - details of the subroutine may be outlined on a separate flowchart Subroutine On Page Conn Off Page Conn Lecture #9 place a number/letter in here to differentiate (goto markers) ECE 3430 – Intro to Microcomputer Systems Fall 2015 4

Multi-Precision Arithmetic (Addition) - Sometimes #’s need more bytes to represent the data than

Multi-Precision Arithmetic (Addition) - Sometimes #’s need more bytes to represent the data than a machine can natively handle. Ex) Imagine an 8 -bit machine that can only natively add 8 -bit numbers. Addition – 8 -bit unsigned numbers can go up to 255 DEC What if the number is larger? 1 1 0 x. ABCD + 0 x 1234 0 x. BE 01 - RULE: A sum will require the same # of bytes as the operands + a carry bit. - The code needs to track the carry bit. Work from least-significant “chunks” to more significant “chunks”. * Load two least-significant bytes into two registers. * Perform an ordinary 8 -bit addition. Carry bit may be set or cleared. * Load next-most-significant bytes into two registers—without changing the C flag. * Perform an “add with carry” operation of those two registers. * Continue previous two bullets until desired precision is met. Note that the code we write dictates the size of numbers that can be added by the CPU. Only bound by time and space. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2015 5

Multi-Precision Arithmetic (Subtraction) - In subtraction, if we are subtracting a large number from

Multi-Precision Arithmetic (Subtraction) - In subtraction, if we are subtracting a large number from a smaller number, we need to “borrow” from the next higher digit (just like we did in elementary school…) 2 11 ex) - 31 15 16 (during 1 -5, we “borrow” and make it 11 -5) - This also occurs in the MSP 432 CPU. During subtraction, the inverse of the carry flag (C) is used to indicate if a borrow occurred: C=0 if a borrow occurred C=1 if a borrow DID NOT occur - For simplicity, assume an 8 -bit CPU and the need to subtract two 16 -bit numbers: - 0 x. AB 12 0 x 34 CD 0 x 7645 Work from least-significant “chunks” to more significant “chunks”. * Load two least-significant bytes into two registers. * Perform an ordinary 8 -bit subtraction. Carry bit may be set or cleared. The opposite indicates a borrow. * Load next-most-significant bytes into two registers—without changing the C flag. * Perform a “subtract with carry” operation of those two registers. * Continue previous two bullets until desired precision is met. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2015 6

Multi-Precision Arithmetic (Multiplication) To continue the expansion use the algorithm of Sum of Partial

Multi-Precision Arithmetic (Multiplication) To continue the expansion use the algorithm of Sum of Partial Products. With enough memory and time, any CPU can multiply numbers of any size. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2015 7

Multi-Precision Arithmetic Example (16 -bit x 16 -bit multiply via 8 -bit multiplier, result

Multi-Precision Arithmetic Example (16 -bit x 16 -bit multiply via 8 -bit multiplier, result is 32 -bits) Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2015 8

Multi-Precision Arithmetic (Integer Division) A similar progressive expansion can be performed. With enough memory

Multi-Precision Arithmetic (Integer Division) A similar progressive expansion can be performed. With enough memory and time, any CPU can divide numbers of any size. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2015 9

Multi-Precision Arithmetic (Signed vs. Unsigned) <Review when signed vs unsigned matters in addition, subtraction,

Multi-Precision Arithmetic (Signed vs. Unsigned) <Review when signed vs unsigned matters in addition, subtraction, multiplication, and division> The radix-2 multiplication and division process (discussed in earlier slides) is inherently unsigned. To deal with signed numbers you have to take two’s complements of the values beforehand—and potentially take the two’s complement of the final result. Booth’s algorithm can be used to avoid the two’s complement operations. It works directly with two’s complement encoded values. We won’t discuss it in this class. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2015 10

Multi-Precision Arithmetic Some CPUs don’t offer floating point operations. In that situation, it is

Multi-Precision Arithmetic Some CPUs don’t offer floating point operations. In that situation, it is possible to write code using the existing instructions to pull off floating point operations—but it is not easy—and typically requires a lot of code! -> Use data type “float” or “double” with the C compiler. If the CPU has no floating point support, the C compiler has to write algorithms. Any mathematical operation can be performed on any CPU with enough code ingenuity. When a u. C or u. P lacks an instruction to perform a specific operation, the engineer (or toolset) has to use existing operations to pull off more complex operations. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2015 11

Multi-Precision Arithmetic (BCD) BCD is sometimes useful for eliminating I/O circuitry for decoding binary

Multi-Precision Arithmetic (BCD) BCD is sometimes useful for eliminating I/O circuitry for decoding binary numbers to decimal and vice-versa. Many standard arithmetic operations can be performed almost as easily in BCD with somewhat little overhead (if the CPU has native BCD instructions). Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2015 12

Multi-Precision Arithmetic (BCD) Multi-Precision Arithmetic (BCD Addition) - If we are using BCD, traditional

Multi-Precision Arithmetic (BCD) Multi-Precision Arithmetic (BCD Addition) - If we are using BCD, traditional addition doesn’t work. In BCD, think of each hex digit as a decimal digit. 0 x 99 correct answer in BCD is 0 x 106. Traditional addition only considers + 0 x 07 an 8 -bit addition carry. In BCD we need to consider a 4 -bit addition carry. 0 x. A 0 - The solution is to add 0 x 6 to every nibble sum greater than 0 x 9 and to every nibble-sum that generated a 4 -bit carry: 0 x 99 + 0 x 07 0 x. A 0 + 0 x 66 0 x 106 = When a nibble addition results in 0 x. A or greater, we add 0 x 6. 0 x 9 + 0 x 7 generated a 4 -bit carry, so we add 0 x 6 to it. - To support BCD addition, the CPU needs to monitor the 4 -bit carry. - The ARM processor leaves pulling off BCD arithmetic operations up to the compilers or the assembly programmer. - Several other processors have decimal versions of “add” and “add with carry” instructions. - For BCD instructions, the numbers must be BCD to begin with! Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2015 13

Multi-Precision Arithmetic (BCD) • BCD subtraction involves similar operations to BCD addition: If a

Multi-Precision Arithmetic (BCD) • BCD subtraction involves similar operations to BCD addition: If a borrow occurs or the BCD nibble (decimal digit) is greater than or equal to 10 (0 x. A), 6 (0 x 6) is subtracted from the nibble. • BCD subtraction can be performed using BCD addition. It involves taking either the nine’s or ten’s complement of the subtrahend… Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2015 14

Multi-Precision Arithmetic (BCD) • BCD subtraction via 9’s complement approach: – – • BCD

Multi-Precision Arithmetic (BCD) • BCD subtraction via 9’s complement approach: – – • BCD subtraction via 10’s complement approach: – – • Find 10’s complement of the subtrahend. Add two numbers using BCD addition. If carry is generated, answer is positive. If carry is not generated, answer is negative (take 10’s complement of result to find magnitude). Standard binary addition can be used to quickly determine a 9 or 10’s complement of a number. – – • Find 9’s complement of the subtrahend. Add two numbers using BCD addition. If carry is generated, add carry to the result (answer is positive). If carry is not generated, answer is negative (take 9’s complement of result to find magnitude). 9’s complement: Invert each bit in a BCD digit. Add 10. Discard carry. 10’s complement: Find 9’s complement and add 1. BCD multiplication and BCD division algorithms exist—but are pretty complicated and won’t be discussed in this course. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2015 15

Multi-Precision Arithmetic (BCD) • There are both packed and unpacked BCD formats. In unpacked

Multi-Precision Arithmetic (BCD) • There are both packed and unpacked BCD formats. In unpacked BCD, each decimal digit is represented by 8 bits (1 byte). In packed BCD (as we have discussed in this presentation), each decimal digit is represented by 4 bits (1 nibble). • Addition and subtraction can easily be pulled off using both packed and unpacked BCD formats. • Multiplication and division algorithms generally require an unpacked BCD format. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2015 16

Other Useful Instructions (Shifts and Rotates) Fundamentally there are three operations in any CPU:

Other Useful Instructions (Shifts and Rotates) Fundamentally there are three operations in any CPU: • Logical shifts • Arithmetic shifts • Rotates ARM supports: • Logical shift right and left (LSR and LSL). • Arithmetic shift right (ASR). ASL is the same as LSL. • Rotate right (ROR). Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2015 17

Other Useful Instructions (Bit Reversal, Endian Change, Reverse Subtract) Reverse bits (RBIT): • Mirrors

Other Useful Instructions (Bit Reversal, Endian Change, Reverse Subtract) Reverse bits (RBIT): • Mirrors the bits in a register in one cycle. Reverse bytes (REV/REV 16): • Reverses the byte order in a register or in the two 16 -bit halves of a register. Reverse subtraction (RSB): • Reverses the order of the subtraction. • Consider: R 5 = 10 - R 4 • RSB R 5, R 4, #10 Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2015 18