1
Machine-Level Representation of Programs
III
2
Outline
• Jump instructions
• Translate Control constructs in C to
assembly
– goto
– branch
– loop
• Suggested reading
– Chap 3.6
3
Control
• Two of the most important parts of
program execution
– Data flow (Accessing and operating data)
– Control flow (control the sequence of
operations)
4
Control
• Sequential execution is default– the instructions are executed in the order
they appear in the program
• Chang the control flow– Jump instructions
5
Jump 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
6
Unconditional jump
• Jumps unconditionally
• Direct jump: jmp label
– jmp .L
• Indirect jump: jmp *Operand
– jmp *%eax
– jmp *(%eax)
7
Conditional 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
8
Jump Instructions
1 jle .L22 .L5:3 movl %edx, %eax4 sarl $1, %eax5 subl %eax, %edx6 Leal (%edx, %edx, 2), %edx7 testl %edx, %edx8 jg .L59 .L2:10 movl %edx, %eax
9
1 8: 7e 0d jle 17<silly+0x17>
2 a: 89 d0 mov %edx, %eax dest1:
3 c: c1 f8 sar %eax
4 e: 29 c2 sub %eax, %edx
5 10: 8d 14 52 lea (%edx, %edx, 2), %edx
6 13: 85 d2 test %edx, %edx
7 15: 7f f3 jg a<silly+0x10>
8 17: 89 d0 movl %edx, %eax dest2:
d+a = 17
17+f3(-d) =a
Example for Jump
10
1 804839: 7e 0d jle 17<silly+0x17>
2 804839e: 89 d0 mov %edx, %eax dest1:
3 80483a0: c1 f8 sar %eax
4 80483a2: 29 c2 sub %eax, %edx
5 80483a4: 8d 14 52 lea (%edx, %edx, 2), %edx
6 80483a7: 85 d2 test %edx, %edx
7 80483a9: 7f f3 jg a<silly+0x10>
8 80483ab: 89 d0 movl %edx, %eax dest2:
d+804849e = 80483ab
80483ab+f3(-d) = 804839e
Example for Jump
11
Jump Instructions
• PC-relative – Jump target is an offset relative to the address
of the instruction just followed jump (pointed by PC)
• Absolute address – Jump target is an absolute address
12
Control Constructs in C
• Gotos
– goto L
– break
– continue
• Branch
– if () { } else { }
– switch () { }
13
Control Constructs in C
• Loop
– while () { }
– do { } while ()
– for (init; test; incr) { }
14
Translating Conditional Branches
if ( test-expr )
then-statement
else
else-statement
t = test-expr ;
if ( t )
goto true ;
else-statement
goto done
true:
then-statement
done:
15
Translating Conditional Branches
• int absdiff(int x, int y)• {• if (x < y)• return y – x;• else• return x – y;• }
• int absdiff(int x, int y)• {• int rval ;• if (x < y)• goto less• rval = x – y ;• goto done;• less:• rval = y – x;• done:• return rval;• }
16
Jump Instructions
1. movl 8(%ebp), %edx get x
2. movl 12(%ebp), %eax get y
3. cmpl %eax, %edx cal x - y
4. jl .L3 if x < y goto less
5. subl %eax, %edx compute x - y
6. movl %edx, %eax set return val
7. jmp .L5 goto done
8. .L3: less:
9. subl %edx, %eax compute y – x
10. .L5: done: Begin Completion code
17
Do-while Translation
do body-statement
while (test-expr)
loop: body-statement
t = test-expr; if ( t )
goto loop ;
18
Do-while Translation
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 ( i<n) ; return val ; }
.L6:
lea (%ebx, %edx), %eax
movl %edx, %ebx
movl %eax, %edx
incl %ecx
cmpl %esi, %ecx
jl .L6
movl %ebx, %eax register value initially
%ecx i 0
%esi n n
%ebx val 0
%edx nval 1
%eax t -
19
While 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:
20
While Loop Translation
int fib_w(int n) {
int i=1;int val=1;int nval=1;
while ( i<n ) {int t=val+nval ;val = nval ;nval = t ;i++;
}return val ;
}
int fib_w_goto(int n) {
int val=1;int nval=1;
int nmi, t ;
if ( val >= n )goto done ;
nmi = n-1;
loop:t=val+nval ;val = nval ;nval = t ;
nmi--;if ( nmi )
goto loop done:
return val }
21
While Loop Translation
1. movl 8(%ebp), %eax
2. movl $1, %ebx
3. movl $1, %ecx
4. cmpl %eax, ebx
5. jge .L9
6. lea –1(%eax), %edx
7. .L10:
8. lea (%ecx, %ebx), %eax
9. movl %ecx, %ebx
10. movl %eax, %ecx
11. decl %edx
12. jnz .L10
13. .L9:
Register usageRegister Variable Initially
%edx nmi n-1
%ebx val 1
%ecx nval 1
22
While Loop Translation
/* strcpy: copy t to s; pointer version 2 */void strcpy(char *s, char *t){
while ((*s = *t) != '\0') {s++ ;t++ ;
}}
23
While Loop Translation
_strcpy:
pushl %ebp
movl %esp, %ebp
jmp L2
L3:
addl $1, 8(%ebp)
addl $1, 12(%ebp)
L2:
movl 12(%ebp), %eax
movzbl (%eax), %edx
movl 8(%ebp), %eax
movb %dl, (%eax)
movl 8(%ebp), %eax
movzbl (%eax), %eax
testb %al, %al
jne L3
popl %ebp
ret
24
For Loop Translation
for ( init-expr; test-expr; update-expr)body-statement
init-expr while ( test-expr) {
body-statementupdate-expr
}
25
For Loop Translation
/* strcmp: return <0 if s<t, 0 if s==t, >0 if s>t */
int strcmp(char *s, char *t)
{
for (; *s == *t ; s++, t++)
if (*s == '\0')
return 0;
return *s - *t;
}
26
For Loop Translation
_strcmp:pushl %ebpmovl %esp, %ebpsubl $4, %espjmp L2
L5:movl 8(%ebp), %eaxmovzbl (%eax), %eaxtestb %al, %aljne L3movl $0, -4(%ebp)jmp L4
L3:addl $1, 8(%ebp)addl $1, 12(%ebp)
L2:movl 8(%ebp), %eaxmovzbl (%eax), %edxmovl 12(%ebp), %eaxmovzbl (%eax), %eaxcmpb %al, %dlje L5
27
For Loop Translation
movl 8(%ebp), %eaxmovzbl (%eax), %eaxmovsbl%al,%edxmovl 12(%ebp), %eaxmovzbl (%eax), %eaxmovsbl%al,%eaxmovl %edx, %ecxsubl %eax, %ecxmovl %ecx, -4(%ebp)
L4:movl -4(%ebp), %eaxleaveret
28
case 103
result += 11;
break ;
case 104: case 106:
result *= result ;
break ;
default:
result = 0 ;
}
return result ;
}
Switch Statements
int switch_eg(int x, int n)
{
int result = x ;
switch ( n ) {
case 100:
result *= 13 ;
break ;
case 102:
result += 10 ;
/* fall through */
29
Switch Construct
• Properties of Switch Construct– Integer testing– Multiple outcomes (may be a large number)– Improve the readability of the source code
30
case 103
result += 11;
break ;
case 104: case 106:
result *= result ;
break ;
default:
result = 0 ;
}
return result ;
}
Switch Statements
int switch_eg(int x, int n)
{
int result = x ;
switch ( n ) {
case 100:
result *= 13 ;
break ;
case 102:
result += 10 ;
/* fall through */Integer
testing
Multiple cases
31
Switch Form
switch(op) { case val_0: Block 0 case val_1: Block 1 • • • case val_n-1: Block n–1}
32
Jump Table
• Efficient implementation• Avoid long sequence of if-else statement• Criteria
– the number of cases and the sparcity of the case value
33
Jump Table Implementation
Targ0Targ1Targ2
Targn-1
•••
jtab:
Jump Table
target = JTab[op];goto *target;
Approx. Translation
Code Block 0Targ0:
Code Block 1Targ1:
Code Block 2Targ2:
Code Block n–1Targn-1:
•••
Jump Targets
34
case 103
result += 11;
break ;
case 104: case 106:
result *= result ;
break ;
default:
result = 0 ;
}
return result ;
}
Switch Statements
int switch_eg(int x, int n)
{
int result = x ;
switch ( n ) {
case 100:
result *= 13 ;
break ;
case 102:
result += 10 ;
/* fall through */
35
Jump Table Implementation
int switch_eg_goto ( int x, int n) {
unsigned ni = n-100;int result = x ;if ( ni >6 )
goto loc_def ; //defaultgoto jt[xi];
loc_a: //100result *= 13 ;goto done ;
loc_b: //102
result += 10 ; /* fall through*/
loc_c: //103
result +=11;
goto done ;
loc_d: //104, 106
result *= result ;
goto done ;
loc_def: //default
result = 0 ;
done:
return result ;
}
code jt[7] = {loc_a, loc_def, loc_b, loc_c, loc_d, loc_def, loc_d};
36
Jump Table
1. .section .rodata
2. .align 4
3. .L7:
4. .long .L3 case 100: loc_a
5. .long .L2 case 101: loc_def
6. .long .L4 case 102: loc_b
7. .long .L5 case 103: loc_c
8. .long .L6 case 104: loc_d
9. .long .L2 case 105: loc_def
10. .long .L6 case 106: loc_d
37
Jump Table Implementation
1. movl 8(%ebp), %edx get x
2. movl 12(%ebp), %eax get n
3. subl $100, %eax compute index = n – 100
4. cmpl $6, %eax compare index:6
5. ja .L2 If > , goto default
6. jmp *.L7(, %eax, 4)
7. .L2: default:
8. mov $0, %eax result = 0
9. jmp .L8 goto done
38
Jump Table Implementation
10. .L5: loc_c: // 103
11. movl %edx, %eax result = x
12. jmp .L9 goto rest
13. .L3: loc_a: // 100
14. leal (%edx, %edx, 2), %eax result = x * 3
15. leal (%edx, %eax, 4), %eax result = x + 4 * result
16. jmp .L8 goto done
17. .L4: loc_b: // 102
18. leal 10(%edx), %eax result = x + 10
39
Jump Table Implementation
19. .L9: rest: // fall through
20. addl $11, %eax result += 11
21. jmp .L8 goto done
22. .L6: loc_d: // 104, 106
23. movl %edx, %eax result = x
24. imull %edx, %eax result *= x
25. .L8: done: