Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 3430 – Intro to Microcomputer Systems

Similar presentations


Presentation on theme: "ECE 3430 – Intro to Microcomputer Systems"— Presentation transcript:

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

2 ECE 3430 – Intro to Microcomputer Systems
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 MSP430 addresses are 16-bits wide, there are two reserved memory locations. These locations form what is referred to as the reset vector. - In the MSP430, it resides at locations 0xFFFE and 0xFFFF (16-bit). These memory locations CPU specific. The PC is initialized to the data stored in 0xFFFE and 0xFFFF. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

3 ECE 3430 – Intro to Microcomputer Systems
Reset Vector Reset Vector – How do we initialize it? ORG 0xFFFE RST_VECT: DW 0xC000 ; this initializes addresses 0xFFFE and ; 0xFFFF to 0x00 and 0xC0 respectively Why 0xC000? It is the beginning of the Flash in our MSP430. In this class, this is where we will originate all of our code. In theory though, we could use any valid 16-bit address. The same type of thing happens 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 2014

4 ECE 3430 – Intro to Microcomputer Systems
Reset Vector Reset Vector – How do we initialize it? ORG 0xFFFE RstVect: DW Main Could also be a label of the first line of assembly you want to execute. For the MSP430 chip in your LaunchPad kit (G2553): RAM = 512 B: 0x0200 – 0x03FF Flash (info memory) = 256 B: 0x1000-0x10FF Flash (code memory) = 16 kB: 0xC000 – 0xFFFF Note: 0xFFC0 – 0xFFFF are reserved for interrupt vectors – no code here! (See MSP430 memory map in presentation 5, slide 11.) Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

5 ECE 3430 – Intro to Microcomputer Systems
Reset Vector Reset Vector – - On reset, the upper byte of the PC/R0 will get xC0 and the lower byte will get 0x PC = 0xC000. This is where the CPU will READ the first instruction code. Ex) Another way to initialize the reset vector: RST_VECT: EQU 0xFFFE FLASH: EQU 0xC ORG RST_VECT RstVect: DW FLASH ORG FLASH <code…> Memory 0C000h code - - 0FFFEh 0x00 0FFFFh 0xC0 Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

6 ECE 3430 – Intro to Microcomputer Systems
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) Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

7 We’ll discuss flowcharts in more depth after we discuss
Intro to Flow Charts Ex) Design a program to 16-bit add M(0200h) to M(0202h) and store to M(0204h). ORG 0C000h mov 0200h,R4 ; read into R mov 0202h,R5 ; read into R5 add R4,R ; R5 = R4 + R mov R5,0204h ; write result END START Get M(0200h)  R4 Get M(0202h)  R5 R4 + R5  R5 Store Sum: R5  M(0204h) We’ll discuss flowcharts in more depth after we discuss jump instructions and conditional code execution. END Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

8 Multi-Precision Arithmetic
Multi-Precision Arithmetic (Addition) - Sometimes #’s need more than 1 byte to represent the data. So we need more than just 8-bit arithmetic and representation. Ex) 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. + 0x The code needs to track the carry bit. 0xBE01 Write the code to perform the above sum and store in 0x0200:0x0201 using 8-bit arithmetic: mov.b #0xCD,R4 ; For the LS byte, don’t worry about carry bit. add.b #0x34,R4 ; The instruction “add” changes the carry bit--but doesn’t consider it. mov.b R4,0x ; Store the lower byte in 0x mov.b #0xAB,R4 ; For the MS byte, we need to consider the carry bit from the previous instruction. addc.b #0x12,R4 ; “addc” will add the operand and the carry bit (in the status register). mov.b R4,0x ; Store the upper byte in 0x This way we can sequentially read the 2-byte result. Note that the code we write dictates the size of numbers that can be added by the CPU. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

9 Multi-Precision Arithmetic
Multi-Precision Arithmetic (Addition) - Of course, the MSP430 can natively add 16-bit numbers, so we could just do the following instead: Ex) 1 1 0xABCD + 0x1234 0xBE01 Write the code to perform the above sum and store in 0x0200:0x0201 using 16-bit arithmetic: mov.w #0xABCD,R4 ; Load the first 16-bit number. add.w #0x1234,R4 ; The instruction “add” changes the carry bit--but doesn’t consider it. mov.w R4,0x ; Store the word in 0x0200. Note that we could use this pattern to add numbers of limitless size! Bigger numbers just take longer and consume more memory! Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

10 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…) ex) (during 1-5, we “borrow” and make it 11-5) This also occurs in the MSP430 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 0xAB12 - 0x34CD 0x7645 Write the code to perform the above subtraction and store in 0x0200:0x201 using 8-bit arithmetic: mov.b #0x12,R4 ; For the LS byte, don’t worry about borrow bit. sub.b #0xCD,R4 ; The instruction “sub” changes the borrow bit (carry bit), but doesn’t consider it. mov.b R4,0x ; Store the lower byte in 0x mov.b #0xAB,R4 ; For the MS byte, we need to consider the borrow bit from the previous instruction. subc.b #0x34,R4 ; “subc” will subtract the operand and the borrow bit (in the static register). mov.b R4,0x ; Store the upper byte in 0x This way we can sequentially read the 2-byte result. Note that the code we write dictates the size of numbers that can be subtracted by the CPU. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

