Professional Documents
Culture Documents
Branching
Vassar College
2
Vassar College
Jumps
Instruction Synonym Jump condition Description
jmp Label 1 Direct jump
je Label jz ZF Equal / zero
jne Label jnz ~ZF Not Equal / not zero
js Label SF Negative
jns Label ~SF Nonnegative
jg Label jnle ~(SF ^ OF) & ~ZF Greater (signed > )
jge Label jnl ~(SF ^ OF) Greater or equal (signed >=)
jl Label jnge SF ^ OF Less (signed <)
jle Label jng (SF ^ OF) | ZF Less or equal (signed <=)
ja Label jnbe ~CF & ~ZF Above (unsigned >)
jae Label jnb ~CF Above or equal (unsigned >=)
jb Label jnae CF Below (unsigned <)
jbe Label jna CF | ZF Below or equal (unsigned <=)
4
Vassar College
5
Vassar College
Branch Example
• Generation
linux> gcc –Og -S –fno-if-conversion control.c
bonus:
cmpq $100, %rdi
jg .L4
call add_bonus
ret
.L4: Register Use(s)
movl $100, %eax
%rdi Argument x
ret
%rax Return value
7
Vassar College
Conditional Example
long conditional(long x, long y, long z)
{
return x ? y : z;
}
Register Use(s)
conditional_with_jmp:
movq %rdx, %rax # z to %rax %rdi Argument x
testq %rdi, %rdi # CC set on x %rsi Argument y
je .L2 # jump if x == 0 %rdx Argument z
movq %rsi, %rax # return y
.L2: %rax Return value
ret
8
Vassar College
•
Translation (Extended C) •
goto *JTab[x]; •
11
Vassar College
12
Vassar College
Jump Table
Jump table
switch(x) {
.section .rodata case 1: // .L3
.align 8 w = y*z;
.L4: break;
.quad .L8 # x = 0
.quad .L3 # x = 1 case 2: // .L5
.quad .L5 # x = 2 w = yz;
.quad .L9 # x = 3 /* Fall Through */
.quad .L8 # x = 4
.quad .L7 # x = 5
case 3: // .L9
.quad .L7 # x = 6 w += z;
break;
case 5:
case 6: // .L7
w -= z;
• .L8 handles the default case break;
default: // .L8
w = 2;
}
13
Vassar College
Code Blocks (x == 1)
switch(x) { .L3:
case 1: // .L3 movq %rsi, %rax # y
w = y*z; imulq %rdx, %rax # y*z
break; ret
. . .
}
Register Use(s)
%rdi Argument x
%rsi Argument y
%rdx Argument z
%rax Return value
14
Vassar College
Handling Fall-Through
long w = 1;
. . .
switch(x) { case 2:
. . . w = y-z;
case 2: goto merge;
w = y-z;
/* Fall Through */
case 3:
w += z;
break;
. . .
case 3:
}
w = 1;
merge:
w += z;
15
Vassar College
Code Blocks (x == 2, x == 3)
.L5: # Case 2
long w = 1; movq %rsi, %rax # y
. . . subq %rdx, %rax # y-z
switch(x) { jmp .L6 # goto merge
. . . .L9: # Case 3
case 2: movl $1, %eax # w = 1
w = y-z; .L6: # merge:
/* Fall Through */ addq %rdx, %rax # w += z
case 3: ret
w += z;
break;
. . .
Register Use(s)
}
%rdi Argument x
%rsi Argument y
%rdx Argument z
%rax Return value
16
Vassar College
Register Use(s)
%rdi Argument x
%rsi Argument y
%rdx Argument z
%rax Return value
17
Conditional Moves
Instruction Synonym Move Condition Description
• Destination must be
cmove S,R cmovz ZF Equal / zero
a register
cmovne S,R cmovnz ~ZF Not equal / not zero
cmovs S,R SF Negative • Source can be
register or memory
cmovns S,R ~SF Nonnegative
location
cmovg S,R cmovnle ~(SF ^ OF) & ~ZF Greater (signed >)
cmovge S,R cmovnl ~(SF ^ OF) Greater or equal (signed >=) • Length is determined
by the name of the
cmovl S,R cmovnge SF ^ OF Less (signed <) destination register
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 <=)
18
Using Conditional Moves
• Conditional Move Instructions
• Instruction supports: C Code
if (Test) Dest Src val = Test ? Then_Expr : Else_Expr;
• Supported in post-1995 x86 processors
• GCC tries to use them
• But only when known to be safe Conditional Move Pseudo Code
val = Else_Expr;
eval = Then_Expr;
if (Test) val = eval;
19
Vassar College
Conditional Example
long conditional(long x, long y, long z)
{
return x ? y : z;
}
conditional_with_jmp: conditional_with_cmove:
movq %rdx, %rax # z to %rax testq %rdi, %rdi # CC set on x
testq %rdi, %rdi # CC set on x movq %rdx, %rax # z to %rax
je .L2 # jump if x == 0 cmovne %rsi, %rax # y to %rax if x!=0
movq %rsi, %rax # return y ret
.L2:
ret Register Use(s)
%rdi Argument x
%rsi Argument y
%rdx Argument z
%rax Return value
20
Vassar College
21
Vassar College
Risky Computations
val = p ? *p : 0;
22
Summary
• jXX family of instructions are a conditional goto
• Changes the flow of instructions
• If statements
• If !(test) to jump to else branch
• Reorder code so else comes first and if (test) to jump to then branch
• Conditional moves
• Switch statements
• Often implemented as a jump table to handle cases
• Have to account for default and fall-through cases
23