Title: MachineLevel Representation of Programs IV
1Machine-Level Representation of Programs IV
2Outline
- Translate Control constructs in C to assembly
- Suggested reading
- Chap 3.6,
3Control
- Two of the most important parts of program
execution - Data flow (Accessing and operating data)
- Control flow (control the sequence of operations)
4Control
- Sequential execution is default
- The statements in C and
- the instructions in assembly code
- are executed in the order they appear in the
program - Chang the control flow
- Control constructs in C
- Jump in assembly
5Goto Constructs in C
6Jump Instructions
- Under normal execution
- instructions follow each other in the order they
are listed - A jump instruction can cause
- the execution to switch to a completely new
position in the program. - Label
- Jump destinations
7Jump Instructions
- 1 xorl eax, eax Set eax to 0
- 2 jmp .L1 Goto .L1
- 3 movl (eax), edx Null pointer
dereference - 4 .L1
- 5 popl edx
8Unconditional jump
- Jumps unconditionally
- Direct jump jmp label
- jmp .L
- Indirect jump jmp Operand
- jmp eax
- jmp (eax)
9Other Control Constructs in C
- if () else
- while ()
- do while ()
- for (init test incr)
- switch ()
- What are the counterparts in assembly?
- Conditional jump
10Assembly Programmers View
11Condition codes
- Condition codes
- A set of single-bit
- Maintained in a condition code register
- Describe attributes of the most recently
arithmetic or logical operation
12Condition codes
- EFLAGS
- CF Carry Flag
- The most recent operation generated a carry out
of the most significant bit - Used to detect overflow for unsigned operations
- OF Overflow Flag
- The most recent operation caused a twos
complement overflow either negative or positive
13Condition codes
- EFLAGS
- ZF Zero Flag
- The most recent operation yielded zero
- SF Sign Flag
- The most recent operation yielded a negative value
14Setting Conditional Codes
- Implicit Setting By Arithmetic Operations
- addl Src,Dest
- C analog t ab
- CF set if carry out from most significant bit
- Used to detect unsigned overflow
- ZF set if t 0
- SF set if t lt 0
- OF set if twos complement overflow
- (agt0 bgt0 tlt0) (alt0 blt0 tgt0)
15Conditional Code
- lea instruction
- has no effect on condition codes
- Xorl instruction
- The carry and overflow flags are set to 0
- Shift instruction
- carry flag is set to the last bit shifted out
- Overflow flag is set to 0
16Setting Conditional Codes
- Explicit Setting by Compare Instruction
- cmpl Src2,Src1
- cmpl b,a like computing a-b without setting
destination - CF set if carry out from most significant bit
- Used for unsigned comparisons
- ZF set if a b
- SF set if (a-b) lt 0
- OF set if twos complement overflow
- (agt0 blt0 (a-b)lt0)
- (alt0 bgt0 (a-b)gt0)
17Setting Conditional Codes
- Explicit Setting by Test instruction
- testl Src2,Src1
- Sets condition codes based on value of Src1
Src2 - Useful to have one of the operands be a mask
- testl b,a like computing ab without setting
destination - ZF set when ab 0
- SF set when ab lt 0
18Accessing Conditional Codes
- The condition codes cannot be read directly
- One of the most common methods of accessing them
is - setting an integer register based on some
combination of condition codes - Set commands
19Accessing Conditional Codes
- After each set command is executed
- A single byte to 0 or to 1 is obtained
- The descriptions of the different set commands
apply to the case - where a comparison instruction has been executed
20Accessing Conditional Codes
21Accessing Conditional Codes
- The destination operand is either
- one of the eight single-byte register elements or
- a memory location where the single byte is to be
stored - To generate a 32-bit result
- we must also clear the high-order 24 bits
22Accessing Conditional Codes
- Initially a is in edx, b is in eax
- 1 cmpl eax, edx compare ab
- 2 setl al set low order by to 0 or 1
- 3 movzbl al, eax
- set remaining bytes of eax to 0
23Conditional jump
- Either jump or continue executing at the next
instruction in the code sequence - Depending on some combination of the condition
codes - All direct jump
24Jump Instructions
- 1 jle .L4
- 2 .p2align 4,,7 align next instruction to
multiple of 8 - 3 .L5
- movl edx, eax
- sarl 1, eax
- subl eax, edx
- testl edx, edx
- jg .L5
- 9 .L4
- 10 movl edx, eax
25Translating Conditional Branches
t test-expr if ( t ) goto true
else-statement goto done true then-statement
done
if ( test-expr ) then-statement
else else-statement
26Translating Conditional Branches
27Jump Instructions
- movl 8(ebp), edx get x
- movl 12(ebp), eax get y
- cmpl eax, edx cal x - y
- jl .L3 if x lt y goto less
- subl eax, edx compute x - y
- movl edx, eax set return val
- jmp .L5 goto done
- .L3 less
- subl edx, eax compute y x
- .L5 done Begin Completion code
28Do-while Translation
- do
- body-statement
- while (test-expr)
- loop
- body-statement
- t test-expr
- if ( t )
- goto loop
29Do-while Translation
.L6 lea (ebx, edx), eax movl edx,
ebx movl eax, edx incl ecx cmpl
esi, ecx jl .L6 movl ebx, eax
- int fib_dw(int n)
-
- int i 0
- int val 0
- int nval 1
-
- do
- int t val nval
- val nval
- nval t
- i
- while ( iltn)
- return val
-
30While Loop Translation
- while (test-expr)
- body-statement
- loop if ( !test-expr)
- t test-expr goto done
- if ( !t ) do
- goto done body-statement
- body-statement while(test-expr)
- goto loop done
- done
31While Loop Translation
- int fib_w(int n)
-
- int i1
- int val1
- int nval1
-
- while ( iltn )
- int tvalnval
- val nval
- nval t
-
- return val
-
int fib_w_got0(int n) int val1 int
nval1 int nmi, t if ( val gt n ) goto
done nmi n-1 loop tvalnval val
nval nval t nmi-- if ( nmi ) goto
loop done return val
32While Loop Translation
- movl 8(ebp), eax
- movl 1, ebx
- movl 1, ecx
- cmpl eax, ebx
- jge .L9
- lea 1(eax), edx
- .L10
- lea (ecx, ebx), eax
- movl ecx, ebx
- movl eax, ecx
- decl edx
- jnz .L10
- .L9