Roadmap: Java: C
Roadmap: Java: C
Computer
system:
%eax accumulate
%ecx counter
general purpose
%edx data
%ebx base
source
%esi index
destination
%edi index
stack
%esp pointer
base
%ebp pointer
32-bits wide
Autumn 2013 x86 Programming 4
University of Washington
source
%esi %si index
destination
%edi %di index
stack
%esp %sp pointer
base
%ebp %bp
pointer
§ Extend existing registers, and add 8 new ones; all accessible as 8, 16, 32, 64 bits.
Autumn 2013 x86 Programming 6
University of Washington
movl (%ecx),%eax
movl 8(%ebp),%edx
movl -4(%ebp),%ebx
movl %ebp,%esp
popl %ebp Finish
ret
Understanding Swap
higher addresses
void swap(int *xp, int *yp) •
{ • Stack
int t0 = *xp; • (in memory)
Offset
int t1 = *yp;
*xp = t1; 12 yp
*yp = t0;
lower addresses
8 xp
}
4 Rtn adr
0 Old %ebp %ebp
-4 Old %ebx
Register Value
%ecx yp movl 12(%ebp),%ecx # ecx = yp
%edx xp movl 8(%ebp),%edx # edx = xp
%eax t1 movl (%ecx),%eax # eax = *yp (t1)
%ebx t0 movl (%edx),%ebx # ebx = *xp (t0)
register <-> variable movl %eax,(%edx) # *xp = eax
mapping movl %ebx,(%ecx) # *yp = ebx
Autumn 2013 x86 Programming 15
University of Washington
Address
Understanding Swap 123 0x124
higher addresses
456 0x120
0x11c
%eax 0x118
%edx Offset
0x114
%ecx yp 12 0x120 0x110
xp 8 0x124
lower addresses
%ebx 0x10c
4 Rtn adr 0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 12(%ebp),%ecx # ecx = yp
%ebp 0x104 movl 8(%ebp),%edx # edx = xp
movl (%ecx),%eax # eax = *yp (t1)
movl (%edx),%ebx # ebx = *xp (t0)
movl %eax,(%edx) # *xp = eax
movl %ebx,(%ecx) # *yp = ebx
Autumn 2013 x86 Programming 16
University of Washington
Address
Understanding Swap 123 0x124
456 0x120
0x11c
%eax 0x118
%edx Offset
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx
4 Rtn adr 0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 12(%ebp),%ecx # ecx = yp
%ebp 0x104 movl 8(%ebp),%edx # edx = xp
movl (%ecx),%eax # eax = *yp (t1)
movl (%edx),%ebx # ebx = *xp (t0)
movl %eax,(%edx) # *xp = eax
movl %ebx,(%ecx) # *yp = ebx
Autumn 2013 x86 Programming 17
University of Washington
Address
Understanding Swap 123 0x124
456 0x120
0x11c
%eax 0x118
%edx 0x124 Offset
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx
4 Rtn adr 0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 12(%ebp),%ecx # ecx = yp
%ebp 0x104 movl 8(%ebp),%edx # edx = xp
movl (%ecx),%eax # eax = *yp (t1)
movl (%edx),%ebx # ebx = *xp (t0)
movl %eax,(%edx) # *xp = eax
movl %ebx,(%ecx) # *yp = ebx
Autumn 2013 x86 Programming 18
University of Washington
Address
Understanding Swap 123 0x124
456 0x120
0x11c
%eax 456 0x118
%edx 0x124 Offset
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx
4 Rtn adr 0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 12(%ebp),%ecx # ecx = yp
%ebp 0x104 movl 8(%ebp),%edx # edx = xp
movl (%ecx),%eax # eax = *yp (t1)
movl (%edx),%ebx # ebx = *xp (t0)
movl %eax,(%edx) # *xp = eax
movl %ebx,(%ecx) # *yp = ebx
Autumn 2013 x86 Programming 19
University of Washington
Address
Understanding Swap 123 0x124
456 0x120
0x11c
%eax 456 0x118
%edx 0x124 Offset
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx 123
4 Rtn adr 0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 12(%ebp),%ecx # ecx = yp
%ebp 0x104 movl 8(%ebp),%edx # edx = xp
movl (%ecx),%eax # eax = *yp (t1)
movl (%edx),%ebx # ebx = *xp (t0)
movl %eax,(%edx) # *xp = eax
movl %ebx,(%ecx) # *yp = ebx
Autumn 2013 x86 Programming 20
University of Washington
Address
Understanding Swap 456 0x124
456 0x120
0x11c
%eax 456
456 0x118
%edx 0x124 Offset
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx 123
4 Rtn adr 0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 12(%ebp),%ecx # ecx = yp
%ebp 0x104 movl 8(%ebp),%edx # edx = xp
movl (%ecx),%eax # eax = *yp (t1)
movl (%edx),%ebx # ebx = *xp (t0)
movl %eax,(%edx) # *xp = eax
movl %ebx,(%ecx) # *yp = ebx
Autumn 2013 x86 Programming 21
University of Washington
Address
Understanding Swap 456 0x124
123 0x120
0x11c
%eax 456 0x118
%edx 0x124 Offset
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx 123
4 Rtn adr 0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 12(%ebp),%ecx # ecx = yp
%ebp 0x104 movl 8(%ebp),%edx # edx = xp
movl (%ecx),%eax # eax = *yp (t1)
movl (%edx),%ebx # ebx = *xp (t0)
movl %eax,(%edx) # *xp = eax
movl %ebx,(%ecx) # *yp = ebx
Autumn 2013 x86 Programming 22
University of Washington
§ Extend existing registers, and add 8 new ones; all accessible as 8, 16, 32, 64 bits.
Autumn 2013 x86 Programming 23
University of Washington
¢ 64-bit data
§ Data held in registers %rax and %rdx
§ movq operation
§ “q” stands for quad-word
¢ Uses
§ Computing addresses without a memory reference
§ E.g., translation of p = &x[i];
§ Computing arithmetic expressions of the form x + k*i
§ k = 1, 2, 4, or 8
Autumn 2013
compiler optimization
x86 Programming 42
University of Washington
Jumping
¢ jX Instructions
§ Jump to different part of code depending on condition codes
§ Takes address as argument
jX Condition Description
jmp 1 Unconditional
je ZF Equal / Zero
jne ~ZF Not Equal / Not Zero
js SF Negative
jns ~SF Nonnegative
jg ~(SF^OF)&~ZF Greater (Signed)
jge ~(SF^OF) Greater or Equal (Signed)
jl (SF^OF) Less (Signed)
jle (SF^OF)|ZF Less or Equal (Signed)
ja ~CF&~ZF Above (unsigned)
jb CF Below (unsigned)
Autumn 2013 x86 Programming 45
University of Washington
Jumping
¢ jX Instructions
§ Jump to different part of code depending on condition codes
§ Takes address as argument
jX Condition Description
jmp 1 Unconditional
je ZF Equal / Zero
jne ~ZF Not Equal / Not Zero
js SF Negative
jns ~SF Nonnegative
jg ~(SF^OF)&~ZF Greater (Signed)
jge ~(SF^OF) Greater or Equal (Signed)
jl (SF^OF) Less (Signed)
jle (SF^OF)|ZF Less or Equal (Signed)
ja ~CF&~ZF Above (unsigned)
jb CF Below (unsigned)
Autumn 2013 x86 Programming 53
University of Washington
Conditionals: x86-64
int absdiff( absdiff: # x in %edi, y in %esi
int x, int y) movl %edi, %eax # eax = x
{ movl %esi, %edx # edx = y
int result; subl %esi, %eax # eax = x-y
if (x > y) { subl %edi, %edx # edx = y-x
result = x-y; cmpl %esi, %edi # x:y
} else { cmovle %edx, %eax # eax=edx if <=
result = y-x; ret
}
return result;
}
Conditionals: x86-64
int absdiff( absdiff: # x in %edi, y in %esi
int x, int y) movl %edi, %eax # eax = x
{ movl %esi, %edx # edx = y
int result; subl %esi, %eax # eax = x-y
if (x > y) { subl %edi, %edx # edx = y-x
result = x-y; cmpl %esi, %edi # x:y
} else { cmovle %edx, %eax # eax=edx if <=
result = y-x; ret
}
return result;
}
PC Relative Addressing
Compiling Loops
C/Java code: Machine code:
while ( sum != 0 ) { loopTop: cmpl $0, %eax
<loop body> je loopDone
} <loop body code>
jmp loopTop
loopDone:
65
University of Washington
66
University of Washington
loop: .L11:
result *= x; imull %edx,%eax # result *= x
x = x-1; decl %edx # Translation?
x--
if (x > 1) cmpl $1,%edx # Compare x : 1
goto loop; jg .L11 # if > goto loop
loop: .L11:
result *= x; imull %edx,%eax # result *= x
x = x-1; decl %edx # x--
if (x > 1) cmpl $1,%edx # Compare x : 1
goto loop; jg .L11 # if > goto loop
¢ Body: {
Statement1;
Statement2;
…
Statementn;
}
70
University of Washington
ipwr Computation
/* Compute x raised to nonnegative power p */
int ipwr_for(int x, unsigned int p)
{
int result;
for (result = 1; p != 0; p = p>>1) {
if (p & 0x1)
result *= x;
x = x*x;
}
return result;
}
General Form
for (Initialize; Test; Update)
Body
“For”® “While”
For Version
for (Initialize; Test; Update )
Body
Goto Version
Initialize;
goto middle;
loop:
While Version Body
Initialize; Update ;
while (Test ) { middle:
Body if (Test)
Update ; goto loop;
} done:
75
University of Washington
For-Loop: Compilation
for (result = 1; p != 0; p = p>>1)
For Version {
for (Initialize; Test; Update ) if (p & 0x1)
result *= x;
Body x = x*x;
}
Goto Version
result = 1;
Initialize; goto middle;
goto middle; loop:
loop: if (p & 0x1)
Body result *= x;
Update ; x = x*x;
middle: p = p >> 1;
if (Test) middle:
if (p != 0)
goto loop;
goto loop;
done: done:
76
University of Washington
•
Approximate Translation •
target = JTab[x]; •
goto target;
Targn-1: Code Block
n–1
78
University of Washington
79
University of Washington
84
University of Washington
85
University of Washington
86
University of Washington
87
University of Washington
Assembly Code
switch_eg:
. . .
ja .L61 # if > goto default
jmp *.L62(,%edx,4) # goto JTab[x]
88
University of Washington
Disassembled Targets
8048630: bb 02 00 00 00 mov $0x2,%ebx
8048635: 89 d8 mov %ebx,%eax
8048637: 5b pop %ebx
8048638: c9 leave
8048639: c3 ret
804863a: 8b 45 0c mov 0xc(%ebp),%eax
804863d: 99 cltd
804863e: f7 f9 idiv %ecx
8048640: 89 c3 mov %eax,%ebx
8048642: 01 cb add %ecx,%ebx
8048644: 89 d8 mov %ebx,%eax
8048646: 5b pop %ebx
8048647: c9 leave
8048648: c3 ret
8048649: 29 cb sub %ecx,%ebx
804864b: 89 d8 mov %ebx,%eax
804864d: 5b pop %ebx
804864e: c9 leave
804864f: c3 ret
8048650: 8b 5d 0c mov 0xc(%ebp),%ebx
8048653: 0f af d9 imul %ecx,%ebx
8048656: 89 d8 mov %ebx,%eax
8048658: 5b pop %ebx
8048659: c9 leave
804865a: c3 ret
90
University of Washington
Question
¢ Probably not:
§ Don’t want a jump table with 52001 entries for only 4 cases (too big)
§ about 200KB = 200,000 bytes
§ text of this switch statement = about 200 bytes
92
University of Washington
Quick Review
¢ x86-64 vs. IA32
%rax %eax %r8 %r8d
§ Integer registers: 16 x 64-bit vs. 8 x 32-bit %rbx %edx %r9 %r9d
§ movq, addq, … vs. movl, addl, … %rcx %ecx %r10 %r10d
%rdx %ebx %r11 %r11d
§movq -> “move quad word” or 4*16-bits %rsi %esi %r12 %r12d
93
University of Washington
Quick Review
¢ Control
§ 1-bit condition code registers CF ZF SF OF
§ Set as side effect by arithmetic instructions or by cmp, test
§ Used:
§ Read out by setx instructions (setg, setle, …)
§ Or by conditional jumps (jle .L4, je .L10, …)
§ Or by conditional moves (cmovle %edx, %eax)
¢ Arithmetic operations also set condition codes
§ subl, addl, imull, shrl, etc.
¢ Load Effective Address does NOT set condition codes
§ leal 4(%edx,%eax),%eax # eax = 4 + edx + eax
94
University of Washington
Quick Review
Do-While loop Goto Version
¢ C Code
loop:
do
Body
Body
if (Test)
while (Test);
goto loop
¢ While-Do loop
Goto Version
Do-While Version
if (!Test)
if (!Test) goto done;
While version goto done; loop:
while (Test) do Body
Body Body if (Test)
while(Test); goto loop;
done: done:
goto middle;
loop:
Body
or middle:
if (Test)
goto loop;
95
University of Washington
96