Machine-Level Programming Iii: Switch Statements and Ia32 Procedures
Machine-Level Programming Iii: Switch Statements and Ia32 Procedures
Today
• Switch statements
• IA 32 Procedures
• Stack Structure
• Calling Conventions
• Illustrations of Recursion & Pointers
2
University of Texas at Austin
long
long switch_eg
switch_eg
(long
Switch Statement
(long x,x, long
long y,
y, long
long z)
z)
{{ Example
long
long ww == 1;
1;
switch(x)
switch(x) {{
case
case 1:
1:
ww == y*z;
y*z; • Multiple case labels
break;
break;
case
case 2:
2:
• Here: 5 & 6
ww == y/z;
/*
y/z;
/* Fall
Fall Through
Through */
*/
• Fall through cases
case
case 3:
3: • Here: 2
ww +=
+= z;
z;
break;
break; • Missing cases
case
case 5:
5:
case
case 6:
6: • Here: 4
ww -=
-= z;
z;
break;
break;
default:
default:
ww == 2;
2;
}}
return
return w;w;
}}
3
University of Texas at Austin
•
Approximate Translation •
target
target==JTab[x];
JTab[x]; •
goto
goto*target;
*target;
Targn-1: Code Block
n–1
4
University of Texas at Austin
6
University of Texas at Austin
Jump Table
Jump table
switch(x)
switch(x) {{
.section
.section .rodata case
.rodata case 1:
1: //
// .L3
.L3
.align
.align 44 ww == y*z;
y*z;
.L7:
.L7:
.long .L2
break;
break;
.long .L2 ## xx == 00
.long .L3 case
case 2:
2: //
// .L4
.L4
.long .L3 ## xx == 11
.long
.long .L4
.L4 ## xx == 22 ww == y/z;
y/z;
.long
.long .L5
.L5 ## xx == 33 /*
/* Fall
Fall Through
Through */
*/
.long
.long .L2
.L2 ## xx == 44 case
case 3:
3: //
// .L5
.L5
.long
.long .L6
.L6 ## xx == 55 ww +=
+= z;
z;
.long
.long .L6
.L6 ## xx == 66 break;
break;
case
case 5:
5:
case
case 6:
6: //
// .L6
.L6
ww -=
-= z;
z;
break;
break;
default:
default: //
// .L2
.L2
ww == 2;
2;
}}
8
University of Texas at Austin
Handling Fall-Through
long
long ww == 1;
1; case
case 3:
3:
.. .. ..
ww == 1;
1;
switch(x)
switch(x) {{ goto
goto merge;
merge;
.. .. ..
case
case 2:2:
ww == y/z;
y/z;
/*
/* Fall
Fall Through
Through */
*/
case
case 3:3: case
case 2:2:
ww +=
+= z;
z; ww == y/z;
y/z;
break;
break;
.. .. .. merge:
merge:
}} ww +=
+= z;
z;
9
University of Texas at Austin
switch(x)
switch(x) {{ .L2:
.L2: ## Default
Default
case
case 1: 1: //
// .L3
.L3 movl
movl $2,
$2, %eax
%eax ## ww == 22
ww == y*z;
y*z; jmp
jmp .L8
.L8 ## Goto
Goto done
done
break;
break;
.. .. .. .L5:
.L5: ## xx ==
== 33
case
case 3: 3: //
// .L5
.L5 movl
movl $1,
$1, %eax
%eax ## ww == 11
ww +=
+= z;
z; jmp
jmp .L9
.L9 ## Goto
Goto merge
merge
break;
break;
.. .. .. .L3:
.L3: ## xx ==
== 11
default:
default: //
// .L2
.L2 movl
movl 16(%ebp),
16(%ebp), %eax
%eax ## zz
ww == 2;
2; imull
imull12(%ebp),
12(%ebp), %eax
%eax ## ww == y*z
y*z
}} jmp
jmp .L8
.L8 ## Goto
Goto done
done
10
University of Texas at Austin
switch(x)
switch(x) {{ .L4:
.L4: ## xx ==
== 22
.. .. .. movl
movl 12(%ebp),
12(%ebp), %edx
%edx
case
case 2: 2: //// .L4
.L4 movl
movl %edx,
%edx, %eax
%eax
ww == y/z;
y/z; sarl
sarl $31,
$31, %edx
%edx
/*
/* Fall
Fall Through
Through */
*/ idivl
idivl16(%ebp)
16(%ebp) ## ww == y/z
y/z
merge:
merge: //
// .L9
.L9
ww +=
+= z;
z; .L9:
.L9: ## merge:
merge:
break;
break; addl
addl 16(%ebp),
16(%ebp), %eax
%eax ## ww +=
+= zz
case
case 5: 5: jmp
jmp .L8
.L8 ## goto
goto done
done
case
case 6: 6: //// .L6
.L6
ww -=
-= z;
z; .L6:
.L6: ## xx ==
== 5,
5, 66
break;
break; movl
movl $1,
$1, %eax
%eax ## ww == 11
}} subl
subl 16(%ebp),
16(%ebp), %eax
%eax ## ww == 1-z
1-z
11
University of Texas at Austin
13
University of Texas at Austin
14
University of Texas at Austin
15
University of Texas at Austin
16
University of Texas at Austin
Disassembled Targets
8048422:
8048422: b8
b8 02
02 00
00 00
00 00
00 mov
mov $0x2,%eax
$0x2,%eax
8048427:
8048427: eb
eb 2a
2a jmp
jmp 8048453
8048453 <switch_eg+0x43>
<switch_eg+0x43>
8048429:
8048429: b8
b8 01
01 00
00 00
00 00
00 mov
mov $0x1,%eax
$0x1,%eax
804842e:
804842e: 66
66 90
90 xchg
xchg %ax,%ax
%ax,%ax ## noop
noop
8048430:
8048430: eb
eb 14
14 jmp
jmp 8048446 <switch_eg+0x36>
8048446 <switch_eg+0x36>
8048432:
8048432: 8b
8b 45
45 10
10 mov
mov 0x10(%ebp),%eax
0x10(%ebp),%eax
8048435:
8048435: 0f
0f af
af 45
45 0c
0c imul
imul 0xc(%ebp),%eax
0xc(%ebp),%eax
8048439:
8048439: eb
eb 18
18 jmp
jmp 8048453
8048453 <switch_eg+0x43>
<switch_eg+0x43>
804843b:
804843b: 8b
8b 55
55 0c
0c mov
mov 0xc(%ebp),%edx
0xc(%ebp),%edx
804843e:
804843e: 89
89 d0
d0 mov
mov %edx,%eax
%edx,%eax
8048440:
8048440: c1
c1 fa
fa 1f
1f sar
sar $0x1f,%edx
$0x1f,%edx
8048443:
8048443: f7
f7 7d
7d 10
10 idivl
idivl 0x10(%ebp)
0x10(%ebp)
8048446:
8048446: 03
03 45
45 10
10 add
add 0x10(%ebp),%eax
0x10(%ebp),%eax
8048449:
8048449: eb
eb 08
08 jmp
jmp 8048453
8048453 <switch_eg+0x43>
<switch_eg+0x43>
804844b:
804844b: b8
b8 01
01 00
00 00
00 00
00 mov
mov $0x1,%eax
$0x1,%eax
8048450:
8048450: 2b
2b 45
45 10
10 sub
sub 0x10(%ebp),%eax
0x10(%ebp),%eax
8048453:
8048453: 5d
5d pop
pop %ebp
%ebp
8048454:
8048454: c3
c3 ret
ret
17
University of Texas at Austin
18
University of Texas at Austin
Summarizing
• C Control
• if-then-else
• do-while
• while, for
• switch
• Assembler Control
• Conditional jump
• Conditional move
• Indirect jump
• Compiler generates code sequence to implement more complex control
• Standard Techniques
• Loops converted to do-while form
• Large switch statements use jump tables
• Sparse switch statements may use decision trees
19
University of Texas at Austin
Today
• Switch statements
• IA 32 Procedures
• Stack Structure
• Calling Conventions
• Illustrations of Recursion & Pointers
20
University of Texas at Austin
IA32 Stack
• Region of memory
Stack “Bottom”
managed with stack
discipline
• Grows toward lower Increasing
Addresses
addresses
• Register %esp contains
lowest stack address
• address of “top” element Stack
Grows
Down
Stack Pointer: %esp
Stack “Top” 21
University of Texas at Austin
Stack
Grows
Down
Stack Pointer: %esp
-4
Stack “Top”
22
University of Texas at Austin
Stack
Grows
+4 Down
Stack Pointer: %esp
Stack “Top”
23
University of Texas at Austin
call 8048b90
0x110 0x110
0x10c 0x10c
0x108 123 0x108 123
0x104 0x8048553
ret
0x110 0x110
0x10c 0x10c
0x108 123 0x108 123
0x104 0x8048553 0x8048553
Stack-Based Languages
• Languages that support recursion
• e.g., C, Pascal, Java
• Code must be “Reentrant”
• Multiple simultaneous instantiations of single procedure
• Need some place to store state of each instantiation
• Arguments
• Local variables
• Return pointer
• Stack discipline
• State for given procedure needed for limited time
• From when called to when return
• Callee returns before caller does
• Stack allocated in Frames
• state for single procedure instantiation
27
University of Texas at Austin
28
University of Texas at Austin
Stack Frames
Previous
• Contents Frame
• Local variables
• Return information Frame Pointer: %ebp
• Management
• Space allocated when enter Stack “Top”
procedure
• “Set-up” code
• Deallocated when return
• “Finish” code 29
University of Texas at Austin
Example Stack
yoo(…) yoo
yoo(…) %ebp
{{ yoo
yoo
•• who %esp
••
who();
who();
•• amI amI
••
}} amI
amI
30
University of Texas at Austin
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…)
•{ yoo
yoo
•{ who
•• •• •• •• %ebp
amI();
who();
amI();
who();
•• •• •• •• amI amI who
%esp
•• amI();
amI();
}} •• •• ••
}} amI
amI
31
University of Texas at Austin
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…)
•{ yoo
yoo
•{ amI(…) who
•• ••amI(…)
•• ••
{{
amI();
who();
amI();
who(); • who
•• •• ••• •• amI amI
••
•• amI();
amI();
•• •amI(); %ebp
}} •amI();
••
}} •• amI amI
•• %esp
}}
amI
32
University of Texas at Austin
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…)
•{ yoo
yoo
•{ amI(…) who
•• ••amI(…)
•• ••
{{
amI();
who();
amI();
who();
• •••amI(…)
amI(…)
•• amI who
•• • • {{ amI
••
•• amI();
amI(); ••
• •amI();
amI();
•
}} • • • ••
}} •• amI amI
•• amI();
amI();
}} •• %ebp
•• amI amI
}}
%esp
33
University of Texas at Austin
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…)
•{ yoo
yoo
•{ amI(…) who
•• ••amI(…)
•• ••
{{
amI();
who();
amI();
who();
• •••amI(…)
amI(…)
•• amI who
•• • • {{ amI
••
•• amI();
amI(); ••amI(…)
amI(…)
amI();
amI();
•• •• •• • {{
}}
}} •• • • amI amI
•
•• amI();
amI();
••
}} ••
•• amI();
amI(); amI
•• amI
}}
••
}} %ebp
amI
%esp
34
University of Texas at Austin
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…)
•{ yoo
yoo
•{ amI(…) who
•• ••amI(…)
•• ••
{{
amI();
who();
amI();
who();
• •••amI(…)
amI(…)
•• amI who
•• • • {{ amI
••
•• amI();
amI(); ••
• •amI();
amI();
•
}} • • • ••
}} •• amI amI
•• amI();
amI();
}} •• %ebp
•• amI amI
}}
%esp
35
University of Texas at Austin
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…)
•{ yoo
yoo
•{ amI(…) who
•• ••amI(…)
•• ••
{{
amI();
who();
amI();
who(); • who
•• •• ••• •• amI amI
••
•• amI();
amI();
•• •amI(); %ebp
}} •amI();
••
}} •• amI amI
•• %esp
}}
amI
36
University of Texas at Austin
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…)
•{ yoo
yoo
•{ who
•• •• •• •• %ebp
amI();
who();
amI();
who();
•• •• •• •• amI amI who
%esp
•• amI();
amI();
}} •• •• ••
}} amI
amI
37
University of Texas at Austin
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…)
•{ yoo
yoo
•{ amI(…) who
•• ••amI(…)
•• ••
{{
amI();
who();
amI();
who(); • who
•• •• ••• •• amI amI
••
•• amI();
amI();
•• •amI(); %ebp
}} •amI();
••
}} •• amI amI
•• %esp
}}
amI
38
University of Texas at Austin
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…)
•{ yoo
yoo
•{ who
•• •• •• •• %ebp
amI();
who();
amI();
who();
•• •• •• •• amI amI who
%esp
•• amI();
amI();
}} •• •• ••
}} amI
amI
39
University of Texas at Austin
Example Stack
yoo
yoo(…) %ebp
yoo(…)
{{ yoo
yoo
•• who %esp
••
who();
who(); amI amI
••
••
}} amI
amI
40
University of Texas at Austin
Resulting
void
void swap(int
swap(int *xp,
*xp, int
int *yp)
*yp) • Stack
{{ •
int
int t0
t0 == *xp;
*xp; •
int
int t1
t1 == *yp;
*yp; %esp
*xp
*xp == t1;
t1; &course2 subl
*yp
*yp == t0;
t0;
}} &course1 %esp
call
Rtn adr %esp
42
University of Texas at Austin
Revisiting swap
swap:
pushl %ebp
void Set
void swap(int
swap(int *xp,
*xp, int
int *yp)
*yp) movl %esp, %ebp
{{ pushl %ebx Up
int
int t0
t0 == *xp;
*xp;
int
int t1
t1 == *yp;
*yp; movl 8(%ebp), %edx
*xp
*xp == t1;
t1; movl 12(%ebp), %ecx
*yp
*yp == t0;
t0; movl (%edx), %ebx Body
}} movl (%ecx), %eax
movl %eax, (%edx)
movl %ebx, (%ecx)
popl %ebx
popl %ebp Finish
ret
43
University of Texas at Austin
swap Setup #1
Entering Stack Resulting Stack
%ebp %ebp
• •
• •
• •
&course2 yp
&course1 xp
Rtn adr %esp Rtn adr
Old %ebp %esp
swap:
pushl %ebp
movl %esp,%ebp
pushl %ebx
44
University of Texas at Austin
swap Setup #2
Entering Stack Resulting Stack
%ebp
• •
• •
• •
&course2 yp
&course1 xp
Rtn adr %esp Rtn adr
%ebp
Old %ebp %esp
swap:
pushl %ebp
movl %esp,%ebp
pushl %ebx
45
University of Texas at Austin
swap Setup #3
Entering Stack Resulting Stack
%ebp
• •
• •
• •
&course2 yp
&course1 xp
Rtn adr %esp Rtn adr
Old %ebp %ebp
Old %ebx %esp
swap:
pushl %ebp
movl %esp,%ebp
pushl %ebx
46
University of Texas at Austin
swap Body
Entering Stack Resulting Stack
%ebp
• •
• •
• •
Offset relative to %ebp
&course2 12 yp
&course1 8 xp
Rtn adr %esp 4 Rtn adr
Old %ebp %ebp
Old %ebx %esp
47
University of Texas at Austin
swap Finish
Stack Before Finish Resulting Stack
%ebp
• •
• •
popl %ebx
• •
popl %ebp
yp yp
xp xp
Rtn adr Rtn adr %esp
Old %ebp %ebp
Old %ebx %esp Observation
Saved and restored register %ebx
Not so for %eax, %ecx, %edx
48
University of Texas at Austin
Disassembled swap
08048384 <swap>:
8048384: 55 push %ebp
8048385: 89 e5 mov %esp,%ebp
8048387: 53 push %ebx
8048388: 8b 55 08 mov 0x8(%ebp),%edx
804838b: 8b 4d 0c mov 0xc(%ebp),%ecx
804838e: 8b 1a mov (%edx),%ebx
8048390: 8b 01 mov (%ecx),%eax
8048392: 89 02 mov %eax,(%edx)
8048394: 89 19 mov %ebx,(%ecx)
8048396: 5b pop %ebx
8048397: 5d pop %ebp
8048398: c3 ret
Calling Code
80483b4: movl $0x8049658,0x4(%esp) # Copy &course2
80483bc: movl $0x8049654,(%esp) # Copy &course1
80483c3: call 8048384 <swap> # Call swap
80483c8: leave # Prepare to return
80483c9: ret # Return
49
University of Texas at Austin
Today
• Switch statements
• IA 32 Procedures
• Stack Structure
• Calling Conventions
• Illustrations of Recursion & Pointers
50
University of Texas at Austin
52
University of Texas at Austin
Today
• Switch statements
• IA 32 Procedures
• Stack Structure
• Calling Conventions
• Illustrations of Recursion & Pointers
54
University of Texas at Austin
Recursive Functionpcount_r:
pushl %ebp
/*
/* Recursive
Recursive popcount
popcount */
*/ movl %esp, %ebp
int
int pcount_r(unsigned
pcount_r(unsigned x) x) {{ pushl %ebx
if
if (x
(x ==
== 0)
0) subl $4, %esp
return
return 0;0; movl 8(%ebp), %ebx
else
else return
return movl $0, %eax
(x
(x && 1)
1) ++ pcount_r(x
pcount_r(x >>
>> 1);
1); testl %ebx, %ebx
}} je .L3
movl %ebx, %eax
shrl %eax
movl %eax, (%esp)
• Registers call pcount_r
• %eax, %edx used without movl %ebx, %edx
andl $1, %edx
first saving leal (%edx,%eax), %eax
• %ebx used, but saved at .L3:
addl $4, %esp
beginning & restored at end popl %ebx
popl %ebp
ret
55
University of Texas at Austin
pcount_r:
Recursive Call #1 pushl %ebp
movl %esp, %ebp
/* pushl %ebx
/* Recursive
Recursive popcount
popcount */
*/
int subl $4, %esp
int pcount_r(unsigned
pcount_r(unsigned x) x) {{
if movl 8(%ebp), %ebx
if (x
(x ==
== 0)
0)
return • • •
return 0;0;
else
else return
return
(x
(x && 1)
1) ++ pcount_r(x
pcount_r(x >>
>> 1);
1);
}}
•
•
•
• Actions
• Save old value of %ebx on stack
x
• Allocate space for argument to
Rtn adr
recursive call
Old %ebp %ebp
• Store x in %ebx
Old %ebx
%ebx x %esp
56
University of Texas at Austin
Recursive Call #2
/*
/* Recursive
Recursive popcount
popcount */
*/ • • •
int
int pcount_r(unsigned
pcount_r(unsigned x) x) {{
movl $0, %eax
if
if (x
(x ==
== 0)
0) testl %ebx, %ebx
return
return 0;0; je .L3
else
else return
return • • •
(x
(x && 1)
1) ++ pcount_r(x
pcount_r(x >>
>> 1);
1); .L3:
}}
• • •
ret
• Actions
• If x == 0, return
• with %eax set to 0
%ebx x
57
University of Texas at Austin
Recursive Call #3
/*
/* Recursive
Recursive popcount
popcount */
*/ • • •
int
int pcount_r(unsigned
pcount_r(unsigned x) x) {{ movl %ebx, %eax
if
if (x
(x ==
== 0)
0) shrl %eax
return
return 0;0; movl %eax, (%esp)
else
else return
return call pcount_r
(x
(x && 1)
1) ++ pcount_r(x
pcount_r(x >>
>> 1);
1); • • •
}}
• Actions •
• Store x >> 1 on stack •
•
• Make recursive call
• Effect
Rtn adr
• %eax set to function result
Old %ebp %ebp
• %ebx still has value of x
Old %ebx
x >> 1 %esp
%ebx x
58
University of Texas at Austin
Recursive Call #4
/*
/* Recursive
Recursive popcount
popcount */
*/ • • •
int
int pcount_r(unsigned
pcount_r(unsigned x) x) {{ movl %ebx, %edx
if
if (x
(x ==
== 0)
0) andl $1, %edx
return
return 0;0; leal (%edx,%eax), %eax
else
else return
return • • •
(x
(x && 1)
1) ++ pcount_r(x
pcount_r(x >>
>> 1);
1);
}}
• Assume
• %eax holds value from recursive call
• %ebx holds x
• Actions %ebx x
• Compute (x & 1) + computed value
• Effect
• %eax set to function result
59
University of Texas at Austin
Recursive Call #5
/* • • •
/* Recursive
Recursive popcount
popcount */
*/
int L3:
int pcount_r(unsigned
pcount_r(unsigned x) x) {{
if addl$4, %esp
if (x
(x ==
== 0)
0)
return popl%ebx
return 0;0;
else popl%ebp
else return
return
(x ret
(x && 1)
1) ++ pcount_r(x
pcount_r(x >>
>> 1);
1);
}}
%ebp
• Actions • •
• Restore values • •
• •
of %ebx and %esp
%ebp Rtn adr
• Restore %esp Old %ebp %ebp
%ebx
Old %ebx
%esp Old %ebx
60
University of Texas at Austin
Pointer Code
Generating Pointer
/*
/* Compute
Compute xx ++ 33 */
*/
int
int add3(int
add3(int x)x) {{
int
int localx
localx == x;x;
incrk(&localx,
incrk(&localx, 3); 3);
return
return localx;
localx;
}}
Referencing Pointer
/*
/* Increment
Increment value
value by
by kk */
*/
void
void incrk(int
incrk(int *ip,
*ip, int
int k)
k) {{
*ip
*ip +=
+= k;
k;
}}
8 x
4 Rtn adr
0 Old %ebp %ebp
Middle part of add3
-4 localx
movl
movl $3,
$3, 4(%esp)
4(%esp) ## 22ndnd arg
arg == 33
leal
leal -4(%ebp),
-4(%ebp), %eax#
%eax# &localx
&localx -8
movl
movl %eax,
%eax, (%esp)
(%esp) ## 11stst arg
arg == &localx
&localx -12 Unused
call
call incrk
incrk -16
-20 3 %esp+4
-24 %esp
64
University of Texas at Austin
8 x
4 Rtn adr
0 Old %ebp %ebp
Final part of add3
-4 localx
movl -4(%ebp), %eax # Return val= localx
movl -4(%ebp), %eax # Return val= localx
leave
leave -8
ret
ret -12 Unused
-16
-20
-24 %esp
65
University of Texas at Austin
IA 32 Procedure Summary
• Important Points
• Stack is the right data structure for
Caller
procedure call / return Frame
• If P calls Q, then Q returns before P Arguments
• Recursion (& mutual recursion) Return Addr
handled by normal calling conventions %ebp Old %ebp