Machine-Level Programming IV Control
Machine-Level Programming IV Control
Carnegie Mellon
Today
Control: Condition codes
Conditional branches
Loops
Switch Statements
subl $50, %rax CF 0 Note that these flags are set just
like we talked about in chapter 2!
ZF 0
SF 1
OF 0
▪ Bits
▪ All we can do is look at bits. What those bits mean is determined by the
context of the program.
▪ Instructions set flags.
▪ Other instructions take action based on the bits in those flags.
▪ Don’t think about it as comparison as in the C language sense.
▪CF set if carry out from most significant bit (used for unsigned comparisons)
▪ZF set if a == b
▪SF set if (a-b) < 0 (as signed)
▪OF set if two’s-complement (signed) overflow
(a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)
Note: notice that the operands are in reverse order; b is the first operand , but
we compute a – b!
Note 2: there are also cmpw and cmpb instructions.
do %rcx - %rax
%rdx 0000 0000
then set condition codes
NO other action taken! %rbx 0000 0000
Condition codes
%rsi
CF ZF SF OF
%rdi
?? ?? ?? ??
%rsp
%rbp
do %rcx - %rax
%rdx 0000 0000
then set condition codes
NO other action taken! %rbx 0000 0000
Condition codes
%rsi
CF ZF SF OF
%rdi
0 0 0 0
%rsp
%rbp
▪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 %rax, %rax sets the condition codes depending on the value in %rax
Note 2: there are also testl, testw and testb instructions.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 11
Ithaca College
First compare two numbers, a and b where both are in 2’s complement
form using an arithmetic, logical, test or cmp instruction
Then use setX to set a register with the result of the test
CF ZF SF OF Condition codes
Today
Control: Condition codes
Conditional branches
Loops
Switch Statements
jmp *%eax
or
jmp *(%eax)
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)
CF ZF SF OF Condition codes
Conditional Move
* source/destination may be 16, 32, or 64 bits (not 8). No size suffix: assembler infers the operand length based on destination register
cmovX Instructions
▪ Jump to different part of code depending on condition codes
jX Synonym Condition Description
cmove S*,R* cmovz ZF Equal / Zero
cmovne S,R cmovnz ~ZF Not Equal / Not Zero
cmovs S,R SF Negative
cmovns S,R ~SF Nonnegative
cmovg S,R cmovnle ~(SF^OF)&~ZF Greater (Signed)
cmovge S,R cmovnl ~(SF^OF) Greater or Equal (Signed)
cmovl S,R cmovnge (SF^OF) Less (Signed)
cmovle S,R cmovng (SF^OF)|ZF Less or Equal (Signed)
cmova S,R cmovnbe ~CF&~ZF Above (unsigned)
cmovae S,R cmovnb ~CF Above or equal (unsigned)
cmovb S,R cmovnae CF Below (unsigned)
cmovbe S,R cmovna CF | ZF Below or equal (unsigned)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 37
Ithaca College
absdiff:
movq %rdi, %rax # x
subq %rsi, %rax # result = x-y
movq %rsi, %rdx
subq %rdi, %rdx # eval = y-x
cmpq %rsi, %rdi # x:y
cmovle %rdx, %rax # if <=, result = eval
ret
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 38
Ithaca College
Register Use(s)
Practice Problem %rdi Argument x
Generation %rsi Argument y
%rax Return value
test:
long test leaq 0(,%rdi,8), %rax
(long x, long y) testq %rsi, %rsi
{ jle .L4
long val = _________; movq %rsi, %rax
if (_________){ subq %rdi, %rax
if (__________){ movq %rdi, %rdx
val = ________; andq %rsi, %rdx
else cmpq %rsi, %rdi
val = ________; cmovge %rdx, %rax
} else if (________) ret
val = ________; .L4: # x <= y
return val; addq %rsi, %rdi
} cmpq %-2, %rsi
cmovle %rdi, %rax
ret
Register Use(s)
Practice Problem %rdi Argument x
Generation %rsi Argument y
%rax Return value
test:
long test leaq 0(,%rdi,8), %rax
(long x, long y) testq %rsi, %rsi
{ jle .L4
long val = 8*x; movq %rsi, %rax
if (y > 0){ subq %rdi, %rax
if (x < y){ movq %rdi, %rdx
val = y - x; andq %rsi, %rdx
else cmpq %rsi, %rdi
val = x & y; cmovge %rdx, %rax
} else if (y <= -2) ret
val = x + y; .L4: # x <= y
return val; addq %rsi, %rdi
} cmpq %-2, %rsi
cmovle %rdi, %rax
ret
Summarizing
C Control
▪ if-then-else
▪ do-while
▪ while, for
▪ switch
Assembler Control
▪ Conditional jump
▪ Conditional move
▪ Indirect jump (via jump tables)
▪ Compiler generates code sequence to implement more complex control
Standard Techniques
▪ Loops converted to do-while or jump-to-middle form
▪ Large switch statements use jump tables
▪ Sparse switch statements may use decision trees (if-elseif-elseif-else)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 42
Ithaca College
Summary
Today
▪ Control: Condition codes
▪ Conditional branches & conditional moves
Next Time
▪ Loops
▪ Switch statements
▪ Stack
▪ Call / return
▪ Procedure call discipline