Question: The question to the problem. n pow.s you will implement a main function and a pow function. pow will take two integers ( let s
The question to the problem.
n pow.s you will implement a main function and a pow function. pow will take two
integers lets call them n and m and return nm You must compute nm using recursion.
Can you find a smaller power computation inside What is the base case? What is
the recursive case?
global main
type main, function
main:
sub sp sp #
str lrsp
well store two ints at sp # and sp #
ldr xintscanf
add x sp #
add x sp #
bl scanf
don't touch anything above this line.
Fix: call pow using the two ints on the stack as parameters the first parameter is at sp and the second is at sp
Fix: Print out the results using the intprintf format
mov w #
ldr lrsp
add sp sp #
ret
global pow
type pow, function
pow:
fix: implement the recursive function int powint n int m that returns nm
data
intscanf: asciz d d
intprintf: asciz d
Here is my code that I had already implemented.
global main
type main, function
main:
sub sp sp # Allocate space on stack
str lrsp Save return address
Read two integers n and m from input
ldr xintscanf
add x sp # Address for n
add x sp # Address for m
bl scanf
Load n and m from the stack and call pown m
ldr wsp # Load n
ldr wsp # Load m
bl pow Call pown m result in w
Print the result using printf
ldr xintprintf
mov w w Move result to w for printing
bl printf
Clean up and return
ldr lrsp Restore return address
add sp sp # Deallocate stack space
ret
global pow
type pow, function
pow:
sub sp sp # Allocate stack space for recursion
str lrsp # Save link register
Base case: if m return
cmp w #
beq basecase
Recursive case: compute nm and multiply by n
sub w w # Decrease m by
mov w w Store n in w
bl pow Recursive call pown m
mul w w w Multiply result by n w
Restore link register and return
ldr lrsp #
add sp sp #
ret
basecase:
mov w # Base case: return
ldr lrsp # Restore link register
add sp sp # Deallocate stack space
ret
data
intscanf: asciz d d Format for scanf
intprintf: asciz d
Format for printf
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
