Presentation on theme: "EET 2261 Unit 5 Tables; Decision Trees & Logic Instructions"— Presentation transcript:
1EET 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.-Handout: Boolean practice sheet.
2Review: Addressing Modes The HCS12’s six addressing modes are:InherentImmediateDirectExtendedIndexed (which has several variations)RelativeWe briefly looked at indexed addressing mode two weeks ago. Let’s return to it now.
3Review: Indexed Addressing Mode Indexed addressing mode comes in at least five variations:Constant offset indexed addressingAuto pre/post decrement/increment indexed addressingAccumulator offset indexed addressingConstant indirect indexed addressingAccumulator D indirect indexed addressingOf 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.
4Review: 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.
5Review: Simple Example of Indexed Addressing ORG $2000LDX #$1500LDY #$1600LDAA 3,XINCASTAA 8,YBRA *END
6Copying a Block of DataA 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.Ask them how many bytes we’re copying.
7Copying a Block of Data: The Brute-Force Way Without indexed addressing mode, we’d have to do something like the following:ORG $2000LDAA $1200 ;copy first byteSTAA $1300LDAA $1201 ;copy second byteSTAA $1301LDAA $128F ;copy last byteSTAA $138F
8Copying a Block of Data: The Smart Way With indexed addressing, the code is much shorter:ORG $2000LDAB #$90 ;number of bytesLDX #$1200 ;pointer to source bytesLDY #$1300 ;pointer to destination bytesL1: LDAA 0,XSTAA 0,YINXINYDECBBNE L1ENDSingle-step it in CodeWarrior.
9Variation #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,XINXWe can use this one instruction:LDAA 1,X+
10Copying a Block of Data With Auto Post-Increment Indexed Addressing Once more, our earlier program made shorter:ORG $2000LDAB #$90 ;number of bytesLDX #$1200 ;pointer to source bytesLDY #$1300 ;pointer to destination bytesL1: LDAA 1,X+STAA 1,Y+DECBBNE L1ENDThis program is very similar to three of the programs that they’ll write in Lab #5.
11Variation #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.
12Look-Up TablesIndexed 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.
13Look-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.xx21243916525
14Implementing 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.Let’s say we want our look-up table to start at address $ Then here’s what we need to store in memory:AddressValue$0500$05011$05024$05039$050416$050525…
15The 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 $0500DC 0, 1, 4, 9, 16, 25, 36Do this much in CodeWarrior to see that it works.
16Three 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.
17Three 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
18Three 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 $ DC $A1
19Implementing 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 tableLDAA B,X ;load table’s Bth value
20Putting It All Together Combining the two pieces, we have:ABSENTRY Entry;Define the look-up table.ORG $0500DC 0, 1, 4, 9, 16, 25, 36;Use the look-up table.ORG $2000Entry: LDX #$0500 ;point to the tableLDAA B,X ;load table’s Bth value-Note that we have two ORG directives: one tells where our look-up table is located, and the other tells where our code is located.-Single-step it in CodeWarrior, manually loading different values into AccB.
21Using 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 0, 1, 4, 9, 16, 25, 36;Use the look-up table.ORG $2000Entry: LDX #Table ;point to the tableLDAA B,X ;load table’s Bth value
22Review: 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.
23Review: A “Forever” Loop Example FlowchartProgramStartABSENTRY EntryORG $2000Entry: LDAA $1000GoHere: INCABRA GoHereENDLoad A from $1000Increment A
24Review: A Counted Loop Example StartABSENTRY EntryORG $2000Entry: LDAA $1000LDAB #5 ;Init. CounterAgain: INCA ;Do actionDECB ;Dec. CounterBNE Again ;Counter=0?STAA $1001ENDLoad A from $1000Counter=5Increment ADecrement CounterNote that in both of these examples, the branches took us back to earlier instructions. That’s typical for loops.NoCounter= 0?YesStore A to $1001End
25Conditional Structure 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.YesCondition?NoAction
26Conditional Structure: Example FlowchartProgramStartABSENTRY EntryORG $2000Entry: LDAA $1000BEQ GoHereINCAGoHere: STAA $1001ENDLoad A from $1000A = 0?YesNoIncrement AEnter and single-step it with different initial values in $ Watch what happens in the Source, Assembly, Register, and Memory windows.Store A in $1001End
27A Second Conditional Structure 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.YesCondition?NoAction 1Action n
28A Second Conditional Structure: Example FlowchartProgramStartABSENTRY EntryORG $2000Entry: LDAA $1000BEQ GoHereLDAB $1001INCBABAGoHere: STAA $1001ENDLoad A from $1000YesA = 0?NoLoad B from $1001Enter and single-step it with different initial values in $ Watch what happens in the Source, Assembly, Register, and Memory windows.Increment BAdd B to AStore A in $1001End
29A Third Conditional Structure 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.YesCondition?NoAction aAction b
30A Third Conditional Structure: Example FlowchartProgramStartABSENTRY EntryORG $2000Entry: LDAA $1000BEQ GoHereDECABRA GoThereGoHere: INCAGoThere: STAA $1001ENDLoad A from $1000YesA = 0?NoIncrement ADecrement AEnter and single-step it with different initial values in $ Watch what happens in the Source, Assembly, Register, and Memory windows.Store A in $1001End
31Comparing NumbersMost of our previous branch examples checked to see whether a number was equal to 0. They did this by using BEQ or BNE.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 TemperatureTemp > 70?YesTurn on LED 0NoTurn on LED 1
32How to Compare NumbersThe 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:Subtract or CompareBranch
33Branches 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.
34Comparing Numbers : Example Let’s assume we’re dealing with unsigned numbers.FlowchartProgramStartABSENTRY EntryORG $2000Entry: LDAA $1000CMPA #70BHI GoHereINCAGoHere: STAA $1001ENDLoad A from $1000A > 70?YesNo-Very similar to our first branch example, but that one checked =0.-Note from previous slide that BHI checks to see if C+Z=0.-Enter and single-step it with different initial values in $ Watch what happens in the Source, Assembly, Register, and Memory windows.Increment AStore A in $1001End
35Why 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.
36Lots of BranchesAt this point you should be able to use any of the short branch instructions.(Table from p. 74 of the HCS12 CPU Reference Manual.)
37Boolean Logic Instructions (Table from p. 63 of the HCS12 CPU Reference Manual.)This slide and the following four deal with Boolean Logic Instructions.
38Examples of Boolean Operations Suppose A and B are byte variables, with A=6 and B=12.Then A AND B = 4, becauseAlso, A OR B = 14, becauseAlso, A EOR B = 10, becauseANDOR-Have them do it in CodeWarrior.EOR
39Bitwise AND as a Masking Operation 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” ofu7u6u5u4 u3u2u1u0ANDu3u2u1u0Bits entered by the user.Our mask.Result of masking operation.
40Complement Instructions (Table from p. 63 of the HCS12 CPU Reference Manual.)-Do boolean practice sheet.-Also show that Windows Calculator can do these operations.
41Review: STAA Stores an Entire Byte 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 #$34STAA $1000What 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.
42Bit Manipulation Instructions 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.BCLR and BSET operate on individual bits, in contrast to STAA, which operates on an entire byte.
43“Set” and “Clear” 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.
44Byte MasksBCLR 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 %
45BCLR: ExampleExample: 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.Have them predict result if $1500 holds $8F. Then do it in CodeWarrior, first setting the value of $1500 to $8F.
46BSET: ExampleExample: 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.Have them predict result if $1500 holds $0. Then do it in CodeWarrior, first setting the value of $1500 to $0.
47Details: BSET Actually Does an OR 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.)So BSET $1500, % does the same thing as the following sequence: LDAA $ ORAA #% STAA $1500
48Details: BCLR Actually Does an AND 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.)So BCLR $1500, % does the same thing as the following sequence: LDAA $ ANDA #% STAA $1500
49Review: Most Branch Instructions Look at Bits in the CCR 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 $1000BEQ GoHereWhat if you want to branch based on bits in a memory location? Can you do that? Yes, by using two new instructions.
50Bit Condition Branch Instructions 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.)
51BRCLR: ExampleHere’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 # BRCLR $1500, % , GoHereDECABRA *GoHere: INCADo it in CodeWarrior.
52BRSET: ExampleHere’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 # BRSET $1500, % , GoHereDECABRA *GoHere: INCA