comp40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq assembly instructions: int times16(...

64
COMP40 Assembly Language (continued...)

Upload: others

Post on 25-Mar-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

COMP40Assembly Language (continued...)

Page 2: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

Thesmallexample:

int times16( int i ) {

return i * 16; }

89 f8 c1 e0 04 c3

Actualmachineinstructions:

mov %edi, %eax shl $0x4, %eax retq

Assemblyinstructions:

Page 3: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

mov %edi, %eax shl $0x4, %eax retq

Assemblyinstructions:

int times16( int i ) {

return i * 16; }

89 f8 c1 e0 04 c3

Actualmachineinstructions:

Thesmallexample:

Page 4: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

mov %edi, %eax shl $0x4, %eax retq

Assemblyinstructions:

int times16( int i ) {

return i * 16; }

89 f8 c1 e0 04 c3

Actualmachineinstructions:

Thesmallexample:

Page 5: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

mov %edi, %eax shl $0x4, %eax retq

Assemblyinstructions:

int times16( int i ) {

return i * 16; }

89 f8 c1 e0 04 c3

Actualmachineinstructions:

Thesmallexample:

Page 6: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

mov %edi, %eax shl $0x4, %eax retq

Assemblyinstructions:

int times16( int i ) {

return i * 16; }

89 f8 c1 e0 04 c3

Actualmachineinstructions:

Thesmallexample:

Page 7: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

mov %edi, %eax shl $0x4, %eax retq

Assemblyinstructions:

int times16( int i ) {

return i * 16; }

89 f8 c1 e0 04 c3

Actualmachineinstructions:

Thesmallexample:

Page 8: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

1.)Addressingmodes

3.)Controlflow

2.)Workingthestack

Page 9: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

1.)Addressingmodes

3.)Controlflow

2.)Workingthestack

Page 10: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

Addressingmodes:

%rdi // contents of rdi is data

Inuse: movq %rdi, %rax Assume%rdiholdsvariablexand%raxholdsy,thenhisimplements:

long x, y; y = x;

movq “quad” length move b → byte w → 2 bytes (short) l → long (4 bytes not 8!) q → quad (8 bytes)

Page 11: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

Addressingmodes:

(%rax) // data pointed to by rax 0x10(%rax) // get *(16 + rax) $0x4089a0(, %rax, 8) // Global array index of 8-byte things (%ebx, %ecx, 4) // Add base and scaled index 4(%ebx, %ecx, 2) // Add base and scaled index plus offset 4

%rdi // contents of rdi is data

Inuse: movq (%rax), %rbx Assume%raxholdsvariable&xand%rbxholdsy,thenthisimplements:

long *xp = &x; long y; y = *xp;

The source or the target can reference memory…but not both. Copying from memory to memory takes two instructions (into register and back out).

Page 12: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

Addressingmodes:

(%rax) // data pointed to by rax 0x10(%rax) // get *(16 + rax) $0x4089a0(, %rax, 8) // Global array index of 8-byte things (%ebx, %ecx, 4) // Add base and scaled index 4(%ebx, %ecx, 2) // Add base and scaled index plus offset 4

%rdi // contents of rdi is data

Page 13: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

(%rax) // data pointed to by rax 0x10(%rax) // get *(16 + rax) $0x4089a0(, %rax, 8) // Global array index of 8-byte things (%ebx, %ecx, 4) // Add base and scaled index 4(%ebx, %ecx, 2) // Add base and scaled index plus offset 4

Addressingmodes:

%rdi // contents of rdi is data

Inuse: movq 0x10(%rax), %rbx struct s { long m1, m2, m3, m4) } *sp

long y = sp -> m3;

Yes, (0x10 %rax) probably feels more logical here... but alas.

Page 14: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

(%rax) // data pointed to by rax 0x10(%rax) // get *(16 + rax) $0x4089a0(, %rax, 8) // Global array index of 8-byte things (%ebx, %ecx, 4) // Add base and scaled index 4(%ebx, %ecx, 2) // Add base and scaled index plus offset 4

Addressingmodes:

%rdi // contents of rdi is data

Page 15: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

(%rax) // data pointed to by rax 0x10(%rax) // get *(16 + rax) $0x4089a0(, %rax, 8) // Global array index of 8-byte things (%ebx, %ecx, 4) // Add base and scaled index 4(%ebx, %ecx, 2) // Add base and scaled index plus offset 4

Addressingmodes:

%rdi // contents of rdi is data

Inuse: movq 0x4089a0(,%rax, 8), %rbx long arr[1000]; /* assume at 0x4089a0 */ long i; /* array index in %rax*/ long y; /* move target in %rbx */ y = arr[i]; /* all in one assembler mov! */

