170 likes | 186 Views
This lecture discusses the use of conditional jump instructions in microcomputer systems to dynamically adjust program execution based on specific conditions. It covers jump instructions, status register flags, compare instructions, and various types of conditional jumps.
E N D
ECE 3430 – Introduction to Microcomputer SystemsUniversity of Colorado at Colorado Springs Lecture #10 Agenda Today 1) Jump Instructions/Conditional Execution 2) Status register flags: N,Z,V,C 3) Compare Instructions ECE 3430 – Intro to Microcomputer Systems Fall 2013
Jump 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 Status Register provides the status of the CPU. V = Overflow N = Negative Result Z = Zero Result C = Carry START <op> <loop op> <cond>x == y? NO YES Only these flags affect behavior of conditional jump instructions <op> END ECE 3430 – Intro to Microcomputer Systems Fall 2013
Jump Instructions SR[8,2:0] = {V,N,Z,C}- These flags are altered upon execution of an instruction (check instruction set to see which flags).- We can use a conditional jump instruction depending on these flags to set the new value of the PC.- A jump occurs when the PC is set to different place in the code (besides the next sequential instruction).- Jumps in the MSP430 always use 10-bit relative addressing. This gives a range of –512 to +511 words relative to where the PC is starting. In other words, the PC can be changed to a value within –512 to +511 words of the current PC location.- Labels provide an easy way to mark the destination of a jump. This way, the assembler can calculate the jump instruction’s operand value for you! - Unconditional Jump – “jmp” is an instruction that will always jump when executed regardless of what bits are set in the SR. “jmp” is the only unconditional jump instructions in MSP430 instruction set. - Conditional Jump – When executed, it will check the SR and evaluate a logical condition. If the jump condition is true, it will set the PC to the new address location. If the jump condition is false, it will increment the PC by 2 as in normal operation. All jump instructions other than “jmp” are conditional. ECE 3430 – Intro to Microcomputer Systems Fall 2013
Conditional Jumps Carry Flag Jumps jnc = jump if carry clear, C=0 jc = jump if carry set, C=1 jhs = jump if higher or same, C=1 jlo = jump if lower, C=0 ORG 0xF800 mov.b #0F0h,R4Loop: add.b #1,R4 jnc Loop ; C = 0 jump ; C = 1 no jump mov.b R4,0200h END START alias alias R4 = 0xF0 R4 = R4 +1 SUM > 255? NO YES M(0x0200) = R4 END ECE 3430 – Intro to Microcomputer Systems Fall 2013
Conditional Jumps Carry Flag Jumps How many times did this loop execute? Loop1 R4=0xF1 Loop2 R4=0xF2 Loop3 R4=0xF3 : : Loop15 R4=0xFF Loop16 R4=0x00, C=1, No JumpANSWER = 16 times START R4 = 0xF0 R4 = R4 +1 SUM > 255? NO YES M(0x0200) = R4 END ECE 3430 – Intro to Microcomputer Systems Fall 2013
Conditional Jumps Carry Flag Jumps – Add M(0x0200) and M(0x0201), 8-bit - If the result is =< 255, set P1.5 (clear all other bits in port 1) and END - If the result is > 255, set P1.4 (clear all other bits in port 1) and END ORG 0xF800MAIN: mov.b 0200h,R4 add.b 0201h,R4 jc Error ; C = 1 = branch ; C = 0 = no branch bis.b #00100000b,&P1OUT jmp FinishError: bis.b #00010000b,&P1OUTFinish: jmp Finish START M(0x0200) + M(0x0201) SUM > 255? YES P1.4 = 1 NO P1.5 = 1 END ECE 3430 – Intro to Microcomputer Systems Fall 2013
Conditional Jumps Carry Flag Branches – What does the machine code look like? Addr:IC:Operands: F8A44054 095A mov.b 0200h,R4 F8A85054 0957 add.b 0201h,R4 F8AC2C04 jc Error F8AED0F2 0020 0021 bis.b #00100000b,&P1OUT F8B43C03 jmp Finish F8B6D0F2 0010 0021 Error: bis.b #00010000b,&P1OUT F8BC3FFF Finish: jmp Finish ECE 3430 – Intro to Microcomputer Systems Fall 2013
Conditional Jumps How long does it take to execute if the branch IS taken? How long does it take to execute if the branch IS NOT taken? What is the average execution time? ECE 3430 – Intro to Microcomputer Systems Fall 2013
Conditional Jumps Zero Flag Branches jeq/jz = jump if equal to zero, Z=1jne/jnz = jump if NOT equal to zero, Z=0 Ex) loop 10 times ORG 0xF800 mov.b #10,R4Loop: dec.b R4 jne Loop ; Z = 0 = jump ; Z = 1 = no jump END START count = 10 count = count - 1 count = 0? NO YES END ECE 3430 – Intro to Microcomputer Systems Fall 2013
Conditional Jumps Zero Flag Branches How many loops? Loop1 A=9 Loop2 A=8 : Loop10 A=0 (Z=1, jump NOT taken)How long to execute? Loop dec.b R4 2 cycles jne 2 cycles (4 cycles)(10 loops) = 40 cycles + mov.b #10,R4 2 cycles TOTAL = (42 cycles)(<period of MCLK>) = ?? START count = 10 count = count - 1 count = 0? NO YES END ECE 3430 – Intro to Microcomputer Systems Fall 2013
Conditional Jumps Negative Flag Branches jn = jump if negative, N=1 (jump if minus) Can be used during subtraction to indicate the value was negative. Ex) M(0x0020) – M(0x0021) if negative or zero, END if positive, set P1.4 (clear all other bits on port 1) ORG 0xF800 mov.b 0200h,R4 sub.b 0201h,R4 jn DONE mov.b #00010000b,&P1OUT DONE: jmp $ ; $ is a quick way to jump back to same line END ECE 3430 – Intro to Microcomputer Systems Fall 2013
Conditional Jumps Two’s Complement Overflow Flag Jumpsjge = jump if greater than or equal to ; N V = 0jl = jump if less than ; N V = 1 None that test only the V flag! Notice that a logic equation is evaluated for correctness! ECE 3430 – Intro to Microcomputer Systems Fall 2013
Signed vs. Unsigned Jump Instructions (Summary) Unsigned branch instructions: Equation: jhi = jump if higher than ---- jhs = jump if higher than or the same same as jc C ?= 1 jlo = jump if lower than same as jnc C ?= 0 jls = jump if lower than or the same ---- Signed branch instructions: jge = jump if greater than or equal (*) N xor V ?= 0 jgt = jump if greater than ---- jle = jump if less than or equal ---- jl = jump if less than (*) N xor V ?= 1 jn = jump if negative flag is set (jump if minus) N ?= 1 Neither: jnc = jump if carry flag is clear same as jlo C ?= 0 jc = jump if carry flag is set same as jhs C ?= 1 jeq/jz = jump if equal (zero flag is set) Z ?= 1 jne/jnz = jump if not equal (zero flag is clear) Z ?= 0 jmp = jump always (unconditional jump) ---- = DOES NOT EXIST = UNCONDITIONAL * = checks N and V flags ECE 3430 – Intro to Microcomputer Systems Fall 2013
Conditional Jumps Compare Instructions Jump instructions do not modify SR bits. They simply read the current state of the N,V,C,Z flags. At times we want to alter the SR but not change the contents of the registers. By altering the state of the SR bits, we can control what value the jump condition is relative to --rather than always zero. Compare instructions allow us to do this:cmp <src>,<dst> = compare two values (destination not written), <dst> - <src> tst <dst> = test for zero or minus condition Typically, the above compare/test instructions should immediately precede a jump instruction! Ex: Consider ‘a’ and ‘b’ to both be unsigned. The expression “if (a >= b) then <label>” is evaluated: cmp b, a or cmp a, b NOTE: In MSP430, “jls” does jhs <label> jls <label> not exist! ECE 3430 – Intro to Microcomputer Systems Fall 2013
Using Compare Instructions to Influence Jump Instruction Behavior ORG 0xF800 Start: mov.b #120,R4 cmp.b 0200h,R4 jl Set4 Clr4: mov.b &P1OUT,R4 and.b #11101111b,R4 jmp Rejoin Set4: mov.b &P1OUT,R4 bis.b #00010000b,R4 Rejoin: mov.b R4,&P1OUT START R4 = 120 R4 < M(0x0200)? YES P1.4 = 1 NO P1.4 = 0 END What could be done to eliminate the redundant “mov.b &P1OUT,R4” instruction? How many other ways could this code be written? ECE 3430 – Intro to Microcomputer Systems Fall 2013
Using Compare Instructions to Influence Jump Instruction Behavior JGE (signed) takes branch if “N V = 0” evaluates as true. JHS (unsigned) takes branch if “C = 1” evaluates as true. Assume M(0x0200) = 128 N Z V C 1 0 0 0 mov.b 0200h,R4 1 0 0 0 cmp.b #120,R4 0 0 1 1 jge Set4 0 1 = 0 1 = 0 no jump jhs Set4 1 = 1 1 = 1 jump 128-120 = negative – positive = positive overflow ECE 3430 – Intro to Microcomputer Systems Fall 2013
Using Compare Instructions to Influence Jump Instruction Behavior JGE (signed) takes branch if “N V = 0” evaluates as true. JHS (unsigned) takes branch if “C = 1” evaluates as true. Assume M(0x0200) = 100 N Z V C 0 0 0 0 mov.b 0200h,R4 0 0 0 0 cmp.b #120,R4 1 0 0 0 jge Set4 1 0 = 0 1 = 0 no jump jhs Set4 0 = 1 0 = 1 no jump 100-120 = positive – positive = negative Subtracted larger number from smaller (borrow). Result is negative. Negative, no overflow, carry = 0 (due to borrow) ECE 3430 – Intro to Microcomputer Systems Fall 2013