Consider the following C code implementing function, f. This function is called
ID: 3850162 • Letter: C
Question
Consider the following C code implementing function, f. This function is called with argument 15 (in C code: f(15)). Upon entry to the function before the first instruction of the prologue is executed, assume that the JSR instruction calling f is 0x302A, R5 is 0xFF35, and R6 is 0xFF2C (e.g. the first argument to f is stored at this location). Using these initial assumptions, show the runtime stack just after the RET instruction is executed for f. Also, draw an arrow indicating the position of R5 and R6 when the RET instruction is executed. If you don’t know the value held in a memory location, leave it empty. You do not need to show the LC-3 code for this function. All you must do is interpret its updates to the runtime stack. int f(int i) { int i = 0; int sum = 0; while(i>0) sum += i--; return sum; }
Explanation / Answer
f(int):
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-20], edi
mov DWORD PTR [rbp-20], 0
mov DWORD PTR [rbp-4], 0
.L3:
cmp DWORD PTR [rbp-20], 0
jle .L2
mov eax, DWORD PTR [rbp-20]
lea edx, [rax-1]
mov DWORD PTR [rbp-20], edx
add DWORD PTR [rbp-4], eax
jmp .L3
.L2:
mov eax, DWORD PTR [rbp-4]
pop rbp
ret
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.