Page 16: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

(%rax) // data pointed to by rax 0x10(%rax) // get *(16 + rax) $0x4089a0(, %rax, 8) // Global array index of 8-byte things (%ebx, %ecx, 4) // Add base and scaled index 4(%ebx, %ecx, 2) // Add base and scaled index plus offset 4

Addressingmodes:

%rdi // contents of rdi is data

Page 17: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

(%rax) // data pointed to by rax 0x10(%rax) // get *(16 + rax) $0x4089a0(, %rax, 8) // Global array index of 8-byte things (%ebx, %ecx, 4) // Add base and scaled index 4(%ebx, %ecx, 2) // Add base and scaled index plus offset 4

Addressingmodes:

%rdi // contents of rdi is data

Inuse: movl (%ebx,%ecx, 4), %edx int *ebx; int edx = ebx[ecx]

// edx <- *(ebx + (ecx * 4))

Page 18: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

(%rax) // data pointed to by rax 0x10(%rax) // get *(16 + rax) $0x4089a0(, %rax, 8) // Global array index of 8-byte things (%ebx, %ecx, 4) // Add base and scaled index 4(%ebx, %ecx, 2) // Add base and scaled index plus offset 4

Addressingmodes:

%rdi // contents of rdi is data

Inuse: movl (%ebx,%ecx, 4), %edx // edx <- *(ebx + (ecx * 4)) leal (%ebx,%ecx, 4), %edx // edx <- (ebx + (ecx * 4))

int *ebx; int *edx = &(ebx[ecx])

int x, y, z; z = x + (y * 4)

OR

Page 19: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

(%rax) // data pointed to by rax 0x10(%rax) // get *(16 + rax) $0x4089a0(, %rax, 8) // Global array index of 8-byte things (%ebx, %ecx, 4) // Add base and scaled index 4(%ebx, %ecx, 2) // Add base and scaled index plus offset 4

Addressingmodes:

%rdi // contents of rdi is data

Inuse: movb (%bx,%cx, 1), %dx // dx <- *(bx + (cx * 1))

leab (%bx,%cx, 1), %dx // dx <- (bx + (cx * 1))

char *bx; char *dx = &(bx[cx])

Page 20: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

(%rax) // data pointed to by rax 0x10(%rax) // get *(16 + rax) $0x4089a0(, %rax, 8) // Global array index of 8-byte things (%ebx, %ecx, 4) // Add base and scaled index 4(%ebx, %ecx, 2) // Add base and scaled index plus offset 4

Addressingmodes:

%rdi // contents of rdi is data

Page 21: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

(%rax) // data pointed to by rax 0x10(%rax) // get *(16 + rax) $0x4089a0(, %rax, 8) // Global array index of 8-byte things (%ebx, %ecx, 4) // Add base and scaled index 4(%ebx, %ecx, 2) // Add base and scaled index plus offset 4

Addressingmodes:

%rdi // contents of rdi is data

Inuse: movl 4(%ebx,%ecx, 2), %edx // edx <- *(4 + ebx + (ecx * 2))

Page 22: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

A( B, C, D)

A + B + (C * D))

=

Page 23: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

A( B, C, D)

A + B + (C * D))

=

Page 24: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

A( B, C, D)

A + B + (C * D))

=

Page 25: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

A( B, C, D)

A + B + (C * D))

=

Page 26: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

A( B, C, D)

A + B + (C * D))

=

Page 27: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

A( B, C, D)

A + B + (C * D))

=

Page 28: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

1.)Addressingmodes

3.)Controlflow

2.)Workingthestack

Page 29: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

1.)Addressingmodes

3.)Controlflow

2.)Workingthestack

Stack

Text(code)

Staticinitialized

Staticuninitialized

Heap(malloc’d)

argv,environ

0

7fffffffffff

Page 30: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

1.)Addressingmodes

3.)Controlflow

2.)Workingthestack

Stack

argv,environ

0

7fffffffffff

arg3

arg2

arg1Functioncallismade

returnaddress%esp

Page 31: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

int arith(int x, int y, int z) {

int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48;

int t5 = t3 + t4; int rval = t2 * t5;

return rval; }

arith: pushl %ebp movl %esp,%ebp

