1 / 28

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: wilson2@andrew.cmu.edu Office Hours: Friday 1:30 – 3:00 Wean 52XX Cluster. Machine Model. CPU. Memory. Addresses. Registers. E I P.

garren
Download Presentation

15-213 Recitation 2 – 2/4/01

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 15-213 Recitation 2 – 2/4/01 Outline • Machine Model • Assembly Programming • Structure • Addressing Modes • L2 Practice Stuff James Wilson e-mail: wilson2@andrew.cmu.edu Office Hours: Friday 1:30 – 3:00 Wean 52XX Cluster

  2. Machine Model CPU Memory Addresses Registers E I P Object Code Program Data Data Condition Codes Instructions Stack

  3. Special Registers • %eax Return Value • %eip Instruction Pointer • %ebp Base (Stack Frame) Pointer • %esp Stack Pointer

  4. 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

  5. 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?

  6. Assembly Programming:Simple Addressing Modes Examples • (R) Mem[R] • $10(R) Mem[R + 10] • $0x10(R) Mem[R + 16]

  7. Assembly Programming:Indexed Addressing Modes Generic Form D(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]]

  8. Example 1: Simple Stuff Convert to Assembly int foo(int x, int y) { int t, u; t = 3*x-6*y; t = t - 1; u = 16*t; return -u*t; }

  9. Example 1: Answer 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 Method 1 foo: pushl %ebp movl %esp,%ebp movl 8(%ebp),%edx movl 12(%ebp),%eax movl $3, %ecx imull %ecx,%edx movl $6, %ecx imull %ecx,%eax subl %eax,%edx decl %edx movl %edx,%eax sall $4,%eax imull %edx,%eax negl %eax movl %ebp,%esp popl %ebp ret

  10. Example 2: More Simple Stuff Convert to Assembly int absdiff(int x, int y) { if (x>=y) return x-y; else return y-x; }

  11. Example 2: Answer absdiff: 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

  12. Example 3: Backwards Convert to C 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

  13. 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; }

  14. Example 4: Jump Tables Convert to Assembly 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; }

  15. .rodata .L10: .long .L4 .long .L5 .long .L6 .long .L7 .long .L8 calc: 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 Example 4: Answer (sort of)

  16. Example 4: Answer (sort of) • The real assembler output has alignment directives

  17. Example 5 A mystery function int mystery(int x, int y) is compiled into the assembly on the next page. What is the function?

  18. Example 5 A0: mystery: A1: movl 8(%ebp), %edx A2: movl $0x0, %eax A3: cmpl $0x0, 12(%ebp) A4: je .L11 A5: .L8 A6: cmpl $0x0, 12(%ebp) A7: jle .L9 A8: add %edx, %eax A9: decl 12(%ebp) A10: jmp .L10 A11:.L9 A12: sub %edx, %eax A13: incl 12(%ebp) A14:.L10 A15: compl $0x0, 12(%ebp) A16: jne .L8 A17:.L11 A18: mov %ebp, %esp A19: pop %ebp A20: ret

  19. Example 5 A0: mystery: A1: movl 8(%ebp), %edx A2: movl $0x0, %eax A3: cmpl $0x0, 12(%ebp) A4: je .L11 A5: .L8 A6: cmpl $0x0, 12(%ebp) A7: jle .L9 A8: add %edx, %eax A9: decl 12(%ebp) A10: jmp .L10 A11:.L9 A12: sub %edx, %eax A13: incl 12(%ebp) A14:.L10 A15: compl $0x0, 12(%ebp) A16: jne .L8 A17:.L11 A18: mov %ebp, %esp A19: pop %ebp A20: ret # 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 #

  20. Example 5 So 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; }

  21. 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?

  22. Challenge Problem A0: mystery2: A1: push %ebp A2: movl %esp, %ebp A3: movl 8(%ebp), %ebx A4: movl 16(%ebp), %edx A5: movl $0xffffffff, %eax A6: movl $0x0, 0xfffffff4(%ebp) A7: decl %edx A8: compl %edx, %ecx A9: jg .L13

  23. Challenge Problem (continued) A10: .L9 A11: add %edx, %ecx A12: sarl $0x1, %ecx A13: cmpl 12(%ebp), (%ebx, %ecx, 4) A14: jge .L10 A15: incl %ecx A16: movl %ecx, 0xfffffff4(%ebp) A17: jmp .L12 A18: .L10 A19: cmpl (%ebx, %ecx, 4), 12(%ebp) A20: jle .L11 A21: movl %ecx, %edx A22: decl %edx A23: jmp .L12

  24. Challenge Problem (more code) A24: .L11 A25: movl %ecx, %eax A26: jmp .L13 A27: .L12 A28: cmpl %edx, 0xfffffff4 A29: jle .L9 A30: .L13 A31: movl %ebp, %esp A32: pop %ebp A33: ret

  25. Challenge Problem Answer: Binary Search # Set up # # ebx = Array A # edx = High = N # result = -1 # Low = 0 # ecx = Mid = Low # High— # Compare Low:High # If Low > High goto Done A0: mystery2: A1: push %ebp A2: movl %esp, %ebp A3: movl 8(%ebp), %ebx A4: movl 16(%ebp), %edx A5: movl $0xffffffff, %eax A6: movl $0x0, 0xfffffff4(%ebp) A7: decl %edx A8: compl %edx, %ecx A9: jg .L13

  26. Challenge Problem (continued) A10: .L9 A11: add %edx, %ecx A12: sarl $0x1, %ecx A13: cmpl 12(%ebp), (%ebx, %ecx, 4) A14: jge .L10 A15: incl %ecx A16: movl %ecx, 0xfffffff4(%ebp) A17: jmp .L12 A18: .L10 A19: cmpl (%ebx, %ecx, 4), 12(%ebp) A20: jle .L11 A21: movl %ecx, %edx A22: decl %edx A23: 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

  27. Challenge Problem (more code) # Found: # Result = Mid # goto Done # Check: # Compare Low:High # If <=, goto Loop # Done: # # Cleanup # A24: .L11 A25: movl %ecx, %eax A26: jmp .L13 A27: .L12 A28: cmpl %edx, 0xfffffff4 A29: jle .L9 A30: .L13 A31: movl %ebp, %esp A32: pop %ebp A33: ret

  28. Challenge Problem int 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; }

More Related