Microsoft PowerPoint – 24_X86_Assembly_Language_Part5
O
SU
C
SE
2
42
1
J.E.Jones
Jumping & Conditional
instructions
O
SU
C
SE
2
42
1
J. E. Jones
Information about currently
executing program
◦ Temporary data
( %rax, … )
◦ Location 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
O
SU
C
SE
2
42
1
J. E. Jones
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
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
ZF set if t == 0
SF set if t < 0 (as signed)
OF set if two’s-complement (signed) overflow
msb carry-in != msb carry-out
Not implicitly set by leaq instruction
IMPORTANT!
O
SU
C
SE
2
42
1
J. E. Jones
Explicitly set flags with Compare Instruction: cmpX
◦ cmpq Src2, Src1
◦ cmpq b, a
◦ the same as computing a-b but not assigning the result to a
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
Note that cmpX can
set all 4 flags we have
interest in
O
SU
C
SE
2
42
1
J. E. Jones
Explicitly set flags with Test Instruction: testX
◦testq Src2, Src1
◦testq b, a
◦the same as computing a&b, but not assigning the result to a
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!
O
SU
C
SE
2
42
1
J. E. Jones
setX Instructions (Figure 3.14 in Bryant/O’Hallaron)
◦ Where X indicates a condition to be met rather than data type size
◦ 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 ALU 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)
O
SU
C
SE
2
42
1
J. E. Jones
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
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
O
SU
C
SE
2
42
1
J. E. Jones
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
O
SU
C
SE
2
42
1
J. E. Jones
jX Instructions
◦ Jump to different part of code within a function depending on condition code specified by
X
◦ This is only a partial list. A more inclusive one at:
http://unixwiz.net/techtips/x86-jumps.html
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)
O
SU
C
SE
2
42
1
J. E. Jones
cmovX Instructions
◦ Move a value (or not) depending on condition codes
◦ These instructions can help you avoid branches in your code
◦ This is only a partial list. A move inclusive one at:
https://www.felixcloutier.com/x86/cmovcc
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)
O
SU
C
SE
2
42
1
J. E. Jones
.globl sum
.type sum, @function
sum:
pushq %rbp #save caller’s rbp
movq %rsp, %rbp #set function’s frame pointer
# register %rdi contains count (1st parameter)
# register %rsi contains address to array (2nd parameter)
movq $0, %rax # initialize sum to 0, by putting 0 in %rax,
# it’s where return value
# needs to be when we return
movq $0, %r8
loop: # loop to sum values in array
decq %rdi # decrement number of remaining elements by 1
jl exit # jump out of loop if no elements remaining
addq (%rsi,%rdi,8), %rax # add element to sum
jmp loop # jump to top of loop
exit: # sum already in register %rax so ready to return
leave
ret #return to caller’s code at return address
.size sum, .-sum
O
SU
C
SE
2
42
1
J. E. Jones
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);
}
O
SU
C
SE
2
42
1
J. E. Jones
.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_2:
.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
O
SU
C
SE
2
42
1
J. E. Jones
.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
O
SU
C
SE
2
42
1
J. E. Jones
Let’s look at another coding example
that deals with jumps, compare, and
conditional moves.
O
SU
C
SE
2
42
1
J. E. Jones
gcc_Copt = -ansi -pedantic -Wimplicit-function-declaration -Wreturn-type –c -g
gcc_Sopt = –lc –m64 –c -g
# target all means all FINAL targets currently defined in this file
# all:
all: lab6.zip bit_encode
# you must have a subsequent target for each file listed above on the right side of the colon
# that would be lab6.zip and lab6 here
lab6.zip:
bit_encode:
# now you must have a target for each .o file listed above
—1 tab —– gcc
# in this class, .c files would use $(gcc_Copt), add –g if you want to debug it>
# .s files would use $(gcc_Sopt), add –g if you want to debug it>
# if you wish to override the default .o file the output goes to, then you can use –o option
# but you don’t have to, you can use the default .o
# this target deletes all files produced from the Makefile
# so that a completely new compile of all items is required
clean:
—1 tab —– rm -rf