movl 12(%ebp),%eax movl 16(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 20(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax

movl %ebp,%esp popl %ebp ret

Page 32: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

arith: pushl %ebp movl %esp,%ebp

movl 12(%ebp),%eax movl 16(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 20(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax

movl %ebp,%esp popl %ebp ret

int arith(int x, int y, int z) {

int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48;

int t5 = t3 + t4; int rval = t2 * t5;

return rval; }

arithneedstouseregister%ebp,sowepushitsvalueontothestackandrestoreitlater.

Page 33: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

Stack

argv,environ

0

7fffffffffff

arg3

arg2

arg1

returnaddress%esp

%ebp

Page 34: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

arith: pushl %ebp movl %esp,%ebp

movl 12(%ebp),%eax movl 16(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 20(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax

movl %ebp,%esp popl %ebp ret

int arith(int x, int y, int z) {

int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48;

int t5 = t3 + t4; int rval = t2 * t5;

return rval; }

Page 35: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

arith: pushl %ebp movl %esp,%ebp

movl 12(%ebp),%eax movl 16(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 20(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax

movl %ebp,%esp popl %ebp ret

int arith(int x, int y, int z) {

int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48;

int t5 = t3 + t4; int rval = t2 * t5;

return rval; }

Nowwecanuse%ebp,asamarkofwhereourstackpointerstarted

Page 36: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

%ebp =

Stack

argv,environ

0

7fffffffffff

arg3

arg2

arg1

returnaddress

%esp %ebp

Page 37: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

arith: pushl %ebp movl %esp,%ebp

movl 12(%ebp),%eax movl 16(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 20(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax

movl %ebp,%esp popl %ebp ret

int arith(int x, int y, int z) {

int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48;

int t5 = t3 + t4; int rval = t2 * t5;

return rval; }

Page 38: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

arith: pushl %ebp movl %esp,%ebp

movl 12(%ebp),%eax movl 16(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 20(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax

movl %ebp,%esp popl %ebp ret

int arith(int x, int y, int z) {

int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48;

int t5 = t3 + t4; int rval = t2 * t5;

return rval; }

Page 39: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

%ebp =

Stack

argv,environ

0

7fffffffffff

arg3

arg2

arg1

returnaddress

%ebp +4

+12

Page 40: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

%ebp =

Stack

argv,environ

0

7fffffffffff

arg3

arg2

arg1

returnaddress

%ebp +4

+12

Page 41: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

arith: pushl %ebp movl %esp,%ebp

movl 12(%ebp),%eax movl 16(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 20(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax

movl %ebp,%esp popl %ebp ret

int arith(int x, int y, int z) {

int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48;

int t5 = t3 + t4; int rval = t2 * t5;

return rval; }

Page 42: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

arith: pushl %ebp movl %esp,%ebp

movl 12(%ebp),%eax movl 16(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 20(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax

movl %ebp,%esp popl %ebp ret

int arith(int x, int y, int z) {

int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48;

int t5 = t3 + t4; int rval = t2 * t5;

return rval; }

// %eax = x

Page 43: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

arith: pushl %ebp movl %esp,%ebp

movl 12(%ebp),%eax movl 16(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 20(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax

movl %ebp,%esp popl %ebp ret

int arith(int x, int y, int z) {

int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48;

int t5 = t3 + t4; int rval = t2 * t5;

return rval; }

// %eax = x // %edx = y

Page 44: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

arith: pushl %ebp movl %esp,%ebp

movl 12(%ebp),%eax movl 16(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 20(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax

movl %ebp,%esp popl %ebp ret

int arith(int x, int y, int z) {

int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48;

int t5 = t3 + t4; int rval = t2 * t5;

return rval; }

// %eax = x // %edx = y

// %ecx = x+y

Page 45: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

int arith(int x, int y, int z) {

int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48;

int t5 = t3 + t4; int rval = t2 * t5;

return rval; }

arith: pushl %ebp movl %esp,%ebp

movl 12(%ebp),%eax movl 16(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 20(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax

movl %ebp,%esp popl %ebp ret

// %eax = x // %edx = y

// %ecx = x+y // %edx = y*3

Page 46: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

arith: pushl %ebp movl %esp,%ebp

movl 12(%ebp),%eax movl 16(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 20(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax

movl %ebp,%esp popl %ebp ret

int arith(int x, int y, int z) {

int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48;

int t5 = t3 + t4; int rval = t2 * t5;

return rval; }

// %eax = x // %edx = y

// %ecx = x+y // %edx = y*3 // %edx = y*16

Page 47: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

arith: pushl %ebp movl %esp,%ebp

movl 12(%ebp),%eax movl 16(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 20(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax

movl %ebp,%esp popl %ebp ret

int arith(int x, int y, int z) {

int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48;

int t5 = t3 + t4; int rval = t2 * t5;

return rval; }

// %eax = x // %edx = y

// %ecx = x+y // %edx = y*3 // %edx = y*16 // %ecx = z+t1

Page 48: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

int arith(int x, int y, int z) {

int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48;

int t5 = t3 + t4; int rval = t2 * t5;

return rval; }

arith: pushl %ebp movl %esp,%ebp

movl 12(%ebp),%eax movl 16(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 20(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax

movl %ebp,%esp popl %ebp ret

// %eax = x // %edx = y

// %ecx = x+y // %edx = y*3 // %edx = y*16 // %ecx = z+t1

// %eax = 4+t4+x

Page 49: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

int arith(int x, int y, int z) {

int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48;

int t5 = t3 + t4; int rval = t2 * t5;

return rval; }

arith: pushl %ebp movl %esp,%ebp

movl 12(%ebp),%eax movl 16(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 20(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax

movl %ebp,%esp popl %ebp ret

// %eax = x // %edx = y

// %ecx = x+y // %edx = y*3 // %edx = y*16 // %ecx = z+t1

// %eax = 4+t4+x // %eax = t2*t5

Page 50: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

int arith(int x, int y, int z) {

int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48;

int t5 = t3 + t4; int rval = t2 * t5;

return rval; }

arith: pushl %ebp movl %esp,%ebp

movl 12(%ebp),%eax movl 16(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 20(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax

movl %ebp,%esp popl %ebp ret

// %eax = x // %edx = y

// %ecx = x+y // %edx = y*3 // %edx = y*16 // %ecx = z+t1

// %eax = 4+t4+x // %eax = t2*t5

Page 51: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

%esp =

Stack

argv,environ

0

7fffffffffff

arg3

arg2

arg1

returnaddress

%ebp %ebp

Page 52: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

Stack

argv,environ

0

7fffffffffff

arg3

arg2

arg1

returnaddress

%esp =

Page 53: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

Stack

argv,environ

0

7fffffffffff

arg3

arg2

arg1

%esp =

Whenafunctioniscalled,thestackpointermustbeamultipleof16. returnaddress

Page 54: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

Stack

argv,environ

0

7fffffffffff

arg3

arg2

arg1%esp = Whenafunctioniscalled,thestack

pointermustbeamultipleof16. returnaddress

wastedspace

notamultipleof16!

Page 55: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

Stack

argv,environ

0

7fffffffffff

arg3

arg2

arg1

%esp =

Whenafunctioniscalled,thestackpointermustbeamultipleof16.

sub $8,%rsp

returnaddress

wastedspace

add $8,%rsp

...

Page 56: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

1.)Addressingmodes

3.)Controlflow

2.)Workingthestack

Page 57: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

1.)Addressingmodes

3.)Controlflow

2.)Workingthestack

Page 58: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

Controlflow:

void ifelse(int a, int b) {

if (a > b) func1(a); else func2(b);

}

ifelse: subq $8, %rsp cmpl %esi, %edi jle .L2 call func1 jmp .L1

.L2: movl %esi, %edi call func2

.L1: addq $8, %rsp ret

Page 59: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

ifelse: subq $8, %rsp cmpl %esi, %edi jle .L2 call func1 jmp .L1

.L2: movl %esi, %edi call func2

.L1: addq $8, %rsp ret

Controlflow:

void ifelse(int a, int b) {

if (a > b) func1(a); else func2(b);

}

Page 60: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

ifelse: subq $8, %rsp cmpl %esi, %edi jle .L2 call func1 jmp .L1

.L2: movl %esi, %edi call func2

.L1: addq $8, %rsp ret

void ifelse(int a, int b) {

if (a > b) func1(a); else func2(b);

}

Controlflow:

Page 61: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

void ifelse(int a, int b) {

if (a > b) func1(a); else func2(b);

}

ifelse: subq $8, %rsp cmpl %esi, %edi jle .L2 call func1 jmp .L1

.L2: movl %esi, %edi call func2

.L1: addq $8, %rsp ret

Controlflow:

Buthowdoweknowtheresultofthiscompare?

Page 62: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

Theflagsregistertrackscomparisons:

62

Example:ZeroflagsetiflastresultiszeroorlastcompareisequalConditionaljumpsandmovestesttheflags

Page 63: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

ifelse: subq $8, %rsp cmpl %esi, %edi jle .L2 call func1 jmp .L1

.L2: movl %esi, %edi call func2

.L1: addq $8, %rsp ret

void ifelse(int a, int b) {

if (a > b) func1(a); else func2(b);

}

Controlflow:

Page 64: COMP40 · 2020. 9. 29. · mov %edi, %eax shl $0x4, %eax retq Assembly instructions: int times16( int i ) { return i * 16; } 89 f8 c1 e0 04 c3 Actual machine instructions: The small

Summary

•  Ccodecompiledtoassembler

•  Datamovedtoregistersformanipulation

•  Conditionalandjumpinstructionsforcontrolflow

•  Stackusedforfunctioncalls

•  Compilersplayallsortsoftrickswhencompilingcode