Machine Programming Control

References

Outline

Processor State (x86-64, Partial)

Condition Codes (Implicit Setting)

Condition Codes (Explicit Setting)

Condition Codes (Explicit Setting)

Condition Codes (Explicit Reading)

Condition Codes (Explicit Reading)

Instuction 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)

Example: setl (signed <)

Jumping

Jumping

Instruction 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)

Conditional Branch Example

Conditional Branch Example

Conditional Branch Example

Conditional Moves

Conditional Move Example

Bad Cases for Conditional Move

Condition Code Example

Do While Loop Example

Do While Loop Example

Do While Loop Compilation

General Do While Translation

General While Translation #1

While Loop Example #1

While Loop Example #1

General While Translation #2

While Loop Example #2

For Loop

For Loop Translation

For While Conversion

For Loop Do-While Conversion

Switch Statements Example

long my_switch(long x, long y, long z) {
    long w = 1;
    switch(x) {
        case 1: w = y*z; break;
        case 2: w = y/z; /* fall through */
        case 3: w += z; break;
        case 5:
        case 6: w -= z; break;
        default: w = 2;
    }
    return w;
}

Switch Statements

Jump Table Structure

Switch Statement Example

Switch Statement Example

Switch Statement Example

Switch Statement Example

Switch Statement Example

Summarizing

Summary