Presentation is loading. Please wait.

Presentation is loading. Please wait.

EET 2261 Unit 5 Tables; Decision Trees & Logic Instructions  Read Almy, Chapters 9 and 10.  Homework #5 and Lab #5 due next week.  Exam #1 next week.

Similar presentations


Presentation on theme: "EET 2261 Unit 5 Tables; Decision Trees & Logic Instructions  Read Almy, Chapters 9 and 10.  Homework #5 and Lab #5 due next week.  Exam #1 next week."— Presentation transcript:

1 EET 2261 Unit 5 Tables; Decision Trees & Logic Instructions  Read Almy, Chapters 9 and 10.  Homework #5 and Lab #5 due next week.  Exam #1 next week.

2 Review: Addressing Modes The HCS12’s six addressing modes are: Inherent Immediate Direct Extended Indexed (which has several variations) Relative We briefly looked at indexed addressing mode two weeks ago. Let’s return to it now.

3 Review: 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 pages in the textbook or the section starting on p. 34 of the HCS12 CPU Reference Manual.HCS12 CPU Reference Manual

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

5 Review: Simple Example of Indexed Addressing ORG $2000 LDX #$1500 LDY #$1600 LDAA 3,X INCA STAA 8,Y BRA * END

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

7 Copying a Block of Data: The Brute-Force 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......

8 Copying a Block of Data: The Smart Way With indexed addressing, the code is much shorter: 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

9 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+

10 Copying a Block of Data With Auto Post-Increment Indexed Addressing Once more, our earlier program made shorter: 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

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

12 Look-Up Tables Indexed addressing mode is 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.

13 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. xx2x

14 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. AddressValue $05000 $05011 $05024 $05039 $ $ …… Let’s say we want our look-up table to start at address $0500. Then here’s what we need to store in memory:

15 The DC Assembler Directive The DC (Define Constant) directive lets us set up constant values in memory bytes. To define our look-up table, we’d do the following: ORG $0500 DC 0, 1, 4, 9, 16, 25, 36

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

17 Three Ways to Load Values into Memory: Second Way The second way is to use HCS12 instructions in your program, which will execute when the program runs. LDAA #$A1 STAA $0600

18 Three Ways to Load Values into Memory: Third Way The third way is to use the DC assembler directive, which places the value into memory when the program is downloaded to the chip, before the program runs. ORG $0600 DC $A1

19 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 accumulator offset 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

20 Putting It All Together Combining the two pieces, we have: ABSENTRY Entry ;Define the look-up table. ORG $0500 DC 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 table’s Bth value

21 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 $0500 Table: DC 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 table’s Bth value

22 Review: Using Branch Instructions for Iteration (Loops) In Unit 4 we saw two uses for branch instructions: We used BRA instruction to create “forever” loops. We used BNE instruction to create counted loops. (See next two slides for review.) After reviewing those two uses, we’ll move on to other uses of branch instructions.

23 ABSENTRY Entry ORG $2000 Entry: LDAA $1000 GoHere: INCA BRA GoHere END Review: A “Forever” Loop Example Start Load A from $1000 Program Flowchart Increment A

24 Review: A Counted Loop Example ABSENTRY Entry ORG $2000 Entry: LDAA $1000 LDAB #5 ;Init. Counter Again: INCA ;Do action DECB ;Dec. Counter BNE Again ;Counter=0? STAA $1001 END Counter=5 Counter = 0? Increment A Decrement Counter No Yes Start End Load A from $1000 Store A to $1001

25 Conditional Structure Action Condition? No Yes Often we want to check some condition and then either perform an action or skip the action, depending on whether the condition is true or false.

26 Yes No ABSENTRY Entry ORG $2000 Entry: LDAA $1000 BEQ GoHere INCA GoHere: STAA $1001 END Conditional Structure: Example Start Load A from $1000 End Increment A Store A in $1001 A = 0? Program Flowchart

27 A Second Conditional Structure Action 1 Condition? No Yes Action n This is similar to the previous one, but this time there are several actions that we we’ll either do or skip, depending on whether the condition is true or false.

28 Yes No ABSENTRY Entry ORG $2000 Entry: LDAA $1000 BEQ GoHere LDAB $1001 INCB ABA GoHere: STAA $1001 END A Second Conditional Structure: Example Start Load A from $1000 End Load B from $1001 Store A in $1001 A = 0? Increment B Add B to A Program Flowchart

29 A Third Conditional Structure Condition ? YesNo Action aAction b This time, instead of either doing an action or skipping it, we’re doing different actions depending on whether the condition is true or false.

30 Yes No ABSENTRY Entry ORG $2000 Entry: LDAA $1000 BEQ GoHere DECA BRA GoThere GoHere: INCA GoThere: STAA $1001 END A Third Conditional Structure: Example Start Load A from $1000 End Increment A Store A in $1001 A = 0? Decrement A Program Flowchart

31 Comparing Numbers Often, we want to compare two numbers to see whether one is greater than or less than the other. To do this we need other branch instructions. Read Temperature Temp > 70? Turn on LED 0 Turn on LED 1 Yes No Most of our previous branch examples checked to see whether a number was equal to 0. They did this by using BEQ or BNE.

32 How to Compare Numbers The general procedure for comparing two numbers is to execute a subtract instruction (such as SUBA or SUBB) or a compare instruction (such as CMPA or CMPB), followed by one of the branch instructions shown on the next slide. So it’s a two-step process: 1.Subtract or Compare 2.Branch

