28
15-213 Recitation 2 – 2/4/01 Outline Machine Model • Assembly Programming – Structure Addressing Modes L2 Practice Stuff James Wilson e-mail: [email protected] Office Hours: Friday 1:30 – 3:00 Wean 52XX Cluster

15-213 Recitation 2 – 2/4/01

  • Upload
    garren

  • View
    29

  • Download
    0

Embed Size (px)

DESCRIPTION

15-213 Recitation 2 – 2/4/01. Outline Machine Model Assembly Programming Structure Addressing Modes L2 Practice Stuff. James Wilson e-mail: [email protected] Office Hours: Friday 1:30 – 3:00 Wean 52XX Cluster. Machine Model. CPU. Memory. Addresses. Registers. E I P. - PowerPoint PPT Presentation

Citation preview

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

15-213 Recitation 2 – 2/4/01

Outline• Machine Model• Assembly

Programming– Structure– Addressing Modes

• L2 Practice Stuff

James Wilson

e-mail:

[email protected]

Office Hours:

Friday 1:30 – 3:00

Wean 52XX Cluster

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

Machine Model

EIP

Registers

CPU Memory

Object CodeProgram Data

Addresses

Data

Instructions

Stack

ConditionCodes

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

Special Registers

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

Page 4: 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 5: 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 6: 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 7: 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 8: 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 9: 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 10: 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 11: 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 12: 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 %ecx,%eax # jge .L4 # if (ecx >= 0) goto L4.L6: addl (%ebx,%edx,4),%eax # eax += Mem[ebx+edx*4] incl %edx # edx ++ cmpl %ecx,%edx # jl .L6 # if (ecx < edx) goto L6

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

Convert to C

Page 13: 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 14: 15-213 Recitation 2 – 2/4/01

Example 4: Jump Tables

int calc(int operation, int x, int y){ int result;

switch(operation) { case 0: result = x + y; break; case 1: result = x – y; break; case 2: result = x * y; break; case 3: result = x / y; break; case 4: result = x % y; break;

default: result = 0; break; }

return result;}

Convert to Assembly

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

Example 4: Answer (sort of)

.rodata

.L10: .long .L4 .long .L5 .long .L6 .long .L7 .long .L8calc: pushl %ebp movl %esp,%ebp pushl %esi pushl %ebx movl 8(%ebp),%ecx movl 12(%ebp),%ebx movl 16(%ebp),%esi cmpl $4,%ecx ja .L9 jmp *.L10(,%ecx,4).L4: addl %esi,%ebx jmp .L3.L5: subl %esi,%ebx jmp .L3

.L6: imull %esi,%ebx jmp .L3.L7: movl %ebx,%eax cltd idivl %esi movl %eax,%ebx jmp .L3.L8: movl %ebx,%eax cltd idivl %esi movl %edx,%ebx jmp .L3.L9: xorl %ebx,%ebx.L3: movl %ebx,%eax popl %ebx popl %esi movl %ebp,%esp popl %ebp ret

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

Example 4: Answer (sort of)

• The real assembler output has alignment directives

Page 17: 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 18: 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 19: 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 20: 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 21: 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 22: 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 23: 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 24: 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

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

Challenge ProblemAnswer: Binary Search

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

# Set up## ebx = Array A# edx = High = N# result = -1# Low = 0# ecx = Mid = Low# High—# Compare Low:High# If Low > High goto Done

Page 26: 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

# Loop:# Mid += High# Mid /= 2# Compare A[Mid]:X# If >=, goto High# Mid++# Low = Mid# goto Check# High:# Compare X:A[Mid]# If <=, goto Found# High = Mid# High—# goto Check

Page 27: 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

# Found:# Result = Mid# goto Done# Check:# Compare Low:High# If <=, goto Loop# Done:## Cleanup#

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

Challenge Problemint binsearch(int *A, int X, int N){ int Low, Mid, High;

Low = 0; High = N - 1; while( Low <= High ) { Mid = ( Low + High ) / 2; if( A[ Mid ] < X ) Low = Mid + 1; else if( A[ Mid ] > X ) High = Mid - 1; else return Mid; /* Found */ } return -1;}