OSU CSE 2421
Jumping & Conditional instructions
J.E.Jones
OSU CSE 2421
Information about currently executing program
Registers
◦ Temporary data %rax %r8
( %rax, … )
%rbx %r9 %rcx %r10 %rdx %r11 %rsi %r12 %rdi %r13 %rsp %r14 %rbp %r15
◦ Location of runtime stack ( %rsp )
◦ Location of current code control point
( %rip, … )
◦ Status of recent tests ( CF, ZF, SF, OF )
Current stack top
%rip
CF ZF SF OF
Instruction pointer
Condition codes
J. E. Jones
OSU CSE 2421
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!
J. E. Jones
OSU CSE 2421
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
J. E. Jones
OSU CSE 2421
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)
IMPORTANT! Note that OF and CF are not set!
◦ZF set when a&b == 0 ◦SF set when a&b < 0
J. E. Jones
OSU CSE 2421
setX Instructions (Figure 3.14 in Bryant/O’Hallaron)
◦ Setlow-orderbyteofdestination(lowordersingle-byteregisterorasinglebyte
memory location) to 0 or 1 based on combinations of condition codes ◦ Doesnotalterremaining7bytes
SetX Condition sete ZF
setne ~ZF
sets SF
Description
Equal / Zero
Not Equal / Not Zero Negative
Nonnegative
Greater (Signed)
Greater or Equal (Signed) Less (Signed)
Less or Equal (Signed) Above (unsigned)
Below (unsigned)
setns ~SF
setg ~(SF^OF)&~ZF setge ~(SF^OF)
setl (SF^OF)
setle (SF^OF)|ZF
seta ~CF&~ZF
setb CF
◦ Why? So that you can store a condition longer than one ALU instruction
J. E. Jones
OSU CSE 2421
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)
Register Use(s)
int is_gt (long x, long y) {
%rdi Argument x %rsi Argument y %rax Return value
return x > y; }
is_gt:
cmpq %rsi, %rdi setg %al
movzbq %al, %rax ret
# Compare x:y (x-y)
# Set when x>y (i.e., x-y>0) # Zero rest of %rax
J. E. Jones
OSU CSE 2421
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
src1 and src2 must be of the same size
◦ testb, testw, testl, testq
J. E. Jones
OSU CSE 2421
jX Instructions
◦ Jump to different part of code depending on condition codes
jX Condition
Description
jmp 1
je ZF
jne ~ZF
js SF
jns ~SF
jg ~(SF^OF)&~ZF jge ~(SF^OF)
jl (SF^OF)
jle (SF^OF)|ZF
ja ~CF&~ZF
jb CF
Unconditional
Equal / Zero
Not Equal / Not Zero Negative
Nonnegative
Greater (Signed)
Greater or Equal (Signed) Less (Signed)
Less or Equal (Signed) Above (unsigned)
Below (unsigned)
◦ This is only a partial list. A more inclusive one at: http://unixwiz.net/techtips/x86-jumps.html
J. E. Jones
OSU CSE 2421
cmovX Instructions
◦ Move a value (or not) depending on condition codes
cmovX Condition
Description
cmove ZF
cmovne ~ZF
cmovs SF
cmovns ~SF
cmovg ~(SF^OF)&~ZF cmovge ~(SF^OF) cmovl (SF^OF) cmovle (SF^OF)|ZF cmova ~CF&~ZF cmovb CF
Equal / Zero
Not Equal / Not Zero Negative
Nonnegative
Greater (Signed)
Greater or Equal (Signed) Less (Signed)
Less or Equal (Signed) Above (unsigned)
Below (unsigned)
◦ This is only a partial list. A move inclusive one at: https://www.felixcloutier.com/x86/cmovcc
J. E. Jones
OSU CSE 2421
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);
}
J. E. Jones
OSU CSE 2421
.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 .type
#required directive for every function main, @function #required directive
J. E. Jones
OSU CSE 2421
.text
main:
pushq %rbp
movq %rsp, %rbp movq $PR_1, %rdi
#required directive
movq $0, %rax call printf
movq $x, %rsi movq $SC_1, %rdi movq $0, %rax call scanf
#stack housekeeping #1
#stack housekeeping #2
#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
# C library ABI says %rax should be zero b4 call to printf
addq $5, x
movq x, %rsi movq $PR_2, %rdi movq $0, %rax call printf
movq $0, %rax leave
ret
.size main, .-main
#add the constant 5 to what is stored in variable x #value of x to %rsi (2nd parameter)
#address of string “The value of…” to %rdi (1st param) # keep ABI happy
#mov the address of x to %rsi (2nd parameter) #address of string “%li” in %rdi (1st parameter) # to keep ABI happy
#set return value to 0 #required directive
J. E. Jones
OSU CSE 2421
We’ll look at more coding examples that deal with jumps, compare, test, set and conditional moves next class.
J. E. Jones
OSU CSE 2421
# target all means all FINAL targets currently defined in this file
# all:
# 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:
lab6:
# now you must have a target for each .o file listed above
# in this class, .c files would use –ansi –pedantic –c, use –g if you want to debug it>
# .s files would use –lc –m64 –c, use –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