Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assembly Language The following is a recursive definition of the operation a b,

ID: 3581822 • Letter: A

Question

Assembly Language

The following is a recursive definition of the operation ab, where it is assumed that a and b are nonnegative integers:

{a0 = 1; a1 = a; and for b > 1; ab = a * ab-1}.

Write a recursive MIPS assembly program that accepts two nonnegative integers a and b in registers $a1 and $a2 respectively, computes ab recursively, and returns the result in $v0. If the value of ab exceeds the value 0x7FFFFFFF at any stage of the recursion, then the function returns -1 as a "sign" of overflow.

Explanation / Answer

Answer:

.zero 1
main:
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-8], 1
mov eax, DWORD PTR [rbp-12]
mov DWORD PTR [rbp-16], eax
mov DWORD PTR [rbp-20], 0
.L3:
cmp DWORD PTR [rbp-20], 1
jle .L2
mov eax, DWORD PTR [rbp-12]
imul eax, DWORD PTR [rbp-4]
sub eax, 1
mov DWORD PTR [rbp-4], eax
jmp .L3
.L2:
mov eax, 0
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
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote