21
15-213 Recitation 2 – 2/4/01 Outline • Assembly Programming Special Registers – Structure Addressing Modes Practice Assembly Trent e-mail: [email protected] Office Hours: Thursday 12:30-2:00 Wean 52XX Cluster

15-213 Recitation 2 – 2/4/01

  • Upload
    shona

  • View
    32

  • Download
    0

Embed Size (px)

DESCRIPTION

15-213 Recitation 2 – 2/4/01. Outline Assembly Programming Special Registers Structure Addressing Modes Practice Assembly. Trent e-mail: [email protected] Office Hours: Thursday 12:30-2:00 Wean 52XX Cluster. Assembly Programming: Special Registers. %eax Return Value - PowerPoint PPT Presentation

Citation preview

Page 1: 15-213 Recitation 2 – 2/4/01

15-213 Recitation 2 – 2/4/01

Outline• Assembly

Programming– Special Registers– Structure– Addressing Modes

• Practice Assembly

Trent

e-mail:

[email protected]

Office Hours:

Thursday 12:30-2:00

Wean 52XX Cluster

Page 2: 15-213 Recitation 2 – 2/4/01

Assembly Programming:Special Registers

• %eax Return Value• %eip Instruction Pointer• %ebp Base (Stack Frame) Pointer• %esp Stack Pointer

Page 3: 15-213 Recitation 2 – 2/4/01

Assembly Programming: Structure

Function Setup• Save Old Base Pointer (pushl %ebp)• Set up own base pointer (movl %esp,

%ebp)– Note that this saves the old stack pointer

• Save any registers that could be clobbered– Where?

Function Body• Operations on data, loops, function calls

Page 4: 15-213 Recitation 2 – 2/4/01

Assembly Programming: Structure

Function Cleanup• Return value placed in %eax

– What about returning larger values? (structs, doubles, etc.)

• Restore Caller’s Stack Pointer (movl %ebp, %esp)

• Restore Old Base Pointer (popl %ebp)• Return

– Where does it return to?

Page 5: 15-213 Recitation 2 – 2/4/01

Assembly Programming:Simple Addressing Modes

Examples• (R) Mem[R]• $10(R) Mem[R + 10]• $0x10(R) Mem[R + 16]

Page 6: 15-213 Recitation 2 – 2/4/01

Assembly Programming:Indexed Addressing Modes

Generic FormD(Rb, Ri, S) Mem[Reg[Rb]+S*Reg[Ri]+ D]

Examples

• (Rb,Ri) Mem[Reg[Rb]+Reg[Ri]]

• D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D]• (Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]]

Page 7: 15-213 Recitation 2 – 2/4/01

Example 1: Simple Stuff

int foo(int x, int y){ int t, u; t = 3*x-6*y; t = t - 1; u = 16*t; return -u*t;}

Convert to Assembly

Page 8: 15-213 Recitation 2 – 2/4/01

Example 1: AnswerMethod 1

foo:pushl %ebp

movl %esp,%ebp movl 8(%ebp),%edx movl 12(%ebp),%eax

movl $3, %ecximull %ecx,%edxmovl $6, %ecximull %ecx,%eaxsubl %eax,%edxdecl %edxmovl %edx,%eaxsall $4,%eaximull %edx,%eaxnegl %eax

movl %ebp,%esppopl %ebpret

Method 2

foo:

pushl %ebp

movl %esp,%ebp

movl 8(%ebp),%edx

movl 12(%ebp),%eax

leal (%edx,%edx,2),%edx

leal (%eax,%eax,2),%eax

addl %eax,%eax

subl %eax,%edx

decl %edx

movl %edx,%eax

sall $4,%eax

imull %edx,%eax

negl %eax

movl %ebp,%esp

popl %ebp

ret

Page 9: 15-213 Recitation 2 – 2/4/01

Example 2: More Simple Stuff

int absdiff(int x, int y){ if (x>=y) return x-y; else return y-x;}

Convert to Assembly

Page 10: 15-213 Recitation 2 – 2/4/01

Example 2: Answerabsdiff: pushl %ebp movl %esp,%ebp

movl 8(%ebp),%edx # edx = x movl 12(%ebp),%eax # eax = y cmpl %eax,%edx # jl .L3 # if (x<y) goto L3 subl %eax,%edx # edx = x-y movl %edx,%eax # eax = x-y jmp .L6 # goto L6.L3: subl %edx,%eax # eax = y-x

.L6: movl %ebp,%esp popl %ebp ret

Page 11: 15-213 Recitation 2 – 2/4/01

Example 3: Backwards

get_sum: pushl %ebp movl %esp,%ebp pushl %ebx

movl 8(%ebp),%ebx # ebx = 1st arg movl 12(%ebp),%ecx # ecx = 2nd arg xorl %eax,%eax # eax = 0 movl %eax,%edx # edx = 0 cmpl %edx,%ecx # jge .L4 # if (ecx >= 0) goto L4.L6: addl (%ebx,%edx,4),%eax # eax += Mem[ebx+edx*4] incl %edx # edx ++ cmpl %edx,%ecx # jl .L6 # if (ecx < edx) goto L6

