210 likes | 325 Views
15-213 Recitation 2 – 2/4/01. Outline Assembly Programming Special Registers Structure Addressing Modes Practice Assembly. Trent e-mail: mmt@cmu.edu Office Hours: Thursday 12:30-2:00 Wean 52XX Cluster. Assembly Programming: Special Registers. %eax Return Value
E N D
15-213 Recitation 2 – 2/4/01 Outline • Assembly Programming • Special Registers • Structure • Addressing Modes • Practice Assembly Trent e-mail: mmt@cmu.edu Office Hours: Thursday 12:30-2:00 Wean 52XX Cluster
Assembly Programming:Special Registers • %eax Return Value • %eip Instruction Pointer • %ebp Base (Stack Frame) Pointer • %esp Stack Pointer
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
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?
Assembly Programming:Simple Addressing Modes Examples • (R) Mem[R] • $10(R) Mem[R + 10] • $0x10(R) Mem[R + 16]
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]]
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; }
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
Example 2: More Simple Stuff Convert to Assembly int absdiff(int x, int y) { if (x>=y) return x-y; else return y-x; }
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
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 %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
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; }
Example 4: Jump Tables Convert to Assembly #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; }
Example 5 A mystery function int mystery(int x, int y) is compiled into the assembly on the next page. What is the function?
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
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 #
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; }
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?
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
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
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