Problem 4.2+(P263) E Xercise ·C.0x3005054070000000FOB018 D.0×400:6113730004000010 ·E.0x500:6362A080 2021/10/29
Problem 4.2 +(P263) • Exercise • C. 0x300:5054070000000F0B018 • D. 0x400:6113730004000010 • E. 0x500:6362A080 2021/10/29 6
Problem 4.3(P269) Write y86 code to implement a recursive sum function rSum, based on the following c code: int rSum (int aston P178 Recursive Procedures if(count <= o return o return * Start+ rSum start+1, Count-1) 2021/10/29
Problem 4.3 (P269) • Write Y86 code to implement a recursive sum function rSum, based on the following C code: int rSum(int *Start, int Count) { if (Count <= 0) return 0; return *Start + rSum(Start+1, Count-1); } 2021/10/29 7 P178 Recursive Procedures
Problem 4.3+(P269) rSum: push1 %ebp rrmovl %ebx, %eax rrmovl %esp, %ebp add%ed×,%ea irmovl 20, %eax 66 su ea×,%esp pushl yeax US %ebx call rSum mrmovl 8(%ebp), %ebx mrmovI (%ebx, %edx mrmovl 12(%ebp),%eax ‰edx.%eax add!%ea×.%ea Jmp L39 L38 irmovl $-8, %edx add %edx, %es L38:xorl%ea×,%eax irmovl $-1, %edx L39: mrmovl-24(%ebp), %ebx add %edx eax rrmovI %ebp, %esp push/ hl eax Irmove|$4,‰edx popl %ebp ret 2021/10/29
Problem 4.3+ (P269) rSum: push1 %ebp rrmovl %esp, %ebp irmovl $20, %eax subl %eax, %esp pushl %ebx mrmovl 8(%ebp), %ebx mrmovl 12(%ebp), %eax addl %eax, %eax jle L38 irmovl $-8, %edx addl %edx, %esp irmovl $-1, %edx addl %edx, %eax pushl %eax irmovel $4, %edx 2021/10/29 8 rrmovl %ebx, %eax addl %edx, %eax pushl %eax call rSum mrmovl (%ebx), %edx addl %edx, %eax jmp L39 L38: xorl %eax, %eax L39: mrmovl -24(%ebp), %ebx rrmovl %ebp, %esp popl %ebp ret
Problem 4.4(P270) int pushtesto int rval / Insert the following assembly code movl %esp, %eax #save stack pointer pushl%esp #push stack pointer popl %edx #pop it back subl edx. eax tto or 4 movl %oeax, rval set as return quay It implies that pus the original value of return rval %esp old value In our experiments, we find that the function pushtest return o W lat does this imply about the behavior of the instruction pushl %oesp under IA 32 2021/10/29
Problem 4.4 (P270) int pushtest() { int rval; /* Insert the following assembly code: movl %esp, %eax #save stack pointer pushl %esp #push stack pointer popl %edx #pop it back subl %edx, %eax # 0 or 4 movl %eax, rval # set as return value */ … return rval; } In our experiments, we find that the function pushtest return 0. What does this imply about the behavior of the instruction pushl %esp under IA32? 2021/10/29 9 It implies that push the original value of %esp (old value)
Problem 4.5(P270) int poptesto int rval / Insert the following assembly code pushl tval #t save tval on stack movl %esp, %edx #f save stack pointer popl %esp t pop to stack pointer movl %oesp, rval setI movl %edx, %esp( It implies that popl %esp set the stack pointer to the value read from memory return rval mrmovl o(%esp). loess We find this function always returns tval the value passed to it as its argument. What does this imply about the behavior of pop es other y86 ins c tion would have th behavior 2021/10/29
Problem 4.5 (P270) int poptest() { int rval; /* Insert the following assembly code: pushl tval # save tval on stack movl %esp, %edx # save stack pointer popl %esp # pop to stack pointer movl %esp, rval # set popped value as return value movl %edx, %esp # restore original stack pointer */ … return rval; } We find this function always returns tval, the value passed to it as its argument. What does this imply about the behavior of popl %esp ? What other Y86 instruction would have the exact same behavior? 2021/10/29 10 It implies that popl %esp set the stack pointer to the value read from memory. mrmovl 0(%esp), %esp