11 Multi-Precision Arithmetic
Multi-Precision Arithmetic (Subtraction) - Of course, the MSP430 can natively subtract 16-bit numbers, so we could just do the following instead: Ex) 0xAB12 - 0x34CD 0x Write the code to perform the above difference and store in 0x0200:0x0201 using 16-bit arithmetic: mov.w #0xAB12,R4 ; Load the first 16-bit number. sub.w #0x34CD,R4 ; The instruction “sub” changes the carry bit--but doesn’t consider it. mov.w R4,0x ; Store the word in 0x0200. Note that we could use this pattern to subtract numbers of limitless size! Bigger numbers just take longer and consume more memory! Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

12 Multi-Precision Arithmetic
Multi-Precision Arithmetic (Multiplication) - The MSP430 does not natively have a multiplication instruction (you have to perform it manually). Or use the hardware multiplier if available. To continue the expansion use the algorithm of Sum of Partial Products. With enough memory and time, the MSP430 can multiply numbers of any size. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

13 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 2014

14 Multi-Precision Arithmetic
Multi-Precision Arithmetic (Multiplication) Here, an “unsigned int” can be any size you like: 8, 16, 32, 64-bit…but they all must be the same precision! Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

15 Multi-Precision Arithmetic
Multi-Precision Arithmetic (Multiplication) Here, an “unsigned int” can be any size you like: 8, 16, 32, 64-bit…but they all must be the same precision! Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

16 Multi-Precision Arithmetic
Multi-Precision Arithmetic (Multiplication) Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

17 Multi-Precision Arithmetic
Multi-Precision Arithmetic (Multiplication) Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

18 Multi-Precision Arithmetic
Multi-Precision Arithmetic (Integer Division) As discussed previously, the MSP430 also does not provide native division instructions. You have to perform it manually using subtract and shift operations… Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

19 Multi-Precision Arithmetic
Multi-Precision Arithmetic (Integer Division) Here, an “int” is 32-bit—but can be any size you like: 8, 16, 32, 64-bit, … Here, “long int” needs to be twice the precision of “int”. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

20 Multi-Precision Arithmetic
Multi-Precision Arithmetic (Signed vs. Unsigned) Addition and subtraction have no notion of the meaning “signed” or “unsigned”. It is always performed the same! The result can be interpreted as signed or unsigned. All operands and the result must assume a common convention: all signed or all unsigned! Multiplication and division operations, on the other hand, do have to deal with “signed” and “unsigned” numbers differently. 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 2014

21 Multi-Precision Arithmetic
Although the MSP430 does not natively support floating point operations, it is possible to write code using the existing MSP430 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 and look at how much code the compiler writes to perform floating point operations. Any mathematical operation can be performed on the MSP430 with enough code ingenuity. When a uC or uP lacks an instruction to perform a specific operation, the engineer has to use existing operations to pull off more complex operations. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

22 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. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

23 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 x correct answer in BCD is 0x106. Traditional addition only considers x an 8-bit addition carry. In BCD we need to consider a 4-bit addition carry xA0 - The solution is to add 0x6 to every nybble sum greater than 0x9 and to every nybble-sum that generated a 4-bit carry: 0x = When a nybble addition results in 0xA or greater, we add 0x x x9 + 0x7 generated a 4-bit carry, so we add 0x6 to it xA x x The CPU needs to monitor the 4-bit carry. - The MSP430 provides instructions to accommodate BCD: DADD and DADC - The numbers must be BCD to begin with! Ex) clrc ; “dadd” adds in the C flag—so make sure it is zero! mov.b #0x99,R4 dadd.b #0x07,R4 ; “dadd” does two things: ; 1) adds 0x6 to each nybble that is 0xA or greater. mov.b R4,0x0200 ; 2) checks the internal “half carry flag” and adds 0x6 if necessary. NOTE: M(0200h) = 0x06 and the carry flag in the status register will be set! Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

24 Multi-Precision Arithmetic (BCD)
The DADD can be continued in a loop working towards the most-significant digits. DADD adds in the carry flag every time so we must explicitly clear the C flag before the first DADD and then just keep using DADD for more-significant bits. The DADC instruction takes only a destination operand and just adds the C flag to that. BCD subtraction is not supported natively by the MSP430. BCD subtraction involves similar operations to BCD addition: If a borrow occurs or the BCD nybble (decimal digit) is greater than or equal to 10 (0xA), 6 (0x6) is subtracted from the nybble. 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 2014

25 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. 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. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014

26 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 nybble). Addition and subtraction can easily be pulled off using both packed and unpacked BCD formats. Multiplication and division algorithms require an unpacked BCD format. Lecture #9 ECE 3430 – Intro to Microcomputer Systems Fall 2014


Download ppt "ECE 3430 – Intro to Microcomputer Systems"

Similar presentations


Ads by Google