Untitled
Machine-Level Programming II: Control
Overview of Today's Topics
Control: Focuses on the mechanisms of controlling program flow in machine-level programming.
Condition codes
Conditional branches
Loops
Switch statements
Processor State (x86-64, Partial)
Purpose: Holds information about the currently executing program.
Temporary Data: Stored in registers such as %rax, %rbx, %rcx, %rdx, %rsi, %rdi, %rbp, and others.
Location of Runtime Stack: Specification via %rsp.
Location of Current Code Control Point: Maintained in %rip.
Status of Recent Tests: Includes condition flags (CF, ZF, SF, OF).
Condition Codes (Implicit Setting)
Definition: Special single bit registers that provide status about the results of arithmetic operations.
Flags:
CF (Carry Flag): Indicates an unsigned overflow condition (set if there is a carry out from the most significant bit).
ZF (Zero Flag): Set if the result equals zero.
SF (Sign Flag): Set if the result is negative when treated as signed.
OF (Overflow Flag): Set if there is a signed overflow, which occurs under certain conditions defined for two's-complement arithmetic.
Example: When using the instruction
addq Src, Dest:Compute .
CF is set if there’s an unsigned overflow.
ZF is set if .
SF is set if .
OF is set if signed overflow occurs, defined as:
or.
Condition Codes (Explicit Setting)
Compare Instruction
cmpq Src2, Src1: Used for explicit setting of condition codes by simulating the operation of subtraction without modifying the destination.Flags set by the operation:
CF is set if there’s an unsigned overflow (carry out).
ZF is set if .
SF is set if (when treated as signed).
OF is set if signed overflow occurs:
or.
Test Instruction
testq Src2, Src1: Used to explicitly set condition codes based on bitwise AND operation.Operation: Computes a & b without changing the destination.
Flags set:
ZF is set when a & b == 0.
SF is set when a & b < 0.
Reading Condition Codes
SetX Instructions: A family of instructions to set the low-order byte of a destination to either 0 or 1 based on condition codes.
Instruction family:
sete: sets if ZF is true (equal / zero).setne: sets if ZF is false (not equal / not zero).sets: sets if SF is true (negative).setns: sets if SF is false (nonnegative).setg: sets if greater than (signed), defined as.setge: sets if greater than or equal to (signed).setl: sets if less than (signed).setle: sets if less than or equal to (signed).seta: sets if above (unsigned).setb: sets if below (unsigned).
x86-64 Integer Registers
Accessing Registers: Can reference low-order bytes in addition to standard registers.
Example:
Low-order
%al,%bl,%cl, etc. along with their higher forms like%rax,%rbx, etc.
Purpose of Registers:
%rax: Return value.%rbx: Callee-saved register.%rcx: 4th argument.%rdx: 3rd argument.%rsi: 2nd argument.%rdi: 1st argument.%rbp: Callee-saved base pointer.%rsp: Stack pointer.
Example of Reading Condition Codes
Code Sample:
cmpq %rsi, %rdi # Compare arguments x and y
setg %al # Set %al if x > y
movzbl %al, %eax # Zero extend %al into %eax
ret
Control: Conditional Branching Statements
Goto Statement in C
Definition: A method for jumping to labeled statements in C programs.
C Code Example:
#include <stdio.h>
int main() {
int n = 0;
if (n == 0)
goto jump_here;
printf("You entered: %d\n", n);
jump_here:
printf("Exiting the program.\n");
return 0;
}
Use Case: When specific conditions are met, the program can jump to pre-defined labels.
Switch Case in C
Definition: A control statement that executes code blocks based on the value of a variable.
C Code Example:
#include <stdio.h>
int main() {
int var = 1;
switch (var) {
case 1:
printf("Case 1 is Matched.");
break;
case 2:
printf("Case 2 is Matched.");
break;
case 3:
printf("Case 3 is Matched.");
break;
default:
printf("Default case is Matched.");
break;
}
return 0;
}
Jump Instructions and their Conditions
jX Instructions: Used to control the flow by jumping to different parts of code based on set conditions.
Instruction Set:
jmp: Unconditional jump.je: Jump if equal (ZF is set).jne: Jump if not equal (ZF is not set).js: Jump if negative (SF is set).jns: Jump if nonnegative (SF is not set).jg: Jump if greater (signed).jge: Jump if greater or equal (signed).jl: Jump if less (signed).jle: Jump if less or equal (signed).ja: Jump if above (unsigned).jb: Jump if below (unsigned).
Conditional Branch Example (Old Style)
Function Implementation:
long absdiff(long x, long y) {
long result;
if (x > y)
result = x - y;
else
result = y - x;
return result;
}
Assembly Representation:
absdiff:
cmpq %rsi, %rdi # x:y
jle .L4
movq %rdi, %rax
subq %rsi, %rax
ret
.L4:
movq %rsi, %rax
subq %rdi, %rax
ret
Jump in C (Using Goto)
Refactored Example:
long absdiff_j(long x, long y) {
long result;
int ntest = x <= y;
if (ntest) goto Else;
result = x - y;
goto Done;
Else:
result = y - x;
Done:
return result;
}
Conditional Expressions and Goto Version
Example:
val = Test ? Then_Expr : Else_Expr;
// Goto Version
ntest = !Test;
if (ntest) goto Else;
val = Then_Expr;
goto Done;
Else:
val = Else_Expr;
Done:
Using Conditional Moves
Definition: Optimizes branchless execution by using the
cmovinstruction for moving data, thus avoiding control transfer.Risks: Certain computations may require conditional moves to be side-effect-free.
Cost Implications of Conditional Moves
Examples of Risky Computations:
val = p ? *p : 0; // Dereference must be side-effect free
val = x > 0 ? x*=7 : x+=3; // Both operations executed if no side effect guarantees
Example of Counting Bits Using Loops
Do-While Loop Example
C Code:
long pcount_do(unsigned long x) {
long result = 0;
do {
result += x & 0x1;
x >>= 1;
} while (x);
return result;
}
Goto Version:
long pcount_goto(unsigned long x) {
long result = 0;
loop:
result += x & 0x1;
x >>= 1;
if (x)
goto loop;
return result;
}
General Do-While Loop Translation
Translation Logic:
C Code:
do {
Body;
} while (Test);
Goto Version:
loop:
Body;
if (Test)
goto loop;
While and For Loops
While Loop Translation
C Code:
long pcount_while(unsigned long x) {
long result = 0;
while (x) {
result += x & 0x1;
x >>= 1;
}
return result;
}
Goto Version:
long pcount_goto_jtm(unsigned long x) {
long result = 0;
goto test;
loop:
result += x & 0x1;
x >>= 1;
test:
if (x)
goto loop;
return result;
}
General While Loop Translation – Approach 2
C Code:
while (Test)
Body;
Goto Version:
goto test;
loop:
Body;
test:
if (Test)
goto loop;
done:
Switch Statement Example
C Code for Switch Statement
long switch_eg(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;
}
Assembly for Switch Statement
Directly Implemented:
switch_eg:
movq %rdx, %rcx
cmpq $6, %rdi # Check if x is less than or equal to 6
ja .L8 # Jump to default if x > 6
jmp *.L4(,%rdi,8) # Indirect jump using a jump table
Summary of Control Methods in C vs. Assembly
C Control Statements:
if-then-else
do-while
while, for
switch
Assembly Control Constructs:
Conditional jumps
Conditional moves
Indirect jumps (via jump tables)
Compilation Techniques:
Loops converted into more efficient forms like do-while or jump-to-middle method.
Large switch statements optimized using jump tables, while sparse statements may use decision trees.
Future Topics
Next time: Focus on stack management, function calls, and the procedure call discipline in programming.