ECE 3430 – Intro to Microcomputer Systems ECE 3430 – Introduction to Microcomputer Systems University 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 Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014
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 Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014
ECE 3430 – Intro to Microcomputer Systems 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. Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014
ECE 3430 – Intro to Microcomputer Systems 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 0xC000 mov.b #0xF0,R4 Loop: add.b #1,R4 jnc Loop ; C = 0 jump ; C = 1 no jump mov.b R4,0x0200 END START alias alias R4 = 0xF0 R4 = R4 +1 SUM > 255? NO YES M(0x0200) = R4 END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014
ECE 3430 – Intro to Microcomputer Systems 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 Jump ANSWER = 16 times START R4 = 0xF0 R4 = R4 +1 SUM > 255? NO YES M(0x0200) = R4 END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014
ECE 3430 – Intro to Microcomputer Systems 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 0xC000 MAIN: mov.b 0x0200, R4 add.b 0x0201, R4 jc Error ; C = 1 = branch ; C = 0 = no branch bis.b #00100000b,&P1OUT jmp Finish Error: bis.b #00010000b,&P1OUT Finish: jmp Finish START M(0x0200) + M(0x0201) SUM > 255? YES P1.4 = 1 NO P1.5 = 1 END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014
ECE 3430 – Intro to Microcomputer Systems Conditional Jumps Carry Flag Branches – What does the machine code look like? Addr: IC: Operands: C000 4054 41FE mov.b 0x0200,R4 C004 5054 41FB add.b 0x0201,R4 C008 2C04 jc Error C00A D0F2 0020 0021 bis.b #00100000b,&P1OUT C00E 3C03 jmp Finish C010 D0F2 0010 0021 Error: bis.b #00010000b,&P1OUT C014 3FFF Finish: jmp Finish Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014
ECE 3430 – Intro to Microcomputer Systems 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 What is the average execution time? -> In the previous example, the time is the same whether the branch is taken or not. Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014
ECE 3430 – Intro to Microcomputer Systems Conditional Jumps Zero Flag Branches jeq/jz = jump if equal to zero, Z=1 jne/jnz = jump if NOT equal to zero, Z=0 Ex) loop 10 times ORG 0xC000 mov.b #10,R4 Loop: dec.b R4 jne Loop ; Z = 0 = jump ; Z = 1 = no jump END START count = 10 count = count - 1 count = 0? NO YES END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014
ECE 3430 – Intro to Microcomputer Systems 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>) = <execution time> START count = 10 count = count - 1 count = 0? NO YES END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014
ECE 3430 – Intro to Microcomputer Systems 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 0xC000 mov.b 0x0200, R4 sub.b 0x0201, R4 jn DONE mov.b #00010000b, &P1OUT DONE: jmp $ ; $ is a quick way to jump back to same line (trap PC) END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014
ECE 3430 – Intro to Microcomputer Systems Conditional Jumps Two’s Complement Overflow Flag Jumps jge = jump if greater than or equal to ; N V = 0 jl = jump if less than ; N V = 1 None that test only the V flag! Notice that a logic equation is evaluated for correctness! Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014
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 Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014
ECE 3430 – Intro to Microcomputer Systems 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! Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014
Using Compare Instructions to Influence Jump Instruction Behavior ORG 0xC000 Start: mov.b #120,R4 cmp.b 0x0200,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 What could be done to eliminate the redundant “mov.b &P1OUT,R4” instruction? How many other ways could this code be written? END Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014
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 0x0200,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, V = 1). Subtracted smaller number from larger (no borrow, C = 1). Result not negative (N = 0). Two values were not the same (Z = 0). Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014
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 0x0200,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 (no overflow, V = 0). Subtracted larger number from smaller (borrow, C = 0). Result is negative (N = 1). Two values were not the same (Z = 0). Lecture #10 ECE 3430 – Intro to Microcomputer Systems Fall 2014