Floyd, Digital Fundamentals, 10 th ed EET 2261 Unit 7 Indexed Addressing Mode Read Mazidi, Chapter...

28
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.

Transcript of Floyd, Digital Fundamentals, 10 th ed EET 2261 Unit 7 Indexed Addressing Mode Read Mazidi, Chapter...

Page 1: 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.

Floyd, Digital Fundamentals, 10th ed

EET 2261 Unit 7Indexed Addressing Mode

Read Mazidi, Chapter 6.

Homework #7 and Lab #7 due next week.

Quiz next week.

Page 2: 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 HCS12’s six addressing modes are:

• Inherent• Immediate• Direct• Extended• Relative• Indexed (which has several variations)

• We’ve studied all of these except indexed addressing mode.

Page 3: 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.

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, we’ll use only the three in bold above. For the others, see the section starting on p. 34 of the HCS12 CPU Reference Manual.

Page 4: 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.

The Original Indexed Addressing Mode

• In older microcontrollers (including the Freescale HC11) “indexed addressing” meant what we’re calling constant offset indexed addressing. The HCS12 added the other variations.

Page 5: 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.

Variation #1: Constant Offset Indexed Addressing Mode

• In constant offset indexed addressing mode, the operand’s 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.

Page 6: 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.

Simple Example of Indexed Addressing

ORG $2000

LDX #$1500LDY #$1600

LDAA 3,XINCASTAA 8,Y

BRA *END

Page 7: 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.

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.

Page 8: 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.

Copying a Block of Data: The Hard Way

• Without indexed addressing mode, we’d 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

.

.

.

Page 9: 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.

Copying a Block of Data: The Smart Way

• With indexed addressing, it’s much easier:

ORG $2000 LDAB #$90 ;number of bytes LDX #$1200 ;pointer to source bytes LDY #$1300 ;pointer to destination bytesL1: LDAA 0,X STAA 0,Y INX INY DECB BNE L1 END

Page 10: 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.

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+

Page 11: 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.

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 bytesL1: LDAA 1,X+ STAA 1,Y+ DECB BNE L1 END

Page 12: 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.

Variation #3: Accumulator Offset Indexed Addressing Mode

• In accumulator offset indexed addressing, the operand’s 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.

Page 13: 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.

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.

Page 14: 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.

Look-Up Tables: Example• Example: Suppose your program frequently

needs to raise numbers to the 2nd 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.

x x2

0 0

1 1

2 4

3 9

4 16

5 25

Page 15: 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.

Implementing Our Look-Up Table Example: Part 1

• First, we need to store the square values in consecutive bytes of memory. Typically you’ll store them in EEPROM so that the values are retained when power is lost.

Address Value

$0500 0

$0501 1

$0502 4

$0503 9

$0504 16

$0505 25

… …

• Let’s say we want our look-up table to start at address $0500. Then here’s what we need to store in memory:

Page 16: 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.

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, we’d do the following:

ORG $0500 DC.B 0, 1, 4, 9, 16, 25, 36

Page 17: 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.

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 CodeWarrior’s Memory window.

Page 18: 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.

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

Page 19: 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.

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

Page 20: 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.

Implementing Our Look-Up Table Example: Part 2

• Now that we’ve defined our look-up table in memory, how do we use the table? Here’s where indexed addressing is handy.

• Suppose we have a number in Accumulator B and we want to load that number’s square into Accumulator A. Here’s how to do it:

LDX #$0500 ;point to the table LDAA B,X ;load table’s Bth value

Page 21: 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.

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 $2000Entry: LDX #$0500 ;point to the table LDAA B,X ;load table’s Bth value

Page 22: 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.

Using a Label for the Table• Instead of using the table’s address, we’d

do better to use a label:

ABSENTRY Entry ;Define the look-up table. ORG $0500Table: DC.B 0, 1, 4, 9, 16, 25, 36

;Use the look-up table. ORG $2000Entry: LDX #Table ;point to the table LDAA B,X ;load table’s Bth value

Page 23: 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.

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.

Page 24: 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.

Different Ways to Compute Checksums

• There are many different ways to compute checksum bytes. They’re 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 doesn’t 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 we’ll see in Chapter 8, CodeWarrior uses

a slightly different method when sending a program to the HCS12 chip.

Page 25: 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.

Chapter 6’s 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 two’s-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.

Page 26: 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.

Chapter 6’s 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.

Page 27: 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: 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.

Page 28: 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.

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 won’t use in this course.