180 likes | 187 Views
This lecture covers various topics including the Reset Vector, Formal Introduction to Flow Charts, Multi-Precision Arithmetic (Addition, Subtraction, Multiplication, and Integer Division), and more.
E N D
ECE 3430 – Introduction to Microcomputer SystemsUniversity of Colorado at Colorado Springs Lecture #9 Agenda Today 1) The Reset Vector 2) Formal Intro to Flow Charts 3) Multi-Precision Arithmetic 4) BCD Addition ECE 3430 – Intro to Microcomputer Systems Fall 2015
Reset Vector 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 MSP432 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. ECE 3430 – Intro to Microcomputer Systems Fall 2015
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). ECE 3430 – Intro to Microcomputer Systems Fall 2015
Formal Into to Flow Charts 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 Subroutine - details of the subroutine may be outlined on a separate flowchart On Page Conn place a number/letter in here to differentiate (goto Off Page Conn markers) ECE 3430 – Intro to Microcomputer Systems Fall 2015
Multi-Precision Arithmetic 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 255DEC What if the number is larger? 1 1 0xABCD - RULE: A sum will require the same # of bytes as the operands + a carry bit. + 0x1234 - The code needs to track the carry bit. 0xBE01Work 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. ECE 3430 – Intro to Microcomputer Systems Fall 2015
Multi-Precision Arithmetic 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 (during 1-5, we “borrow” and make it 11-5)- 15 16- This also occurs in the MSP432 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: 0xAB12 - 0x34CD 0x7645Work 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. ECE 3430 – Intro to Microcomputer Systems Fall 2015
Multi-Precision Arithmetic 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. ECE 3430 – Intro to Microcomputer Systems Fall 2015
Multi-Precision Arithmetic Example (16-bit x 16-bit multiply via 8-bit multiplier, result is 32-bits) ECE 3430 – Intro to Microcomputer Systems Fall 2015
Multi-Precision Arithmetic 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. ECE 3430 – Intro to Microcomputer Systems Fall 2015
Multi-Precision Arithmetic 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. ECE 3430 – Intro to Microcomputer Systems Fall 2015
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 uC or uP lacks an instruction to perform a specific operation, the engineer (or toolset) has to use existing operations to pull off more complex operations. ECE 3430 – Intro to Microcomputer Systems Fall 2015
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). ECE 3430 – Intro to Microcomputer Systems Fall 2015
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. 0x99 correct answer in BCD is 0x106. Traditional addition only considers + 0x07 an 8-bit addition carry. In BCD we need to consider a 4-bit addition carry. 0xA0 - The solution is to add 0x6 to every nibble sum greater than 0x9 and to every nibble-sum that generated a 4-bit carry: 0x99 = When a nibble addition results in 0xA or greater, we add 0x6. + 0x07 0x9 + 0x7 generated a 4-bit carry, so we add 0x6 to it. 0xA0 + 0x66 0x106- 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! ECE 3430 – Intro to Microcomputer Systems Fall 2015
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 (0xA), 6 (0x6) 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… ECE 3430 – Intro to Microcomputer Systems Fall 2015
Multi-Precision Arithmetic (BCD) • BCD subtraction via 9’s complement approach: • 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). • 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. • 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. ECE 3430 – Intro to Microcomputer Systems Fall 2015
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. ECE 3430 – Intro to Microcomputer Systems Fall 2015
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). ECE 3430 – Intro to Microcomputer Systems Fall 2015
Other Useful Instructions(Bit Reversal, Endian Change, Reverse Subtract) Reverse bits (RBIT): • Mirrors the bits in a register in one cycle. Reverse bytes (REV/REV16): • 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: R5 = 10 - R4 • RSB R5, R4, #10 ECE 3430 – Intro to Microcomputer Systems Fall 2015