You are on page 1of 43

Ithaca College

Carnegie Mellon

Machine-Level Programming IV Control


Comp 21000: Introduction to Computer Organization & Systems
March 2016 Systems book chapter 3*

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 1


Ithaca College

Today
 Control: Condition codes
 Conditional branches

 Loops

 Switch Statements

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2


Ithaca College

Processor State (x86-64, Partial)


 Information about
currently executing Registers
program %rax %r8
▪ Temporary data %rbx %r9
( %rax, … ) %rcx %r10
▪ Location of runtime stack %rdx %r11
( %rsp ) %rsi %r12
▪ Location of current code %rdi %r13
control point %rsp %r14
( %rip, … )
%rbp %r15
▪ Status of recent tests
( CF, ZF, SF, OF )
%rip Instruction pointer
Current stack top
CF ZF SF OF Condition codes
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 3
Ithaca College

Condition Codes (Implicit Setting)


 Single bit registers
▪CF Carry Flag (for unsigned) SF Sign Flag (for signed)
▪ZF Zero Flag OF Overflow Flag (for signed)

 Implicitly set (think of it as side effect) by arithmetic operations


Example: addq Src,Dest ↔ t = a+b
CF set if carry out from most significant bit (unsigned overflow)
ZF set if t == 0
SF set if t < 0 (as signed)
OF set if two’s-complement (signed) overflow
(a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)

 Not set by leaq instruction


Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 4
Condition Codes
 Examples: assume %rax contains 20
addl $10, %rax CF  0
ZF  0
SF  0
OF  0

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

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 5


Condition Codes
 Single Bit Registers
CF Carry Flag SF Sign Flag
ZF Zero Flag OF Overflow Flag
 Also implicitly Set By logical Operations
xorl Src,Dest
C analog: t = a ^ b (a = Src, b = Dest)
▪ CF set to 0
▪ ZF set if t == 0
▪ SF set if t < 0
▪ OF set to 0
▪ For shift operations:
▪ CF set to last bit shifted out
▪ OF set to 0

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 6


Condition Codes (Implicit Setting)
 There are no Boolean types in assembly language.
 There is no concept of Boolean operators in assembly
language.
 So what is there?

▪ 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.

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 7


Ithaca College

Condition Codes (Explicit Setting: Compare)


 Explicit Setting by Compare Instruction
▪cmpq Src2, Src1
▪cmpq b,a like computing a-b without setting destination

▪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.

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 8


Example %rax 0000 0000

cmpl %rax, %rcx %rcx 0000 0010

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

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 9


Example %rax 0000 0000

cmpl %rax, %rcx %rcx 0000 0010

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

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 10


Ithaca College

Condition Codes (Explicit Setting: Test)


 Explicit Setting by Test instruction
▪testq Src2, Src1
▪testq 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 %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

Reading Condition Codes


 SetX Instructions
▪ Set low-order byte of destination to 0 or 1 based on combinations of
condition codes
▪ Does not alter remaining 7 bytes
SetX Condition Description
sete ZF Equal / Zero
setne ~ZF Not Equal / Not Zero
sets SF Negative
setns ~SF Nonnegative
setg ~(SF^OF)&~ZF Greater (Signed)
setge ~(SF^OF) Greater or Equal (Signed)
setl (SF^OF) Less (Signed)
setle (SF^OF)|ZF Less or Equal (Signed)
seta ~CF&~ZF Above (unsigned)
setb CF Below (unsigned)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 12
x86-64 Integer Registers
%rax %al %r8 %r8b

%rbx %bl %r9 %r9b

%rcx %cl %r10 %r10b

%rdx %dl %r11 %r11b

%rsi %sil %r12 %r12b

%rdi %dil %r13 %r13b

%rsp %spl %r14 %r14b

%rbp %bpl %r15 %r15b

▪ Can reference low-order byte


Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 13
Reading condition codes
 Consider:
setl D (SF^OF) Less (Signed <) D  (SF^OF)

 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

cmpq %rax, %rdx


setl %al puts the result in byte register %al

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 14


Reading condition codes (cont)
 Assume a is in %rax and b in %rdx
cmpq %rax, %rdx
setl %al puts the result in byte register %al
setl D ; D  (SF^OF)
 First instruction sets the condition code.
▪ cmpq computes b – a
▪ If b < a then b – a < 0 If there is no overflow, this is indicated by SF
▪ If there is positive overflow (b – a is large), we have b – a < 0 but OF is set
▪ If there is negative overflow (b – a is very small), we have b – a > 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
 Second instruction sets the %al register to 00000000 or 00000001
depending on the value of (SF^OF)

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 15


Reading condition codes (cont)
 Example: assume %rax holds 20 and %rdx holds 50
