Write the following subroutine in x86 assembly: int f(int v1, int v2, int v3) {
ID: 3679521 • Letter: W
Question
Write the following subroutine in x86 assembly: int f(int v1, int v2, int v3) { int x = v1 + v2; return (x + c3) + (x - c3); } Recall that: Subroutine arguments are passed on the stack, and can be accessed within the body of the subroutine starting at address EBP-S. A the state of each subroutine: Save EBP on tge stack ii. Copy the current value of the stack pointer (ESP) to EBP. Create space within the stack for each local variable by subtracting the appropriate value from ESP. For example, if your function uses four integer local variables, each of which contains four bytes, subtract 16 from ESP. iv. Local variables can then be accessed starting at the address EBP-4. A subroutine's return value is typically stored in EAX.Explanation / Answer
f(int, int, int):
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-20], edi
mov DWORD PTR [rbp-24], esi
mov DWORD PTR [rbp-28], edx
mov edx, DWORD PTR [rbp-20]
mov eax, DWORD PTR [rbp-24]
add eax, edx
mov DWORD PTR [rbp-4], eax
mov edx, DWORD PTR [rbp-4]
mov eax, DWORD PTR [rbp-28]
add edx, eax
mov eax, DWORD PTR [rbp-4]
sub eax, DWORD PTR [rbp-28]
imul eax, edx
pop rbp
ret
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.