Presentation on theme: "Floyd, Digital Fundamentals, 10 th ed EET 2261 Unit 7 Indexed Addressing Mode Read Mazidi, Chapter 6. Homework #7 and Lab #7 due next week. Quiz next week."— Presentation transcript:
Floyd, Digital Fundamentals, 10 th ed EET 2261 Unit 7 Indexed Addressing Mode Read Mazidi, Chapter 6. Homework #7 and Lab #7 due next week. Quiz next week.
Review: Addressing Modes The HCS12s six addressing modes are: Inherent Immediate Direct Extended Relative Indexed (which has several variations) Weve studied all of these except indexed addressing mode.
Indexed Addressing Mode Indexed addressing mode comes in at least five variations: Constant offset indexed addressing Auto pre/post decrement/increment indexed addressing Accumulator offset indexed addressing Constant indirect indexed addressing Accumulator D indirect indexed addressing Of these five variations, well use only the three in bold above. For the others, see the section starting on p. 34 of the HCS12 CPU Reference Manual.HCS12 CPU Reference Manual
The Original Indexed Addressing Mode In older microcontrollers (including the Freescale HC11) indexed addressing meant what were calling constant offset indexed addressing. The HCS12 added the other variations.
Variation #1: Constant Offset Indexed Addressing Mode In constant offset indexed addressing mode, the operands address is found by adding a constant offset to the contents of an index register (usually IX or IY, but possibly also SP or PC). Example: The instruction LDAA 3,X uses Index Register X, with 3 as the constant offset. If Index Register X contains $1500, then this instruction loads Accumulator A with the contents of memory location $1503.
Simple Example of Indexed Addressing ORG $2000 LDX #$1500 LDY #$1600 LDAA 3,X INCA STAA 8,Y BRA * END
Copying a Block of Data A typical use of indexed addressing mode is copying a block of data (many bytes) from one place in memory to another. Example: Suppose we have some data in memory locations $1200 through $128F, and we want to copy it to memory locations $1300 through $138F.
Copying a Block of Data: The Hard Way Without indexed addressing mode, wed have to do something like the following: ORG $2000 LDAA $1200 ;copy first byte STAA $1300 LDAA $1201 ;copy second byte STAA $1301 LDAA $128F ;copy last byte STAA $138F......
Copying a Block of Data: The Smart Way With indexed addressing, its much easier: ORG $2000 LDAB #$90 ;number of bytes LDX #$1200 ;pointer to source bytes LDY #$1300 ;pointer to destination bytes L1: LDAA 0,X STAA 0,Y INX INY DECB BNE L1 END
Variation #2: Auto Pre/post Decrement/ increment Indexed Addressing Mode In the previous program, we incremented IX and IY each time through the loop. Since this is such a common thing to do, the HCS12 gives us a quicker way to do it. In place of these two instructions: LDAA 0,X INX We can use this one instruction: LDAA 1,X+
Copying a Block of Data With Auto Post-Increment Indexed Addressing Once more, our earlier program made easier: ORG $2000 LDAB #$90 ;number of bytes LDX #$1200 ;pointer to source bytes LDY #$1300 ;pointer to destination bytes L1: LDAA 1,X+ STAA 1,Y+ DECB BNE L1 END
Variation #3: Accumulator Offset Indexed Addressing Mode In accumulator offset indexed addressing, the operands address is found by adding the contents of Accumulator A or B or D to the contents of an index register. Example: The instruction LDAA B,X uses Index Register X, with the contents of Accumulator B as the offset. If Index Register X holds $1500 and Accumulator B holds $07, then this instruction loads Accumulator A with the contents of memory location $1507.
Look-Up Tables Indexed addressing mode is also useful for implementing look-up tables. Look-up tables can save us time in a program by storing the results of frequently used computations in memory so that we can look up the results when we need them instead of having to perform the calculation.
Look-Up Tables: Example Example: Suppose your program frequently needs to raise numbers to the 2 nd power. Instead of including instructions in your program to do the math, you can store a table of squares in memory, and then look up the values when you need them. xx2x2 00 11 24 39 416 525
Implementing Our Look-Up Table Example: Part 1 First, we need to store the square values in consecutive bytes of memory. Typically youll store them in EEPROM so that the values are retained when power is lost. AddressValue $05000 $05011 $05024 $05039 $050416 $050525 …… Lets say we want our look-up table to start at address $0500. Then heres what we need to store in memory:
The DC.B Assembler Directive The DC.B (Define Constant Byte) directive lets us set up constant values in memory bytes. To define our look-up table, wed do the following: ORG $0500 DC.B 0, 1, 4, 9, 16, 25, 36
Three Ways to Load Values into Memory: First Way You now know at least three ways to place a specific value into a memory location. Example: Suppose we want to place the value $A1 into memory location $0600. The first way is to do it manually, using CodeWarriors Memory window.
Three Ways to Load Values into Memory: Second Way The second way is to use HCS12 instructions in your program, which of course will execute when the program runs. LDAA #$A1 STAA $0600
Three Ways to Load Values into Memory: Third Way The third way is to use the DC.B assembler directive, which places the value into memory when the program is downloaded to the chip, before the program runs. ORG $0600 DC.B $A1
Implementing Our Look-Up Table Example: Part 2 Now that weve defined our look-up table in memory, how do we use the table? Heres where indexed addressing is handy. Suppose we have a number in Accumulator B and we want to load that numbers square into Accumulator A. Heres how to do it: LDX #$0500 ;point to the table LDAA B,X ;load tables Bth value
Putting It All Together Combining the two pieces, we have: ABSENTRY Entry ;Define the look-up table. ORG $0500 DC.B 0, 1, 4, 9, 16, 25, 36 ;Use the look-up table. ORG $2000 Entry: LDX #$0500 ;point to the table LDAA B,X ;load tables Bth value
Using a Label for the Table Instead of using the tables address, wed do better to use a label: ABSENTRY Entry ;Define the look-up table. ORG $0500 Table: DC.B 0, 1, 4, 9, 16, 25, 36 ;Use the look-up table. ORG $2000 Entry: LDX #Table ;point to the table LDAA B,X ;load tables Bth value
Checksums A checksum byte is an extra byte appended to data that is being transmitted. Its purpose is to allow error checking by the receiver. Similar to a parity bit.
Different Ways to Compute Checksums There are many different ways to compute checksum bytes. Theyre all effective, as long as the sender and the receiver are using the same method. Similar to parity, where we can use even parity or odd parity. It doesnt matter which one we use, as long as the sender and receiver use the same method. See next two slides for the checksum method discussed in Chapter 6. But as well see in Chapter 8, CodeWarrior uses a slightly different method when sending a program to the HCS12 chip.
Chapter 6s Method for Generating the Checksum Byte The sender uses the following method to calculate the checksum byte: 1.Add all of the data bytes together, discarding any carries. 2.Take the twos-complement of the sum. The result of Step 2 is the checksum byte, which is appended to the data bytes when the data is transmitted.
Chapter 6s Method for Checking the Checksum The receiver uses the following method to check for errors: 1.Add all of the data bytes and the checksum byte together, discarding any carries. 2.The result should equal 0. If it does not equal 0, an error has occurred during transmission.
Review: Subroutines Subroutines are useful for at least two reasons: 1.They let you execute code repeatedly without having to type the code again. 2.They help your organize long programs into separate parts that each do their own little task. Example: Review the use of the Delay subroutine in Lab05BlinkingLEDs.Lab05BlinkingLEDs
Macros and Modules Macros and modules are similar to subroutines. For example, think of a module as a subroutine that is saved in its own separate file, instead of being part of the main.asm file. The book explains the extra steps you must take to call code in one file from another file. Macros and modules are advanced topics that we wont use in this course.