Temporary data: %rax, ... Location of runtime stack: %rsp Location of current code control point: %rip, ... Status of recent tests: CF, ZF, SF, OF (condition codes)
Condition Codes: single bit storage
CF: Carry Flag (for unsigned)
SF: Sign Flag (for signed)
ZF: Zero Flag
OF: Overflow Flag (for signed)
Note: leaq
instruction does not set flags
cmpq Src2, Src1
: computing a-b without setting destination
CF: unsigned comparison, if carry/borrow from most significant bit
ZF: a == b
SF: (a-b) < 0 (or a < b, as signed)
OF: if signed overflow
testq Src2, Src1
: computing a&b without setting destination
ZF: a&b == 0
SF: a&b < 0
use case: masking, testq %rax, %rax
check whether %tax is 0
setX Dest
: set lowest byte to 0 or 1 based on some condition flags
cmp %rsi, %rdi # compare x and y
setg %al # lowest byte of %rax set to 1 when >
movzbl %al, %eax # move %al to %eax (both part of %rax) with zero extend from 1 byte quantity to 4 byte quantity (actually zero padding both 4 byte quantity and 8 byte quantity)
jX Dest
(or bX sometimes): jump to part of code depending on condition codes
ja
(unsigned) may be optimized to do if (x > 6 || x < 0)
when x is signed.Conditional Moves (cmov..
): val = Test ? Then_Expr : Else_Expr
(compiler only do it when it is safe)
calculate both resulting branches before testing
instead of guessing the testing result (very inefficient when guessed wrong)
Bad use cases
test
: logical AND
cmp
: subtraction
While Loop in C
while (Test)
Body
While Loop in Goto C
goto test;
loop:
Body
test:
if (Test)
goto loop;
done:
Do-While Loop in C
do
Body
while (Test);
Do-While Loop in Goto C
loop:
Body
if (Test)
goto loop
For Loop in C
for (Init; Test; Update)
Body
For Loop in Goto C
Init;
while (Test) {
Body
Update
}
Example: jmp *.L4 (, %rdi, 8)
breake
jump here is optimized to return
because nothing after break
Table of Content