1 / 51

EET 2261 Unit 4 Branching & Iteration; Decision Trees & Logic Instructions

EET 2261 Unit 4 Branching & Iteration; Decision Trees & Logic Instructions. Read Almy , Sections 8 and 10. Homework #4 and Lab #4 due next week. Flowcharts. A flowchart is a diagram that shows the logic of a computer program. (See pages 41-42 of textbook.)

bambi
Télécharger la présentation

EET 2261 Unit 4 Branching & Iteration; Decision Trees & Logic Instructions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EET 2261 Unit 4Branching & Iteration; Decision Trees & Logic Instructions • Read Almy, Sections 8 and 10. • Homework #4 and Lab #4 due next week.

  2. Flowcharts • A flowchart is a diagram that shows the logic of a computer program. (See pages 41-42 of textbook.) • Arrows show the flow of the logic. • A diamond represents a decision point, with alternate paths based on the decision. Read Temperature Temp > 70? Yes Turn on LED 0 No Turn on LED 1

  3. Straight-Line Structure • The programs we’ve studied up to now had a straight-line structure, with no decision points. • Instructions executed in order from start to finish. Start Action 1 Action 2 ... Action n End

  4. Straight-Line Structure: Example Flowchart Program Start ABSENTRY Entry ORG $2000 Entry: LDAA #05 ADDA #17 LDAB #03 INCA DECB ADDB #$1A END Load 5 into A Add 17 to A Load 3 into B Increment A Decrement B Add $1A to B End

  5. Review: Program Counter • How does the HCS12 keep track of where it is in a program? That’s the job of the program counter (PC), a 16-bit register that points to the address of the next instruction to be executed. • Each time an instruction is executed, the PC is automatically increased by the size of this instruction (a number from 1 to 5) so that the PC now points to the next instruction in memory.

  6. Program Counter: Example PC $2002 $2004 $2006 $2007 $2000 • Initially PC = $2000, pointing to the LDAA. • While the LDAA is being executed, PC = $2002, pointing to the ADDA. • While the ADDA is being executed, PC = $2004, pointing to the LDAB. • While the LDAB is being executed, PC = $2006, pointing to the INCA. • While the INCA is being executed, PC = $2007, pointing to the DECB.

  7. Endless Loop Structure ... • Sometimes we want a program to contain an endless loop, in which we keep repeating the same action over and over again forever. Action

  8. Branch Instructions • To program the endless loop shown on the previous slide, we use a branch instruction. • As we’ll see, the HCS12 has more than 20 different branch instructions. • Branch instructions can change the default order of execution by forcing the program counter to point to an instruction other than the next one in memory.

  9. Branch Always (BRA) • The branch instruction that we need for our endless loop is Branch Always (BRA). • It’s the simplest branch instruction, because it always branches. • We’ll soon study other branches that check a condition and then, depending on that condition, may or may not branch. • These other branches are often calledconditional branches, while BRA is an unconditional branch.

  10. Endless Loop Structure: Example Flowchart Program Start ABSENTRYEntry ORG $2000 Entry: LDAA $1000 GoHere: INCA BRA GoHere END Load A from $1000 Increment A

  11. Review: Addressing Modes • The HCS12’s six addressing modes are: • Inherent • Immediate • Direct • Extended • Relative • Indexed (which has several variations) • We’ve studied the first four, and now need to understand relative addressing mode.

  12. Relative Addressing Mode • Relative addressing mode is used only by branch instructions. • In this addressing mode, the opcode is followed by an offset, which is a two’s-complement number (positive for branching forward in a program, negative for branching backwards). • Example: In the previous program, BRA GoHere in assembly language translates into $20 FD in machine code. The offset, $FD, is −3 in two’s complement notation.

  13. Using Labels with Branch Instructions • Figuring out the offset can be a tedious chore. • Fortunately, the assembler lets you use labels instead to specify the branch destination; then the assembler figures out the offset.

  14. Conditional 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. Yes Condition? No Action ...

  15. Branch Instructions and the CCR • Most branch instructions examine one or more of the flag bits in the Condition Code Register (CCR) to decide whether to continue with the next instruction in memory or jump to a different instruction.

  16. Simple Branch Instructions • The simple branch instructions examine a single bit in the CCR. • Mnemonic Function Condition Checked • (Table from p. 74 of the HCS12 reference manual.)

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

  18. A 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. Yes Condition? No Action 1 ... Action n ...

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

  20. A 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. Yes No Condition? Action a Action b ...

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

  22. A Self-Pointing Branch • We saw in Lab #2 that BRA * is a self-pointing branch instruction. • The assembler treats BRA * as being the same as the following instruction:GoHere: BRA GoHere • It’s useful as a way of stopping a program that has finished its task. Otherwise the processor would try to continue past the end of your program, executing whatever instructions happen to be located in memory after your program.

  23. A Self-Pointing Branch: Example Program Start ABSENTRYEntry ORG $2000 Entry: LDAA $1000 GoHere: BRA GoHere END Load A from $1000

  24. Repeating an Action Many Times Start • Often we want to perform an action several times. The brute-force way to do this is by using a straight-line program in which we repeat the same instruction the correct number of times. Do the action Do it again ... Do it again End

  25. Brute Force Method: Example Start ABSENTRYEntry ORG $2000 Entry: LDAA $1000 INCA INCA INCA INCA INCA STAA $1001 END Load A from $1000 Increment A Increment A Increment A Increment A Increment A Store A to $1001 End

  26. Counted Loop Structure Start • A better way is to use a counted loop, as shown here. Initialize Counter Do the action Decrement Counter No Counter= 0? Yes End

  27. Counted Loop Structure: Example Start 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 Load A from $1000 Counter=5 Increment A Decrement Counter No Counter= 0? Yes Store A to $1001 End

  28. Time Delay Loops • Counted loops are often used to introduce time delays. The idea is that we just spin around and around in a counted loop, wasting time. • Example: • ;Doing some stuff. • LDAB #255 ;Waste some time. • Again: DECB • BNE Again • ;Do more stuff. ... ...

  29. Calculating the Time Delay • We can calculate the approximate delay by knowing the microcontroller’s cycle time (in our case,  41.7 ns) and how many cycles are required to execute each instruction. • Example: Number of cycles • LDAB #255 1 • Again: DECB 1 • BNE Again +3 • 4 × 255 = 1020 • 1021 • Total delay  1021 × 41.7 ns  42.5 µs

  30. How to Get Longer Delays? • The previous timing loop gave us a delay of only about 42 µs. Here three ways to get longer delays: • Use a 16-bit register such as Index Register X instead of the 8-bit Accumulator B. • Place more instructions inside the loop. • Use nested loops. • All three techniques are used on the next slide.

  31. Nested Timing Loops • If a single loop doesn’t give us a long enough delay, we can use nested loops (a loop inside a loop) to waste even more time! • Example: • LDAB #10 • Loop2: LDX #$FFFF • Loop1: DEXNOP • BNE Loop1 • DECB • BNE Loop2 Inner Loop Outer Loop

  32. Short Branches • In short branch instructions, the offset is an eight-bit signed number. (Eight bits = One byte.) • So the offset can range from 128 to +127. • Older microcontrollers only had short branch instructions, limiting how far away you could branch. • All of the branch instructions we’ve discussed (and others that we’ll discuss in two weeks) are short branch instructions. See next slide.

  33. Short Branch Instructions (Table from p. 74 of the HCS12 CPU Reference Manual.)

  34. Is This a Problem? • Is it a problem that the offset can range only from 128 to +127? • Not in short programs, but it can be in long programs. ... BEQ GoHere GoHere: STAA $1001 Suppose there are a few hundred instructions between the BEQ and the STAA. That’s too far away! You’ll get an error when you try to assemble the program. ...

  35. Long Branches • The HCS12 also has long branch instructions, in which the offset is a sixteen-bit signed number. (Sixteen bits = Two bytes.) • The offset can range from 32,768 to +32,767, letting us branch to any other location in the HCS12’s 64K memory space. • There’s a long-branch version of each short branch instruction. See next slide.

  36. Long Branch Instructions (Table from p. 75 of the HCS12 CPU Reference Manual.)

  37. Jump • The Jump (JMP) instruction is similar to BRA or LBRA. It forces the program to jump to a new instruction by loading a value into the Program Counter. • While BRA and LBRA use relative addressing mode, JMP uses extended or indexed addressing. • Example: JMP $2050 causes $2050 to be loaded into the Program Counter, so that the instruction located at that address will be executed next.

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

  39. Examples of Boolean Operations • Suppose A and B are byte variables, with A=6 and B=12. • Then AANDB = 4, because • Also, AORB = 14, because • Also, AEORB = 10, because 0000 0110 AND 0000 1100 0000 0100 0000 0110 OR 0000 1100 0000 1110 0000 0110 EOR 0000 1100 0000 1010

  40. Bitwise 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” of 0000 1111. u7u6u5u4 u3u2u1u0 AND 0 0 0 0 1 1 1 1 0 0 0 0 u3u2u1u0 Bits entered by the user. Our mask. Result of masking operation.

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

  42. Review: 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 %00110100, 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, but not by using instructions that we’ve studied.

  43. Bit 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.

  44. “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.

  45. Byte Masks • 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 %00101100.

  46. BCLR: Example • Example: Suppose we want to clear bit 2 of the byte stored at memory location $1500. • Here’s how to do it:BCLR $1500, %00000100 • 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.

  47. BSET: Example • 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, %00101100 • 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.

  48. Review: 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 $1000 • BEQ GoHere • What if you want to branch based on bits in a memory location? Can you do that? Yes, but not by using the branch instructions that we’ve discussed previously.

  49. Bit 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.)

  50. BRCLR: Example • 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 #5BRCLR $1500, %00101100, GoHere • DECA • BRA * • GoHere: INCA • BRA *

More Related