Download presentation
Presentation is loading. Please wait.
1
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Introduction to Microcomputer Systems University of Colorado at Colorado Springs Lecture #10 Agenda Today 1) Branch Instructions/Conditional Branches 2) CCR flags: N,H,Z,V,C Flags 3) Compare Instructions Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
2
Branch Instructions We need a way to alter the flow of program execution when specific conditions exist. This allows our programs to dynamically adjust to input. The Condition Code Register provides the status of the CPU. CCR[7:0] = {S,X,H,I,N,Z,V,C} S = Stop Disable X = X interrupt mask H = Half Carry I = I interrupt mask N = Negative Result Z = Zero Result V = Overflow C = Carry START LDAA INCA ACCA=10? NO YES Only these flags affect behavior of conditional branch instructions STAA END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
3
ECE 3430 – Intro to Microcomputer Systems
Branch Instructions CCR[7:0] = {S,X,H,I,N,Z,V,C} - These flags are altered upon execution of an instruction (check pink book to see which flags). - We can use a conditional branch instruction depending on these flags to set the new value of the PC. - A branch occurs when the PC is set to different place in the code (besides the next sequential instruction). - Branches in the HC11 always use 8-bit relative addressing. This gives a range of –128 to +127 memory locations relative to where the PC is starting. In other words, the PC can be changed to a value within –128 to +127 of the current PC location. - Labels provide an easy way to mark the destination of a branch. This way, the assembler can calculate the branch instruction’s operand value for you! - Unconditional Branch – BRA is an instruction that will always branch when executed regardless of what bits are set in the CCR. BRN is an instruction that never branches (another no-op instruction). BRA/BRN are the only two unconditional branch instructions in HC11 instruction set. - Conditional Branch – When executed, it will check the CCR. If the branch condition is true, it will set the PC to the new address location. If the branch condition is false, it will increment the PC as in normal operation. All branch instructions other than BRA/BRN in the HC11 instruction set are conditional. Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
4
ECE 3430 – Intro to Microcomputer Systems
Conditional Branches Carry Flag Branches BCC = branch if carry clear, C=0 BCS = branch if carry set, C=1 BHS = branch if higher or same, C=0 BLO = branch if lower, C= ORG $E LDAA #$F0 MAIN: ADDA #$01 BCC MAIN ; C = 0 = branch ; C = 1 = no branch STAA $ END Why can’t we use INCA rather than ADDA #$01? START alias A = $F0 alias A = A +1 SUM > 255? NO YES M($40) = A END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
5
ECE 3430 – Intro to Microcomputer Systems
Conditional Branches Carry Flag Branches How many times did this loop execute? Loop1 – A=$F1 Loop2 – A=$F2 Loop3 – A=$F3 : : Loop15 – A=$FF Loop16 – A=$00, C=1, No Branch ANSWER = 16 times START A = $F0 A = A +1 SUM > 255? NO YES M($40) = A END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
6
Conditional Branches YES NO END No operation, used for timing
Carry Flag Branches – Add M($40) and M($41) - If the result is =< 255, store to Port D and END - If the result is > 255, set PA4 (clear all other bits in port A) and END ORG $E000 MAIN: LDAA $40 ADDA $41 BCS ERROR ; C = 1 = branch ; C = 0 = no branch STAA $08 BRA FINISH ERROR: LDAA #% STAA $00 NOP FINISH: BRA FINISH START M($40) + M($41) SUM > 255? YES PA4 = 1 NO PortD = sum END No operation, used for timing Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
7
ECE 3430 – Intro to Microcomputer Systems
Conditional Branches Carry Flag Branches – What does the machine code look like? Label Addr Data INST Bytes Cycles MAIN $E000 $96 LDAA $ $E001 $ $E002 $9B ADDA $ $E003 $ $E004 $25 BCS ERROR 2 3 $E005 $REL - $E006 $97 STAA $ $E007 $ $E008 $20 BRA FINISH 2 3 $E009 $REL - ERROR $E00A $86 LDAA #% $E00B $10 - $E00C $97 STAA $ $E00D $00 - $E00E $01 NOP FINISH $E00F - Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
8
ECE 3430 – Intro to Microcomputer Systems
Conditional Branches How long does it take to execute if the branch IS taken? LDAA $ ADDA $ BCS ERROR … LDAA #% STAA $ NOP 2 (17 cycles)(500ns) = 8.5us How long does it take to execute if the branch IS NOT taken? LDAA $ ADDA $ BCS ERROR STAA $ BRA FINISH 3 (15 cycles)(500ns) = 7.5us What is the average execution time? (7.5us + 8.5us) = 8us Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
9
ECE 3430 – Intro to Microcomputer Systems
Conditional Branches Zero Flag Branches BEQ = branch if equal to zero, Z=1 BNE = branch if NOT equal to zero, Z=0 Ex) loop 10 times ORG $E LDAA #10 LOOP: DECA BNE LOOP ; Z = 0 = branch ; Z = 1 = no branch END START count = 10 count = count - 1 count = 0? NO YES END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
10
ECE 3430 – Intro to Microcomputer Systems
Conditional Branches Zero Flag Branches How many loops? Loop1 –> A=9 Loop2 –> A=8 : Loop10 –> A=0 (Z=1, branch NOT taken) How long to execute? LOOP -> DECA 2 cycles BNE 3 cycles (5 cycles)(10 loops) = 50 cycles LDAA # cycles TOTAL = (52 cycles)(500ns) = 26 us START count = 10 count = count - 1 count = 0? NO YES END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
11
ECE 3430 – Intro to Microcomputer Systems
Conditional Branches Negative Flag Branches BPL = branch if positive, N=0 BMI = branch if minus, N=1 Can be used during subtraction to indicate the value was negative. ex) M($40) – M($41) if positive, END if negative or zero, set PA4 (clear all other bits on port A) ORG $E LDAA $40 SUBA $41 BPL DONE LDAA #% STAA $00 DONE: BRA DONE END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
12
ECE 3430 – Intro to Microcomputer Systems
Conditional Branches Two’s Complement Overflow Flag Branches BVS = branch if overflow occurred, V=1 BVC = branch if NO overflow occurred, V=0 Other Useful Branches (using combinations of CCR flags) BGE = branch if greater than or equal to ; N V = 0 BLT = branch if less than ; N V = 1 BGT = branch if greater than ; Z + (N V) = 0 BLE = branch if less than or equal to ; Z + (N V) = 1 BHI = branch if higher ; C + Z = 0 BLS = branch if lower or the same ; C + Z = 1 Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
13
Signed vs. Unsigned Branch Instructions
BHI = branch if higher than BHS = branch if higher than or the same BLO = branch if lower than BLS = branch if lower than or the same Signed branch instructions: BGE = branch if greater than or equal BGT = branch if greater than BLE = branch if less than or equal BLT = branch if less than BMI = branch if negative flag is set BPL = branch if negative flag is clear Neither: BCC = branch if carry flag is clear BCS = branch if carry flag is set BEQ = branch if equal (zero flag is set) BNE = branch if not equal (zero flag is clear) BRA = branch always (unconditional branch) BRN = branch never (unconditional branch – like the NOP instruction) BVC = branch if overflow flag is clear BVS = branch if overflow flag is set Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
14
ECE 3430 – Intro to Microcomputer Systems
Conditional Branches Compare Instructions Branch instructions do not modify CCR bits. They simply read the current state of the CCR bits. At times we want to alter the CCR but not change the contents of the accumulators. By altering the state of the CCR bits, we can control what value the branch condition is relative to --rather than always zero. Compare instructions allow us to do this: CBA = A – B = compare accumulator A to accumulator B CMPA = A - M($??) = compare accumulator A to memory CMPB = B - M($??) = compare accumulator B to memory CPD = D - M($??) = compare accumulator D to memory CPX = X - M($??) = compare index register X to memory CPY = Y - M($??) = compare index register Y to memory TST = M($??) – 0 = test for zero or minus (on memory location) TSTA = A – 0 = test for zero or minus (on ACCA) TSTB = B – 0 = test for zero of minus (on ACCB) Typically, the above compare/test instructions should immediately precede a branch instruction! Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
15
Using Compare Instructions to Influence Branch Instruction Behavior
PORTA: EQU $0000 ORG $E000 START: LDAA $40 CMPA #120 BGT SET4 CLR4: LDAB PORTA ANDB #% BRA REJOIN SET4: LDAB PORTA ORAB #% REJOIN: STAB PORTA … START A = M($0040) A > 120? YES PA4 = 1 NO PA4 = 0 END Optimization: Move LDAB after LDAA and memory/time can be saved. Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
16
Using Compare Instructions to Influence Branch Instruction Behavior
BGT (signed) takes branch if “Z + (N V) = 0” evaluates as true. BHI (unsigned) takes branch if “C + Z = 0” evaluates as true. Assume M($0040) = 128 S X H I N Z V C LDAA $ CMPA # BGT SET (0 1) = 0 1 = 0 no branch BHI SET = 0 0 = 0 branch = negative – positive = positive overflow Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
17
Using Compare Instructions to Influence Branch Instruction Behavior
BGT (signed) takes branch if “Z + (N V) = 0” evaluates as true. BHI (unsigned) takes branch if “C + Z = 0” evaluates as true. Assume M($0040) = 100 S X H I N Z V C LDAA $ CMPA # BGT SET (1 0) = 0 1 = 0 no branch BHI SET = 0 1 = 0 no branch = positive – positive = negative Subtracted larger number from smaller (borrow). Result is negative. Negative, no overflow, carry (due to borrow) Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2009
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.