CMPT 295
- Unit - Machine-Level Programming
- Lecture 12 – Assembly language – Program Control – Conditional Statements
Last Lecture
- Demo
- Observation: C compiler will figure out different instruction combinations to carry out the computations in our C code
Today’s Menu
- Introduction
- C program -> assembly code -> machine level code
- Assembly language basics: data, move operation
- Memory addressing modes
- Operation leaq and Arithmetic & logical operations
- Conditional Statement – Condition Code + cmovX (selected)
- Loops
- Function call – Stack
- Array
- Buffer Overflow
- Floating-point operations
Completing our Demo
- (checked)
gcc
usesleaq
for addition ->sum_store.c
our own assembly code (arith.s) using arithmetic - (checked) Writing instructions of x86-64 assembly language
- makefile
- when we compile our own *.s files with *.c files
- when we compile only *.c files
- How would
gcc
compile ourarith.c
intoarith.s
?
Program Control Overview
- We can change the execution flow of a program
- Based on a condition
- Unconditionally
- Control statements (in C)
- if/else
- switch
- cmp* instruction (compare)
- for loop
- Control statements (in x86-64 assemvly)
- cmp* instruction (compare)
- jX instructions (jump)
Function calls -> call
and ret
Conditional statement: if/else
in C:
void func(long x,long y){
if ( x < y ) {
// stmts true
} else {
// stmts false
}
return;
}
A label is a memory address.
in assembly:
# func is a label
func:
cmpq %rsi,%rdi # x – y
jge else #
... # stmts true
jmp endif #
# else is a label
else: ... # stmts false
# endif is a label
endif: ret #
We branch (jump) when the condition is false -> This technique is called “coding the false condition first”
comparison instructions
- * -> Size designator
- q -> long/64-bits
- l -> int/32-bits
- w -> short/16-bits
- b -> char/8-bits
Remember in Lecture 9, we saw… (See header “Programming in x86-64 assembly” from lecture 9)
Syntax | Meaning/Effect | Example | Notes |
---|---|---|---|
cmp* Src2, Src1 | Src1 – Src2 -> > 0? -> Src1 > Src2 = 0? -> Src1 == Src2 < 0? -> Src1 < Src2 |
cmpq %rsi,%rdi |
without saving the result in the destination operand (no Dest). Sets condition codes based on value of |
test* Src2, Src1 | Src1 & Src2 -> | testq %rax,%rax |
without saving the result in the destination operand (no Dest); sets condition code based on value of ; useful when one of the operands is a bit mask |
jX
jump family instructions (branching)
- Jump to different part of the program depending on result of previous instructions (i.e., condition codes)
jX |
Description |
---|---|
jmp | unconditional |
je | Equal / Zero |
jne | Not Equal / Not Zero |
js | Negative |
jns | Nonnegative |
jg | Greater (Signed) |
jge | Greater or Equal (Signed) |
jl | Less (Signed) |
jle | Less or Equal (Signed |
ja | Above (unsigned) |
jb | Below (unsigned) |
Example – int abs(int x)
in C:
int abs(int x){
if(x<0)
x=-x;
return x;
}
in assembly:
# x in edi, result in eax
abs:
movl %edi,%eax # eax <- x
____ #
____ # ret if x >= 0
____ # x = -x
endif:
ret
int max(int x, int y
)- Homework
In C:
int max(int x, int y){
int result=x;
if(y>x)
result=y;
return result;
}
In assembly:
# x in edi, y in esi, result in eax
max:
movl %edi,%eax # result = x
____
____
____
____
____
endif:
ret
Summary
- In C, we can change the execution flow of a program
- Conditionaly
- Conditional statements: if/else, switch
- Iterative statements: loops
- Unconditionally
- Functions calls
- Conditionaly
- In x86-64 assembly, we can also change the execution flow of a program
cmp*
instruction (compare)jX
instructions (jump)call
andret
instructions
Next Lecture
- Introduction
- C program -> assembly code -> machine level code
- Assembly language basics: data, move operation
- Memory addressing modes
- Operation leaq and Arithmetic & logical operations
- Conditional Statement – Condition Code + cmovX
- (selected) Loops
- Function call – Stack
- Array
- Buffer Overflow
- Floating-point operations