.L4: popl %ebx movl %ebp,%esp popl %ebp ret

Convert to C

Page 12: 15-213 Recitation 2 – 2/4/01

Example 3: Answer

int get_sum(int * array, int size){ int sum = 0; int i=0; for (i=0; i<size; i++) sum += array[i]; return sum;}

Page 13: 15-213 Recitation 2 – 2/4/01

Example 4: Jump Tables

#include <stdio.h>

int main(int argc, char *argv[]){ int operation, x, y;

if(argc < 4) { printf("Not enough arguments\n"); exit(0); }

operation = atoi(argv[1]); x = atoi(argv[2]); y = atoi(argv[3]);

switch(operation) { case 0: printf("Sum is: %d\n", x + y); break; case 1: printf("Diff is: %d\n", x - y); break; case 2: printf("Mult is: %d\n", x * y); break; case 3: printf("Div is: %d\n", x / y); break; case 4: printf("Mod is: %d\n", x % y); default: printf("Unknown command\n"); break; }

return 0;}

Convert to Assembly

Page 14: 15-213 Recitation 2 – 2/4/01

Example 5

A mystery function

int mystery(int x, int y)

is compiled into the assembly on the next page.

What is the function?

Page 15: 15-213 Recitation 2 – 2/4/01

A0: mystery:A1: movl 8(%ebp), %edxA2: movl $0x0, %eaxA3: cmpl $0x0, 12(%ebp)A4: je .L11A5: .L8A6: cmpl $0x0, 12(%ebp)A7: jle .L9A8: add %edx, %eaxA9: decl 12(%ebp)A10: jmp .L10A11:.L9A12: sub %edx, %eaxA13: incl 12(%ebp)A14:.L10A15: compl $0x0, 12(%ebp)A16: jne .L8A17:.L11A18: mov %ebp, %espA19: pop %ebpA20: ret

Example 5

Page 16: 15-213 Recitation 2 – 2/4/01

Example 5

# Get x# Set result = 0# Compare y:0# If(y == 0) goto Done# Loop:# Compare y:0# If(y <= 0) goto Negative# result += x# y—# goto Check# Negative:# result -= x# y++# Check:# Compare y:0# If(y != 0) goto Loop# Done:## Cleanup#

A0: mystery:A1: movl 8(%ebp), %edxA2: movl $0x0, %eaxA3: cmpl $0x0, 12(%ebp)A4: je .L11A5: .L8A6: cmpl $0x0, 12(%ebp)A7: jle .L9A8: add %edx, %eaxA9: decl 12(%ebp)A10: jmp .L10A11:.L9A12: sub %edx, %eaxA13: incl 12(%ebp)A14:.L10A15: compl $0x0, 12(%ebp)A16: jne .L8A17:.L11A18: mov %ebp, %espA19: pop %ebpA20: ret

Page 17: 15-213 Recitation 2 – 2/4/01

Example 5So what is the function computing?

/* x times y, where x and y may be pos. or neg. */int multiply(int x, int y){ int result = 0; while( y != 0 ) { if (y > 0) { result += x; y--; } else { result -= x; y++; } }

return result;}

Page 18: 15-213 Recitation 2 – 2/4/01

Challenge Problem

A function with prototype

int mystery2(int *A, int x, int N)

is compiled into the assembly on the next page.

Hint: A is an array of integers, and N is the length of the array.

What is this mystery function computing?

Page 19: 15-213 Recitation 2 – 2/4/01

Challenge Problem

A0: mystery2:A1: push %ebpA2: movl %esp, %ebpA3: movl 8(%ebp), %ebxA4: movl 16(%ebp), %edxA5: movl $0xffffffff, %eaxA6: movl $0x0, 0xfffffff4(%ebp)A7: decl %edxA8: compl %edx, %ecxA9: jg .L13

Page 20: 15-213 Recitation 2 – 2/4/01

Challenge Problem (continued)

A10: .L9A11: add %edx, %ecxA12: sarl $0x1, %ecxA13: cmpl 12(%ebp), (%ebx, %ecx, 4)A14: jge .L10A15: incl %ecxA16: movl %ecx, 0xfffffff4(%ebp)A17: jmp .L12A18: .L10A19: cmpl (%ebx, %ecx, 4), 12(%ebp)A20: jle .L11A21: movl %ecx, %edxA22: decl %edxA23: jmp .L12

Page 21: 15-213 Recitation 2 – 2/4/01

Challenge Problem (more code)

A24: .L11A25: movl %ecx, %eaxA26: jmp .L13A27: .L12A28: cmpl %edx, 0xfffffff4A29: jle .L9A30: .L13A31: movl %ebp, %espA32: pop %ebpA33: ret