Write the following subroutine in x86 assembly: int f(int v1, int v2, int v3) {i
ID: 3802631 • Letter: W
Question
Write the following subroutine in x86 assembly: int f(int v1, int v2, int v3) {int x = v1 + v2; return (x + v3) * (x - v3);} Recall that: Subroutine arguments are passed on the stack, and can be accessed within the body of the subroutine starting at address EBP+8. At the start of each subroutine: i. Save EBP on the stack ii. Copy the current value of the stack pointer (ESP) to EBP iii. 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. Local variables can then be accessed starting at the address EBP-4. iv. Save any registers the function uses other than EAX, ECX, and EDX. A subroutine's return value is typically stored in EAX.Explanation / Answer
Full program for given subroutine---->
#include <iostream>
using namespace std;
int f(int num1, int num2, int num3);
int main () {
int a,b,c;
a=100;b=200;c=300;
int ret;
ret = f(a, b, c);
cout << "Return value is : " << ret << endl;
return 0;
}
int f(int v1, int v2, int v3)
{
int x=v1+v2;
return (x+v3)*(x-v3);
}
x86 assembly language code for above given program --------->
.LC0:
.string "Return value is : "
main:
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], 100
mov DWORD PTR [rbp-8], 200
mov DWORD PTR [rbp-12], 300
mov edx, DWORD PTR [rbp-12]
mov ecx, DWORD PTR [rbp-8]
mov eax, DWORD PTR [rbp-4]
mov esi, ecx
mov edi, eax
call f(int, int, int)
mov DWORD PTR [rbp-16], eax
mov esi, OFFSET FLAT:.LC0
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov rdx, rax
mov eax, DWORD PTR [rbp-16]
mov esi, eax
mov rdi, rdx
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))
mov eax, 0
leave
ret
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
__static_initialization_and_destruction_0(int, int):
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], edi
mov DWORD PTR [rbp-8], esi
cmp DWORD PTR [rbp-4], 1
jne .L7
cmp DWORD PTR [rbp-8], 65535
jne .L7
mov edi, OFFSET FLAT:std::__ioinit
call std::ios_base::Init::Init()
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:std::__ioinit
mov edi, OFFSET FLAT:std::ios_base::Init::~Init()
call __cxa_atexit
.L7:
nop
leave
ret
_GLOBAL__sub_I_main:
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.