Carnegie Mellon Ithaca College Machine-Level Programming IV Control Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3*
Today Control: Condition codes Conditional branches Loops Ithaca College Today Control: Condition codes Conditional branches Loops Switch Statements
Jumping A jump instruction causes execution to jump to a new address Really just puts the argument of the jump into the EIP Jump destinations are usually indicated by a label A label marks a place in code 1 xorq %rax, %rax 2 jmp .L1 3 movq (%rax), %rdx 4 .L1: popq %rdx The instruction jmp .L1 causes program to skip line 3 jmp is an unconditional jump instruction Note that we wouldn’t really use the code this way!
Jumping or A jump instruction has two types of arguments Direct jumps. Target is part of the instruction Use a label Indirect jumps. Target read from a register or memory. Use a ‘*’ followed by an operand specifier jmp *%eax or jmp *(%eax) Any addressing mode can be used Note that jmp is an unconditional jump instruction
Jumping Other jump instructions are conditional Either jump or continue executing at next instruction in the code Depends on some combination of the condition codes Conditional jumps can only be direct Assembler changes labels into actual memory addresses See section 3.6.3 of the book Not very important now; will be very important in chapter 7
Jumping jX Instructions Ithaca College Jumping jX Instructions Jump to different part of code depending on condition codes jX Condition Description jmp 1 Unconditional je ZF Equal / Zero jne ~ZF Not Equal / Not Zero js SF Negative jns ~SF Nonnegative jg ~(SF^OF)&~ZF Greater (Signed) jge ~(SF^OF) Greater or Equal (Signed) jl (SF^OF) Less (Signed) jle (SF^OF)|ZF Less or Equal (Signed) ja ~CF&~ZF Above (unsigned) jb CF Below (unsigned)
Reading condition codes (cont) Assume a is in %rdx and b in %rax cmpq %rax, %rdx jl SUM Goal: jump if a is less than b First instruction sets the condition code. cmpq computes a – b a < b then a – b < 0 Second instruction checks to see if the compq proved that a – b < 0 Checks (SF^OF) Goal: jump if a < b or a – b < 0 jl LABEL ; if (SF^OF) %RIP LABEL
Reading condition codes (cont) Assume a is in %rdx and b in %rax cmpq %rax, %rdx jl SUM How does this work? cmpq computes a – b If a < b then a – b < 0 If TRUE, then a – b will be negative AND there will be no overflow If there is positive overflow (a – b is large), we have a – b < 0 but OF is set If there is negative overflow (a – b is very small), we have a – b > 0 but OF is set In either case the sign flag will indicate the opposite of the sign of the true difference. Hence, use exclusive-or of the OF and SF jl LABEL ; if (SF^OF) %RIP LABEL CF ZF SF OF Condition codes SF ^ OF
jl jump less (Signed) SF^OF cmpq %cl, %al Jl LABEL %cl and %al are 8 bits %al holds 50 %cl holds 20 50 – 20 00110010 + 11101100 = SF = OF = OF ^ SF is 50 < 20 Or 50 – 20 < 0 %al holds 50 %cl holds 70 50 – 70 00110010 + 10111010 = SF = OF = OF ^ SF is 50 < 70 Or 50 – 70 < 0 00011110 11101100 1 1 True False
jl jump less (Signed) SF^OF cmpq %cl, %al Jl LABEL %cl and %al are 8 bits %al holds –50 %cl holds –20 –50 – –20 11001110 + 00010100 = SF = OF = OF ^ SF is –50 < –20 Or 50 – 20 < 0 %al holds –20 %cl holds –50 –20 – –50 11101100 + 00110010 = SF = OF = OF ^ SF is 50 < 70 Or 50 – 70 < 0 11100010 00011110 1 1 False True
jl jump less (Signed) SF^OF cmpq %cl, %al Jl LABEL %cl and %al are 8 bits %al holds 127 %cl holds – 5 127– – 5 = 01111111 + 00000101 = SF = OF = OF ^ SF is 127< – 5 Or 127– – 5 < 0 %al holds – 128 %cl holds + 5 – 128– 5 = 10000000 + 11111011 = SF = OF = OF ^ SF is -128< 5 Or – 128– 5 < 0 10000100 01111011 1 1 1 underflow overflow 1 True False
Condition Codes (Explicit Setting: Test) Explicit Setting by Test instruction testl/testq Src2, Src1 testl b,a like computing a&b without setting destination Sets condition codes based on value of Src1 & Src2 Useful to have one of the operands be a mask CF set to 0 ZF set when a&b == 0 SF set when a&b < 0 OF set to 0 Note: typically the same operand is repeated to test whether it is negative, zero, or positive: testl %eax, %eax sets the condition codes depending on the value in %eax Note 2: there are also testw and testb instructions. Remember that bitwise operations are more efficient than arithmetic