In: Computer Science
Translate the following segment of Python into ARMv8 assembly. You may assume that two positive integers a and b have already been stored in registers X0 and X1. , which should have the correct end values for a and b at the end of the code.
while a != b: if a>b: a = a - b else: b = b - a
Note: Labels must be on their own line, and all programs must start with the label main:
Test
X0=49 X1=64
Expected
X0: 1
Here our task is to convert the given python program segment to ARMv8 assembly
The code is:
while a != b:
if a>b:
a = a - b
else:
b = b - a
so we have one while loop and an if-else statement inside it
Inorder to complete the task we need conditional branching and arithmetic instructions from ARMv8 instruction sets
a is stores in X0 and b is stored at X1
The complete code is given below
main:
CMP X0,X1
B.EQ skip
CMP X1,X0
B.PL 1a
SUB X0,X0,X1
1:
SUB X1,X1,X0
B main
skip:
Now lets analyse the instructions line by line
main:
this is a label from which the main program starts (here it is the starting point of while loop)
CMP X0,X1
B.EQ skip
here we will compare values of X0 and X1 and if they are equal then the program branch to the lable <skip>.this is the while loop equalent statemnet
CMP X1,X0
B.PL here
here we will compare X1 AND X2 ,actually CMP statement is substarcting second value from first and store the result , in next statmnet program will branch to label <here> if X1-X2 is posative
thsi statemnt is equalent to if a>b:
SUB
X0,X0,X1 (body of if)
here:
SUB X1,X1,X0 (body of else)
Thease are body of if and else in which two arithamatic operation is happening
B main
This statment will branch tothe lable<main> so that again while loop will get executed
skip:
When while loop condition get wrong then the program execution will reach this lable (<skip>)