cmpq %rax, %rdx
50 – 20, SF  0, OF  0
setl %al setl D ; D  (SF^OF)
%al  0 ^ 0 = 00000000

 Example: assume %rax holds 0x8000001 and %rdx holds 20


cmpq %rax, %rdx
20 – (-2147483647) , SF  1, OF  1
setl %al
%al  1 ^ 1 = 00000000 setq gives false;
20 is not less than
-2147483647

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 16


Reading condition codes (cont)
 Example: assume %rax holds 20 and %rdx holds 0x8000001
cmpq %rax, %rdx
(-2147483647) - 20 , SF  0, OF  1
0x80000001 + 0xFFFFFFEC
= 0x7FFFFFED (note that 7 = 0111)
setl %al
%al  0 ^ 1 = 00000001 setq gives true;
-2147483647 is less
than 20

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 17


Setg Greater (Signed)
~(SF^OF)&~ZF

First do an arith, logical, ~SF&~OF If this is zero, can’t be >


cmp or test.
Then use the flags If the overflow flag is set, can’t be > if we did a
cmpl or testl as the previous instruction. Why?

CF ZF SF OF Condition codes

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 18


Ithaca College

Reading Condition Codes (Cont.)


 SetX Instructions:
▪ Set single byte based on combination of condition
codes
 One of addressable byte registers
▪ Does not alter remaining bytes
▪ Typically use movzbl to finish job
▪ 32-bit instructions also set upper 32 bits to 0
Register Use(s)
int gt (long x, long y)
{ %rdi Argument x
return x > y; %rsi Argument y
}
%rax Return value

cmpq %rsi, %rdi # Compare x:y


setg %al # Set when >
movzbl %al, %rax # Zero rest of %rax
ret
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 19
Ithaca College

Reading Condition Codes (Cont.)


Register Use(s)
%rax %ah %al
%rdi Argument x
%rdx %dh %dl
%rsi Argument y All 0’s:
%rax Return value 0000000000000000000000000 %rcx %ch %cl
%rbx %bh %bl
Either
00000000 %rsi
or
int gt (long x, long y) 00000001 %rdi
{
%rsp
return x > y;
} %rbp

cmpq %rsi, %rdi # Compare x:y


setg %al # Set when >
movzbl %al, %rax # Zero rest of %rax
ret
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 20
Ithaca College

Today
 Control: Condition codes
 Conditional branches

 Loops

 Switch Statements

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 21


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!

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 22


Jumping
 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

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 23


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
▪ Names and conditions match the set instructions.
▪ 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

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 24


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)

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 25


jg Greater (Signed)
~(SF^OF)&~ZF

First do an arith, logical, ~SF&~OF If this is zero, can’t be >


cmp or test.
Then use the flags If the overflow flag is set, can’t be > if we did a
cmpq or testq as the previous instruction.
Why?

CF ZF SF OF Condition codes

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 26


Ithaca College

Conditional Branch Example (Old Style)


 Generation
nori> gcc –Og -S –fno-if-conversion control.c
absdiff:
long absdiff cmpq %rsi, %rdi # x:y
(long x, long y) jle .L4
{ movq %rdi, %rax
long result; subq %rsi, %rax
if (x > y) ret
result = x-y; .L4: # x <= y
else movq %rsi, %rax
result = y-x; subq %rdi, %rax
return result; ret
}
Register Use(s)
%rdi Argument x
%rsi Argument y
%rax Return value
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 27
Ithaca College

Expressing with Goto Code


 C allows goto statement
 Jump to position designated by label

long absdiff long absdiff_j


(long x, long y) (long x, long y)
{ {
long result; long result;
if (x > y) int ntest = x <= y;
result = x-y; if (ntest) goto Else;
else result = x-y;
result = y-x; goto Done;
return result; Else:
} result = y-x;
Done:
return result;
Generally considered bad coding style }

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 28


Ithaca College

Expressing with Goto Code


 C allows goto statement
 Jump to position designated by label
absdiff:
long absdiff_j cmpq %rsi, %rdi # x:y
(long x, long y) jle .L4
{ movq %rdi, %rax
long result; subq %rsi, %rax
int ntest = x <= y; ret
if (ntest) goto Else; .L4: # x <= y
result = x-y; movq %rsi, %rax
goto Done; subq %rdi, %rax
Else: ret
result = y-x;
Done: Register Use(s)
return result;
%rdi Argument x
}
%rsi Argument y
%rax Return value
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 29
Ithaca College

Expressing with Goto Code


 C allows goto statement
 Jump to position designated by label
