Download presentation
Presentation is loading. Please wait.
1
Carnegie Mellon Ithaca College Machine-Level Programming IV Control Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3*
2
Today Control: Condition codes Conditional branches Loops
Ithaca College Today Control: Condition codes Conditional branches Loops Switch Statements
3
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!
4
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
5
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 of the book Not very important now; will be very important in chapter 7
6
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)
7
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
8
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
9
jl jump less (Signed) SF^OF
cmpq %cl, %al Jl LABEL %cl and %al are 8 bits %al holds 50 %cl holds – = SF = OF = OF ^ SF is 50 < 20 Or 50 – 20 < 0 %al holds 50 %cl holds – = SF = OF = OF ^ SF is 50 < 70 Or 50 – 70 < 0 1 1 True False
10
jl jump less (Signed) SF^OF
cmpq %cl, %al Jl LABEL %cl and %al are 8 bits %al holds –50 %cl holds –20 –50 – – = SF = OF = OF ^ SF is –50 < –20 Or 50 – 20 < 0 %al holds –20 %cl holds –50 –20 – – = SF = OF = OF ^ SF is 50 < 70 Or 50 – 70 < 0 1 1 False True
11
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 = = SF = OF = OF ^ SF is 127< – 5 Or 127– – 5 < 0 %al holds – 128 %cl holds + 5 – 128– 5 = = SF = OF = OF ^ SF is -128< 5 Or – 128– 5 < 0 1 1 1 underflow overflow 1 True False
12
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.