CS2421 Autumn 2013
CSE 2421
X86-64Assembly Language – Part 5: instructions for Control-Flow: Looping & Conditional instructions
Processor State (x86-64, Partial)
Information about currently executing program
Temporary data
( %rax, … )
Location of top of runtime stack
( %rsp )
Location of current code control point
( %rip, … )
Status of recent tests
( CF, ZF, SF, OF )
%rip
Registers
Current stack top
Instruction pointer
CF
ZF
SF
OF
Condition codes
%rsp
%r8
%r9
%r10
%r11
%r12
%r13
%r14
%r15
%rax
%rbx
%rcx
%rdx
%rsi
%rdi
%rbp
Condition Codes (Implicitly Set)
Single bits
CF Carry Flag (for unsigned) SF Sign Flag (for signed)
ZF Zero Flag OF Overflow Flag (for signed)
Implicitly set (think of it as a side effect) of arithmetic operations (more on logical operations later)
Example: addq Src, Dest ↔ b = a + b
Let t be the result of a + b, then
CF set if carry-out from most significant bit (unsigned overflow) is 1; else CF is 0.
ZF set if t == 0; else ZF is 0.
SF set if t < 0 (as signed); else SF is 0.
OF set if two’s-complement (signed) overflow:
msb carry-in != msb carry-out; else OF is 0.
Not implicitly set/cleared by leaq instruction
IMPORTANT!
Condition Codes (Explicitly Set: Compare)
Explicitly set flags with Compare Instruction, cmpX
cmpq Src2, Src1
cmpq b,a similar to computing a-b without assigning result to destination
CF set if carry out from most significant bit (used for unsigned comparisons) is 1
ZF set if a == b
SF set if (a-b) < 0 (as signed)
OF set if two’s-complement (signed) overflow
msb carry-in != msb carry-out
Condition Codes (Explicitly Set: Test)
Explicitly set flags with Test Instruction, testX
testq Src2, Src1
testq b, a similar to computing a&b without assigning result to destination
Useful for:
repeating the operand to determine if value is negative, zero or positive
(e.g. testq %rax %rax)
to have one of the operands be a mask to test individual bits
(e.g. testq %rax, 0x0100)
ZF set when a&b == 0
SF set when a&b < 0
IMPORTANT!
Note that OF and CF are not set!
Reading Condition Codes
setX Instructions (Figure 3.14 in Bryant/O’Hallaron)
Set low-order byte of destination(low order single-byte register or a single byte memory location) to 0 or 1 based on combinations of condition codes
Does not alter remaining 7 bytes
Why? So that you can store a condition longer than one instruction
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)
6
is_gt:
cmpq %rsi, %rdi # Compare x:y (x-y)
setg %al # Set when x>y (i.e. x-y>0)
movzbq %al, %rax # Zero rest of %rax
ret
Reading Condition Codes (Cont.)
setX Instructions:
Set single byte based on combination of condition codes
Store current 1-bit value in one of the 1-byte registers
Does not alter remaining bytes
Typically use movzbq to finish job
(Figure 3.5 & last 4 paragraphs of 3.4.2)
int is_gt (long x, long y)
{
return x > y;
}
Register Use(s)
%rdi Argument x
%rsi Argument y
%rax Return value
In a nutshell
Arithmetic
cmpX
Computes
Condition codes (OF,CF,ZF,SF) are set based on the computation
src1 and src2 must be of the same size
cmpb, cmpw, cmpl or cmpq
Logical
testX
Computes
Condition codes (ZF and SF) are set based on the computation
src1 and src2 must be of the same size
testb, testw, testl, testq
Jumping
jX Instructions
Jump to different part of code depending on condition codes
This is only a partial list
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)
Conditional Moves
cmovX Instructions
Move a value (or not) depending on condition codes
This is only a partial list
cmovX Condition Description
cmove ZF Equal / Zero
cmovne ~ZF Not Equal / Not Zero
cmovs SF Negative
cmovns ~SF Nonnegative
cmovg ~(SF^OF)&~ZF Greater (Signed)
cmovge ~(SF^OF) Greater or Equal (Signed)
cmovl (SF^OF) Less (Signed)
cmovle (SF^OF)|ZF Less or Equal (Signed)
cmova ~CF&~ZF Above (unsigned)
cmovb CF Below (unsigned)
Simple C program
The simple C program below will be translated to assembly language in the following slides:
#include
long x; /* file scope variable – stored on the heap */
int main () {
printf(“Please enter an integer on the next line, followed by enter:\n“);
scanf(“%li”, &x); /* Get a value from the user */
x = x + 5; /* add 5 to the input value */
printf(“The value of x after adding 5 is: %ld\n”, x);
return(0);
}
x86-64 program
.file “scanPrint.s“ #optional directive
.section .rodata #required directives for rodata
PR_1:
.string “Please enter an integer on the next line, followed by enter:\n”
SC_1:
.string “%li”
PR_1:
.string “The value of x after adding 5 is: %ld\n”
.data #required for file scope data: read-write program data #of static storage class
x:
.quad 0
.globl main #required directive for every function
.type main, @function #required directive
.text #required directive
main:
pushq %rbp #stack housekeeping #1
movq %rsp, %rbp #stack housekeeping #2
movq $PR_1, %rdi #address of string “Please enter…:\n“ to %rdi
# %rdi is location of 1st parameter
# not pushing any caller saved registers because
# there is no valuable data there
movq $0, %rax # C library ABI says %rax should be zero b4 call to printf
call printf
movq $x, %rsi #mov the address of x to %rsi (2nd parameter)
movq $SC_1, %rdi #address of string “%li” in %rdi (1st parameter)
movq $0, %rax # to keep ABI happy
call scanf
addq $5, x #add the constant 5 to what is stored in variable x
movq x, %rsi #value of x to %rsi (2nd parameter)
movq $PR_2, %rdi #address of string “The value of…” to %rdi (1st param)
movq $0, %rax # keep ABI happy
call printf
movq $0, %rax #set return value to 0
leave
ret
.size main, .-main #required directive
More coding examples
We’ll look at more coding examples that deal with jumps, compare, test, set and conditional moves tomorrow.
/docProps/thumbnail.jpeg