+1 (315) 557-6473 

Calculate x**y in ARM Assembly Language Assignment Solution

Check out the solution to Calculation of X**Y in Assembly Language following the best coding practices.

Instructions for the assignment

Objective

Write an ARM program that takes two integers as input, x and y, and recursively calculates x.

Requirements and Specifications

Your inputs x and y cannot be stored as a global variable at any point. This means you cannot store them at a data section address, even when accepting them from scanf; they must be returned from scanf on the stack,

You may use X19-X27 only in your main function.

Recursion requires:

  • Save the return address, arguments, and local variables in a new stack frame
  • Recursively call procedure with BL
  • Unwind the stack by restoring the return address, arguments, and local variables, and deallocating the stack frame

Your arguments x and y should be passed to your power function in X0 and X1. Your return value should be placed in X2.

Again, you may use X19-X27 in your main function, but you may not use X19-X27 in your recursive function.

Some assumptions:

  • x, y are 32-bit integers
  • x0 = 1
  • 00 = 0
  • xy <230

Screenshots of output

X Y in Assembly Language

X Y Assembly Language Output

Code (Assignment Solution)

.section .data

input_x_prompt : .asciz "Please enter x:

input_y_prompt : .asciz "Please enter y: "

input_spec : .asciz "%d"

result : .asciz "x^y = %d\n"

.section .text

.global main

main:

# add code and other labels here

sub sp, sp, -8 // make space in stack to save scanf input

ldr x0, =input_x_prompt // load address of x prompt 

bl printf // print x prompt

ldr x0, =input_spec // load address of input spec

mov x1, sp // save input on stack

bl scanf // read an integer from the user

ldr w19, [sp] // copy input number to w19

sxtw x19, w19 // extend number to 64 bits in x19

ldr x0, =input_y_prompt // load address of y prompt

bl printf // print y prompt

ldr x0, =input_spec // load address of input spec

mov x1, sp // save input on stack

bl scanf // read an integer from the user

ldr w20, [sp] // copy input number to w20

sxtw x20, w20 // extend number to 64 bits in x20

mov x0, x19 // copy first number to first argument

mov x1, x20 // copy second number to second argument

bl power // calculate power

ldr x0, =result // load address of result

mov x1, x2 // copy power return to print it

bl printf // print result

add sp, sp, 8 // remove allocated space from stack

# branch to this label on program completion

exit:

mov x0, 0

mov x8, 93

svc 0

ret

# function to compute the power

power:

cmp x0, 0 // if x is 0

bne else1 // if not, go to else

mov x2, 0 // return 0

b return // go to return

else1:

cmp x1, 0 // if y less than 0

bge else2 // if not, go to next else

mov x2, 0 // return 0

b return // go to return

else2:

cmp x1, 0 // if y is 0

bne else3 // if not, go to next else

mov x2, 1 // return 1

b return // go to return

else3:

stp fp, lr, [sp, -16]! // save fp and return address in stack

stp x0, x1, [sp, -16]! // save arguments x0 and x1 in stack

sub x1, x1, 1 // pass y = y-1 as second argument

bl power // recurse to calculate power

ldp x0, x1, [sp], 16 // load arguments x0 and x1 from stack

ldp fp, lr, [sp], 16 // load fp and return address from stack

mul x2, x2, x0 // multiply power result * x to get final result

return:

ret // return

Check out the solution to Calculation of X**Y in Assembly Language following the best coding practices.