Lecture 005

Processor States (x86-64)

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

Implicit Setting when Arithmetic

Condition Codes: single bit storage

Note: leaq instruction does not set flags

Explicit Setting Condition Codes

cmpq Src2, Src1: computing a-b without setting destination

testq Src2, Src1: computing a&b without setting destination

Explicit Reading Condition Codes

setX Dest: set lowest byte to 0 or 1 based on some condition flags

setX Table

setX Table

Lowest Byte

Lowest Byte

instructions

instructions

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

jX Table

jX Table

Conditional Moves (cmov..): val = Test ? Then_Expr : Else_Expr (compiler only do it when it is safe)

test: logical AND cmp: subtraction

Loops

While

While Loop in C

while (Test)
  Body

While Loop in Goto C

  goto test;
loop:
  Body
test:
  if (Test)
    goto loop;
done:

Do-While

Do-While Loop in C

do
  Body
  while (Test);

Do-While Loop in Goto C

loop:
  Body
  if (Test)
  goto loop

For

For Loop in C

for (Init; Test; Update)
  Body

For Loop in Goto C

Init;
while (Test) {
  Body
  Update
}

Jump Table

Jumptable Structure

Jumptable Structure

Example: jmp *.L4 (, %rdi, 8)

Table of Content