absdiff:
long absdiff_j cmpq %rsi, %rdi # x:y
(long x, long y) jle .L4
{ movq %rdi, %rax
long result; subq %rsi, %rax
int ntest = x <= y; ret
if (ntest) goto Else; .L4: # x <= y
result = x-y; movq %rsi, %rax
goto Done; subq %rdi, %rax
Else: ret
result = y-x;
Done: Register Use(s)
return result;
%rdi Argument x
}
%rsi Argument y
%rax Return value
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 30
Ithaca College

Expressing with Goto Code


 C allows goto statement
 Jump to position designated by label
absdiff:
long absdiff_j cmpq %rsi, %rdi # x:y
(long x, long y) jle .L4
{ movq %rdi, %rax
long result; subq %rsi, %rax
int ntest = x <= y; ret
if (ntest) goto Else; .L4: # x <= y
result = x-y; movq %rsi, %rax
goto Done; subq %rdi, %rax
Else: ret
result = y-x;
Done: Register Use(s)
return result;
%rdi Argument x
}
%rsi Argument y
%rax Return value
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 31
Ithaca College

Expressing with Goto Code


 C allows goto statement
 Jump to position designated by label
absdiff:
long absdiff_j cmpq %rsi, %rdi # x:y
(long x, long y) jle .L4
{ movq %rdi, %rax
long result; subq %rsi, %rax
int ntest = x <= y; ret
if (ntest) goto Else; .L4: # x <= y
result = x-y; movq %rsi, %rax
goto Done; subq %rdi, %rax
Else: ret
result = y-x;
Done: Register Use(s)
return result;
%rdi Argument x
}
%rsi Argument y
%rax Return value
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 32
Example 2
 Download ifStmt.c
 gcc –O0 –o ifStmt –fno-if-conversion ifStmt.c
 gdb ifStmt
 In gdb: disass testIf1

int testIf1(int x, int y)


{
int result;
if (x < y)
result = x * 4;
else
result = y * 4;
return result;
}
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 33
Register Use(s)

Example 2 continued… %rdi Argument x


%rsi Argument y
pushq %rbp
movq %rsp, %rbp Setting up the %rax Return value
movq %rdi, -8(%rbp) runtime stack
movq %rsi, -16(%rbp)
movq -8(%rbp), %rsi
if x - y >= 0
cmpq -16(%rbp), %rsi
jump int testIf1(int x, int y)
jge LBB0_2 {
movq -8(%rbp), %rax else calculate int result;
shlq $2, %rax x=x*4 if (x < y)
movq %rax, -24(%rbp) and jump result = x * 4;
jmp LBB0_3 else
LBB0_2: result = y * 4;
movq -16(%rbp), %rax Calculate y=y*4 return result;
shlq $2, %rax
}
movq %rax, -24(%rbp)
LBB0_3:
movq -24(%rbp), %rax Set up the return
popq %rbp value
retq
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 34
Ithaca College

General Conditional Expression Translation


(Using Branches)
C Code
val = Test ? Then_Expr : Else_Expr;

val = x>y ? x-y : y-x;

▪ Test Is an expression returning an


Goto Version integer
ntest = !Test; ▪ = 0 interpreted as false
if (ntest) goto Else;
val = Then_Expr; ▪ ≠ 0 interpreted as true
goto Done;
Else: ▪ Create separate code regions for
val = Else_Expr;
then & else expressions
Done:
. . . ▪ Execute appropriate one

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 35


Ithaca College

Using Conditional Moves


 Conditional Move Instructions
▪ Instruction supports:
if (Test) Dest  Src
▪ Supported in post-1995 x86 processors C Code
▪ GCC tries to use them val = Test
▪ But, only when known to be safe ? Then_Expr
: Else_Expr;
 Why?
▪ Branches are very disruptive to
instruction flow through pipelines
Goto Version
▪ Conditional moves do not require result = Then_Expr;
control transfer eval = Else_Expr;
nt = !Test;
if (nt) result = eval;
return result;

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 36


Ithaca College

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

Conditional Move Example


long absdiff
(long x, long y)
{ Register Use(s)
long result;
if (x > y) %rdi Argument x
result = x-y; %rsi Argument y
else
%rax Return value
result = y-x;
return result;
}

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

Bad Cases for Conditional Move


Expensive Computations
val = Test(x) ? Hard1(x) : Hard2(x);

 Both values get computed


 Only makes sense when computations
are very simple In general, gcc only uses conditional moves when the two
expressions can be computed very easily, e.g., single
Risky Computations instructions.
val = p ? *p : 0;

 Both values get computed


 May have undesirable effects (always dereference p but it may be null!)
Computations with side effects
val = x > 0 ? x*=7 : x+=3;

 Both values get computed


 Must be side-effect free
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 39
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

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 40


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 = 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

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 41


Ithaca College

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

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 43

You might also like