Requirements 1 Translate the following three subroutines int

Requirements:
1. Translate the following three subroutines into assembly language.
main()
{
fn(1,3,2,4,8,6,5);
}
fn(int a, b, c, d, e, f, g)
{
register int x, y, z;
x = a + b*c
y = d - e / f;
z = power(x, g);
return x + y + z;
}
power(int base, int exponent)
{
register int p, e;
p = 1;
for(int e=1; e <= exponent; e++)
{
p *= base;
}
}
Note:
DO NOT use macros (begin_main, end_main, var, local_var) given in text book. You should calculate offsets for stack variables manually.
DO NOT try to optimize your code (i.e. remove nops)
2. Write an assembly program that prompts the user for two numbers and prints the sum.
Sample Screen Shot:
Enter Number 1:
2
Enter Number 2:
3
The sum of 2 and 3 is 5
Note:
Your program should contain .data, .bss, and .text sections
DO NOT try to optimize your code (i.e. remove nops)

Solution

Answer:

Assembly Language Code :

.zero 1
main:
push rbp
mov rbp, rsp
sub rsp, 8
push 5
mov r9d, 6
mov r8d, 8
mov ecx, 4
mov edx, 2
mov esi, 3
mov edi, 1
call fn(int, int, int, int, int, int, int)
add rsp, 16
mov eax, 0
leave
ret
fn(int, int, int, int, int, int, int):
push rbp
mov rbp, rsp
push r13
push r12
push rbx
sub rsp, 40
mov DWORD PTR [rbp-36], edi
mov DWORD PTR [rbp-40], esi
mov DWORD PTR [rbp-44], edx
mov DWORD PTR [rbp-48], ecx
mov DWORD PTR [rbp-52], r8d
mov DWORD PTR [rbp-56], r9d
mov eax, DWORD PTR [rbp-40]
imul eax, DWORD PTR [rbp-44]
mov edx, eax
mov eax, DWORD PTR [rbp-36]
lea ebx, [rdx+rax]
mov eax, DWORD PTR [rbp-52]
cdq
idiv DWORD PTR [rbp-56]
mov edx, eax
mov eax, DWORD PTR [rbp-48]
sub eax, edx
mov r12d, eax
mov esi, DWORD PTR [rbp+16]
mov edi, ebx
call power(int, int)
mov r13d, eax
lea eax, [rbx+r12]
add eax, r13d
add rsp, 40
pop rbx
pop r12
pop r13
pop rbp
ret
power(int, int):
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-20], edi
mov DWORD PTR [rbp-24], esi
mov DWORD PTR [rbp-4], 1
.L7:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-24]
jg .L6
add DWORD PTR [rbp-4], 1
jmp .L7
.L6:
nop
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 .L10
cmp DWORD PTR [rbp-8], 65535
jne .L10
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
.L10:
nop
leave
ret
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret

Requirements: 1. Translate the following three subroutines into assembly language. main() { fn(1,3,2,4,8,6,5); } fn(int a, b, c, d, e, f, g) { register int x, y
Requirements: 1. Translate the following three subroutines into assembly language. main() { fn(1,3,2,4,8,6,5); } fn(int a, b, c, d, e, f, g) { register int x, y
Requirements: 1. Translate the following three subroutines into assembly language. main() { fn(1,3,2,4,8,6,5); } fn(int a, b, c, d, e, f, g) { register int x, y

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site