33 Branches for Comparing Numbers Note 1: Most of these branches check more than one bit in the CCR. Note 2: We have two sets of branches: one for comparing unsigned numbers and one for comparing signed (two’s-complement) numbers. Remember: These branches are meant to be used immediately after a subtract or compare instruction.

34 Yes No ABSENTRY Entry ORG $2000 Entry: LDAA $1000 CMPA #70 BHI GoHere INCA GoHere: STAA $1001 END Comparing Numbers : Example Start Load A from $1000 End Increment A Store A in $1001 A > 70? Program Flowchart Let’s assume we’re dealing with unsigned numbers.

35 Why Do We Need Unsigned Branches and Signed Branches? Consider this question: Is % greater than % ? Answer: It depends! If these are unsigned numbers, then % = 255 and % = 1. So the answer is YES. But if these are signed numbers, then % = -1 and % = 1. So the answer is NO.

36 Lots of Branches At this point you should be able to use any of the short branch instructions. (Table from p. 74 of the HCS12 CPU Reference Manual.)CPU Reference Manual

37 (Table from p. 63 of the HCS12 CPU Reference Manual.)CPU Reference Manual Boolean Logic Instructions

38 Suppose A and B are byte variables, with A=6 and B=12. Then A AND B = 4, because Also, A OR B = 14, because Also, A EOR B = 10, because Examples of Boolean Operations AND OR EOR

39 Of these logical operations, bitwise AND is the most widely used. It’s often used to “mask” some of the bits in a number. Example: suppose the user enters a value, but we only want to use the four lowest-order bits of that value, ignoring the four higher-order bits. We do this by applying a “mask” of Bitwise AND as a Masking Operation u 7 u 6 u 5 u 4 u 3 u 2 u 1 u 0 AND u 3 u 2 u 1 u 0 Bits entered by the user. Our mask. Result of masking operation.

40 (Table from p. 63 of the HCS12 CPU Reference Manual.)CPU Reference Manual Complement Instructions

41 Using instructions that we’ve studied, you can change the value of an entire byte in memory. Example: If you want memory location $1000 to hold the value % , here’s one way to do it: LDAA #$34 STAA $1000 What if you want to change a single bit of the byte held in a memory location? Can you do that? Yes, by using two new instructions. Review: STAA Stores an Entire Byte

42 Two instructions, BCLR and BSET, let us clear or set single bits in memory. (Table from p. 65 of the HCS12 CPU reference manual.) We’ll discuss BITA and BITB in future weeks.reference manual BCLR and BSET operate on individual bits, in contrast to STAA, which operates on an entire byte. Bit Manipulation Instructions

43 As we’re using the words here: “Set” means “set to 1.” “Clear” means “set to 0.” So you’ll use BSET when you want to force a bit to be 1, and you’ll use BCLR when you want to force a bit to be 0. “Set” and “Clear”

44 BCLR and BSET, as well as some other instructions we’ll study, use a byte mask. This is a byte that identifies which bit(s) in a byte we want to work with. Example: Suppose we want to use BCLR or BSET to change the values of bits 2, 3, and 5 of a byte in memory. Then the byte mask we would use is % Byte Masks

45 Example: Suppose we want to clear bit 2 of the byte stored at memory location $1500. Here’s how to do it: BCLR $1500, % Note the comma between the address and the byte mask. This instruction will clear bit 2 of the byte stored at memory location $1500, and will leave the other bits in that byte unchanged. BCLR: Example

46 Example: Suppose we want to set bits 2, 3, and 5 of the byte stored at memory location $1500. Here’s how to do it: BSET $1500, % This instruction will set bits 2, 3, and 5 of the byte stored at memory location $1500, and will leave the other bits in that byte unchanged. BSET: Example

47 From the Instruction Set Summary, we can see that BSET actually ORs the memory byte with our mask: (From p. 383 of the CPU Reference Manual.)CPU Reference Manual So BSET $1500, % does the same thing as the following sequence: LDAA $1500 ORAA #% STAA $1500 Details: BSET Actually Does an OR

48 From the Instruction Set Summary, we can see that BCLR actually ANDs the memory byte with the complement of our mask: (From p. 382 of the CPU Reference Manual.)CPU Reference Manual So BCLR $1500, % does the same thing as the following sequence: LDAA $1500 ANDA #% STAA $1500 Details: BCLR Actually Does an AND

49 Most branch instructions let you make decisions based on the values of bits in the Condition Code Register. Example: The following code loads a byte into accumulator A and then branches if the LDAA instruction resulted in the Z bit being set: LDAA $1000 BEQ GoHere What if you want to branch based on bits in a memory location? Can you do that? Yes, by using two new instructions. Review: Most Branch Instructions Look at Bits in the CCR

50 Two instructions, BRCLR and BRSET, let us branch based on one or more bits in a memory location. (Table from p. 76 of the HCS12 CPU reference manual.)reference manual Bit Condition Branch Instructions

51 Here’s an example that will branch if bits 2, 3, and 5 of the byte stored at memory location $1500 are all 0s (cleared). Otherwise it won’t branch: LDAA #5 BRCLR $1500, % , GoHere DECA BRA * GoHere: INCA BRA * BRCLR: Example

52 Here’s an example that will branch if bit 7 of the byte stored at memory location $1500 is a 1 (set). Otherwise it won’t branch: LDAA #5 BRSET $1500, % , GoHere DECA BRA * GoHere: INCA BRA * BRSET: Example


Download ppt "EET 2261 Unit 5 Tables; Decision Trees & Logic Instructions  Read Almy, Chapters 9 and 10.  Homework #5 and Lab #5 due next week.  Exam #1 next week."

Similar presentations


Ads by Google