Question: ARM ASSEMBLY. PLEASE USE STARTER CODE In this problem you are to write a function, called hypot, to compute the hypotenuse of a right-triangle using
ARM ASSEMBLY. PLEASE USE STARTER CODE
In this problem you are to write a function, called hypot, to compute the hypotenuse of a right-triangle using the Pythagorean Theorem a2 + b2 = c2 . That is, given a and b you must compute c. You have been provided with functions, in file PA6-1.s, to square a number (square) and to compute the integer square-root of a number (isqrt). You must use these functions in the hypot function that you write. You have also been given tester code to test your hypot function. The values that should be in registers after the code completes are shown in the comments.
;-----------
; Programming assignment 6, problem 1
; Your header information goes here
;------------------------------------
; Complete the function hypot which returns
; the hypotenuse of a right-triangle when given
; passed values of the two sides as paramters.
;------------------------------------
; Test code: DO NOT MODIFY
; After the code runs, the following should
; be in registers R5-R7
; R5 = 5
; R6 = 10
; R7 = 7
;----------
tester
MOV R0, #3
MOV R1, #4
BL hypot
MOV R5, R0
MOV R0, #6
MOV R1, #8
BL hypot
MOV R6, R0
MOV R0, #7
MOV R1, #3
BL hypot
MOV R7, R0
END
;---------------------
; hypot. Given a and b as parameters, compute
; c based on the Pythatorean theorm:
; a^2 + b^2 = c^2
; You must use the square and isqrt functions
; given below.
;----------------------
hypot ; your code here
square PUSH {LR}
MUL R1, R0, R0
MOV R0, R1
POP {PC}
;----------------------
; isqrt - compute integer square root like this:
;int floorSqrt(int x)
;{
; // Base cases
; if (x == 0 || x == 1)
; return x;
;
; // Staring from 1, try all numbers until
; // i*i is greater than or equal to x.
; int i = 1, result = 1;
; while (result <= x)
; {
; i++;
; result = i * i;
; }
; return i - 1;
;}
isqrt
PUSH {LR}
CMP R0, #0
BEQ endfunc
CMP R0, #1
BEQ endfunc
MOV R1, #1 ; i
MOV R2, #1 ; result
loop CMP R2, R0
BGT endloop
ADD R1, R1, #1
MUL R2, R1, R1
BAL loop
endloop SUB R0, R1, #1
endfunc POP {PC}
THIS IS THE COMPLETE QUESTION
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
