Question

In: Computer Science

Translate the following segment of Python into ARMv8 assembly. You may assume that two positive integers...

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

Solutions

Expert Solution

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>)


Related Solutions

2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a,...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a, i, and j are in registers $s0, $t0, and $t1, respectively. Assume that register $s2 holds the base address of the array A (add comments to your MIPS code). j = 0; for(i=0 ; i<a ; i++) A[i]=i+j++;
4.Translate the following C code to MIPS assembly code. Assume that the value of i is...
4.Translate the following C code to MIPS assembly code. Assume that the value of i is in register $t0, and $s0 holds the base address of the integer MemArray if (i > 10) MemArray[i] = 0; else MemArray[i] = -MemArray[i]; 6.Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds...
Translate the following C code into MIPS assembly code. Assume a in $s0, i in $s1,...
Translate the following C code into MIPS assembly code. Assume a in $s0, i in $s1, and base address of b[] in $s2, respectively. Note that the element’s data type in array b is integer (word). Please do not change the C code structure and please comment your MIPS code. for (i = 100; i>a; i--) {          b[i] = 16*i; b[i+1] = 16*(i+1); b[i+2] = 2*i + 16; }
Python We say that a pair of positive integers ?,? is special if ? − ?...
Python We say that a pair of positive integers ?,? is special if ? − ? = 10 and both ? and ? can be represented as a sum of two squares. For example, the pair (18,8) is special since 18 − 8 = 10, 18 = 3^2 + 3^2, and 8 = 2^2 + 2^2. Write a program that prints all special pairs (?,?) with 1 ≤ ?,? ≤ 100.
Write assembly code for the following machine code. Assume that the segment is placed starting at...
Write assembly code for the following machine code. Assume that the segment is placed starting at location 80000. Create labels for jump and branch instructions. Indicate the actual memory addresses represented by such labels. 0010 1010 0000 1000 0000 0000 0000 1010 0001 0001 0000 0000 0000 0000 0000 0010 0000 0010 0001 0001 1000 0000 0010 0000 0000 1000 0000 0000 0100 1110 0010 0101 0000 0010 0001 0010 1000 0000 0010 0000
a. Write machine code for the following assembly code. Assume that the segment is placed starting...
a. Write machine code for the following assembly code. Assume that the segment is placed starting at location 80000. Use decimal numbers to represent each instruction. loop:         beq $s3, $s1, endwhile                  add $t0, $s3, $s4                  lw $t1, 0($t0)                  add $s0, $s0, $t1                  addi $s3, $s3, 4                  j loop endwhile: b. Write assembly code for the following machine code. Assume that the segment is placed starting at location 80000. Create labels for jump and branch instructions. Indicate the actual...
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g,...
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively   B[8] = A[i-j]; 2.Translate the following C code to MIPS assembly code. Assume that the values of v in $a0, k in $a1, temp in $t0.    // leaf procedure that...
in assembly show output too - thanks . Translate the following expression to Assembly: a. 5...
in assembly show output too - thanks . Translate the following expression to Assembly: a. 5 x (10 + 13) b. 5 - 4 x 4 c. AX = FF50h - ABCDh d. EAX = (EBX - 4) x (-1) e. EAX = 0 EBX = 9 ECX = 10 X = EAX x EBX - ECX
4. Translate the following C code to MIPS assembly (in two separate files). Run the program...
4. Translate the following C code to MIPS assembly (in two separate files). Run the program step by step and observe the order of instructions being executed and the value of $sp. int main() { int x=2; z=Subfunc(x); printf(“Value of z is: %d”, z); } int Subfunc(int x) { return x+1;} Submission file: Lab4_4a.asm and Lab4_4b.asm
Translate the following C code into M4K assembly language. You do not have to use the...
Translate the following C code into M4K assembly language. You do not have to use the frame pointer, just use $sp if you need to use the stack. You do not have to show the stack initialization nor stack cleanup. If you need a specific value for an address, just make an assumption. int A; main() { int B = 5; B = A+B }; // main //Disassembly starts here !main() { //stack and frame pointer init // you do...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT