CSE351, Spring 2020 L09: x86 Programming II x86-64 Programming II CSE 351 Spring 2020 Instructor: Ruth Anderson Teaching Assistants: Alex Olshanskyy Rehaan Bhimani Callum Walker Chin Yeoh Diya Joy Eric Fan Edan Sneh Jonathan Chen Jeffery Tian Millicent Li Melissa Birchfield Porter Jones Joseph Schafer Connie Wang Eddy (Tianyi) Zhou http://xkcd.com/409/
24
Embed
x86-64 Programming II · 2020. 4. 17. · L09: x86 Programming II CSE351, Spring 2020 Administrivia hw8 due Monday –11am Lab 1b due Monday (4/20) Submit bits.cand lab1Breflect.txt
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Jump to somewhere else if some condition is true, otherwise execute next instruction
Unconditional branch/jump
Always jump when you get to this instruction
Together, they can implement most control flow constructs in high-level languages: if (condition) then {…} else {…}
while (condition) {…}
do {…} while (condition)
for (initialization; condition; iterative) {…}
switch {…}11
CSE351, Spring 2020L09: x86 Programming II
x86 Control Flow
Condition codes
Conditional and unconditional branches
Loops
Switches
12
CSE351, Spring 2020L09: x86 Programming II
Processor State (x86-64, partial)
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 )
• Single bit registers:13
%rip
current top of the Stack
Program Counter(instruction pointer)
CF ZF SF OF Condition Codes
Registers
%rsp
%r8
%r9
%r10
%r11
%r12
%r13
%r14
%r15
%rax
%rbx
%rcx
%rdx
%rsi
%rdi
%rbp
CSE351, Spring 2020L09: x86 Programming II
Condition Codes (Implicit Setting)
Implicitly set by arithmetic operations
(think of it as side effects)
Example: addq src, dst ↔ r = d+s
CF=1 if carry out from MSB (unsigned overflow)
ZF=1 if r==0
SF=1 if r<0 (if MSB is 1)
OF=1 if signed overflow(s>0 && d>0 && r<0)||(s<0 && d<0 && r>=0)
14
Not set by lea instruction (beware!)
CF ZF SF OFCarry Flag Zero Flag Sign Flag Overflow Flag
CSE351, Spring 2020L09: x86 Programming II
Condition Codes (Explicit Setting: Compare)
Explicitly set by Compare instruction cmpq src1, src2
cmpq a, b sets flags based on b-a, but doesn’t store
CF=1 if carry out from MSB (good for unsigned comparison)
ZF=1 if a==b
SF=1 if (b-a)<0 (if MSB is 1)
OF=1 if signed overflow(a>0 && b<0 && (b-a)>0) ||
(a<0 && b>0 && (b-a)<0)
15
CF ZF SF OFCarry Flag Zero Flag Sign Flag Overflow Flag
CSE351, Spring 2020L09: x86 Programming II
Condition Codes (Explicit Setting: Test)
Explicitly set by Test instruction testq src2, src1
testq a, b sets flags based on a&b, but doesn’t store
• Useful to have one of the operands be a mask
Can’t have carry out (CF) or overflow (OF)
ZF=1 if a&b==0
SF=1 if a&b<0 (signed)
16
CF ZF SF OFCarry Flag Zero Flag Sign Flag Overflow Flag
CSE351, Spring 2020L09: x86 Programming II
Using Condition Codes: Jumping
j* Instructions
Jumps to target (an address) based on condition codes
17
Instruction Condition Description
jmp target 1 Unconditional
je target ZF Equal / Zero
jne target ~ZF Not Equal / Not Zero
js target SF Negative
jns target ~SF Nonnegative
jg target ~( SF OF) &~ZF Greater (Signed)
jge target ~( SF OF) Greater or Equal (Signed)
jl target ( SF OF) Less (Signed)
jle target ( SF OF) |ZF Less or Equal (Signed)
ja target ~CF &~ZF Above (unsigned “ >” )
jb target CF Below (unsigned “ <“ )
CSE351, Spring 2020L09: x86 Programming II
Using Condition Codes: Setting
set* Instructions
Set low-order byte of dst to 0 or 1 based on condition codes
Does not alter remaining 7 bytes
18
Instruction Condition Description
sete dst ZF Equal / Zero
setne dst ~ZF Not Equal / Not Zero
sets dst SF Negative
setns dst ~SF Nonnegative
setg dst ~( SF OF) &~ZF Greater (Signed)
setge dst ~( SF OF) Greater or Equal (Signed)
setl dst ( SF OF) Less (Signed)
setle dst ( SF OF) |ZF Less or Equal (Signed)
seta dst ~CF &~ZF Above (unsigned “ >” )
setb dst CF Below (unsigned “ <” )
CSE351, Spring 2020L09: x86 Programming II
Reminder: x86-64 Integer Registers
Accessing the low-order byte:
19
%rsp %spl
%r8b%r8
%r9b%r9
%r10b%r10
%r11b%r11
%r12b%r12
%r13b%r13
%r14b%r14
%r15b%r15
%al%rax
%bl%rbx
%cl%rcx
%dl%rdx
%sil%rsi
%dil%rdi
%bpl%rbp
CSE351, Spring 2020L09: x86 Programming II
Reading Condition Codes
set* Instructions
Set a low-order byte to 0 or 1 based on condition codes
Operand is byte register (e.g. al, dl) or a byte in memory
Do not alter remaining bytes in register• Typically use movzbl (zero-extended mov) to finish job
20
int gt(long x, long y)
{
return x > y;
}
Register Use(s)
%rdi 1st argument (x)
%rsi 2nd argument (y)
%rax return value
cmpq %rsi, %rdi #
setg %al #
movzbl %al, %eax #
ret
CSE351, Spring 2020L09: x86 Programming II
Reading Condition Codes
set* Instructions
Set a low-order byte to 0 or 1 based on condition codes
Operand is byte register (e.g. al, dl) or a byte in memory
Do not alter remaining bytes in register• Typically use movzbl (zero-extended mov) to finish job
21
int gt(long x, long y)
{
return x > y;
}
Register Use(s)
%rdi 1st argument (x)
%rsi 2nd argument (y)
%rax return value
cmpq %rsi, %rdi # Compare x:y
setg %al # Set when >
movzbl %al, %eax # Zero rest of %rax
ret
CSE351, Spring 2020L09: x86 Programming II
Aside: movz and movs
movz__ src, regDest # Move with zero extension
movs__ src, regDest # Move with sign extension
Copy from a smaller source value to a larger destination
Source can be memory or register; Destination must be a register
Fill remaining bits of dest with zero (movz) or sign bit (movs)
movzSD / movsSD:
S – size of source (b = 1 byte, w = 2)
D – size of dest (w = 2 bytes, l = 4, q = 8)
Example:
movzbq %al, %rbx
22
0x?? 0x?? 0x?? 0x?? 0x?? 0x?? 0x?? 0xFF ←%rax
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xFF ←%rbx
CSE351, Spring 2020L09: x86 Programming II
Aside: movz and movs
movz__ src, regDest # Move with zero extension
movs__ src, regDest # Move with sign extension
Copy from a smaller source value to a larger destination
Source can be memory or register; Destination must be a register
Fill remaining bits of dest with zero (movz) or sign bit (movs)
movzSD / movsSD:
S – size of source (b = 1 byte, w = 2)
D – size of dest (w = 2 bytes, l = 4, q = 8)
Example:
movsbl (%rax), %ebx
23
Note: In x86-64, any instruction that generates a 32-bit (long word) value for a register also sets the high-order portion of the register to 0. Good example on p. 184 in the textbook.
Copy 1 byte from memory into 8-byte register & sign extend it
0x00 0x00 0x7F 0xFF 0xC6 0x1F 0xA4 0xE8 ←%rax
0x00 0x00 0x00 0x00 0xFF 0xFF 0xFF 0x80 ←%rbx
... 0x?? 0x?? 0x80 0x?? 0x?? 0x?? ... ← MEM
CSE351, Spring 2020L09: x86 Programming II
Summary
Control flow in x86 determined by status of Condition Codes
Showed Carry, Zero, Sign, and Overflow, though others exist
Set flags with arithmetic instructions (implicit) or Compare and Test (explicit)
Set instructions read out flag values
Jump instructions use flag values to determine next instruction to execute