For the following C statement, write the corresponding LEGv8 assembly code.
long long int func( long long int n ) {
if ( n <= 1 ) {
return n;
}
return func( n – 1 ) + func( n – 2 );
}
I assumed that n is stored in register X19.
I assumed that store the Fibonacci sequence in an array, which has its base address stored in X20.
LEGv8 assembly code:
MOV X17, XZR // keep (previous) 0 in X17 for further use
ADDI X18, XZR, #1 // keep (Current) 1 in X18 for further use
ADDI X9, XZR, #0 // Assuming i = 0 is in register X9
fibo:
SUBI SP, SP, #24 // Adjust stack pointer for 3 items
STUR LR, [SP, #16] // save the return address
STUR X17, [SP, #8] //save content of X17 on the stack
STUR X18, [SP, #0] //save content of X18 on the stack
SUBS X10, X9, X19 // test for i==n CBNZ X10, L1 // If i not equal to n, go to L1
MOV X6, XZR // keep 0 on X6
ADDI X5, XZR, #1 // keep 1 on X5
ADDI X2, X9, #1 //X9 increased by 1 for further use
STUR X6, [X20,#0] //store 0 in the array
STUR X5, [X20, #8] //store 1 in the array
ADDI SP, SP, #24 // pop 3 items from the stack
BR LR // return to the caller
L1:
ADD X16, X17, X18 // Next_Num = previous + Current
MOV X17, X18 // Previous = Current
MOV X18, X16 // Current= Next_Num
ADDI X9, X9, #1 // i++ BL fibo // call fibo
LDUR X18, [SP, #0] // return from BL; restore previous
LDUR X17, [SP, #8] // restore current
LDUR LR, [SP, #16] // restore the return address
ADDI SP, SP, #24 // adjust stack pointer to pop 3 items
ADD X7, X18, X17 // keep (previous + current) value on register X7 LSL X2, X2, #3 // Multiplying by 8 for offset
ADD X12, X20, X2 // address of the array increase by 8
STUR X7, [X12, #0] // store (previous + current) value on the array
SUBI X2, X2, #1 // X9 decreased by 1
BR LR // return
For any clarifications, please feel free to comment.
If you are satisfied with the answer, please give a thumbs up.
For the following C statement, write the corresponding LEGv8 assembly code. long long int func( long...
19. (4 points) Convert the following C function (a leaf procedure) to an LEGv8 assembly subroutine. Make sure that you follow the LEGv8 calling conventions. long long int func (long long int a, long long int b) long long int 0: long long int s 0: while (s < b) return k;
Problem #2 (20pts) Please write the MIPS assembly code corresponding to the following C code segment. Assume that f is assigned to register $s2 int Example int i, int j, int k) int f; f i+j-k-1: return fi
Write a LEGv8 assembly code for Fibonacci sequence using recursion.
Problem 1: a) Write LEGv8 assembly language programs for the following C program fragments. Please make sure to add any necessary comments. P[20] = P[10] + P[30] + 25; b) Show the machine code that will be generated by the assembly code in Problem 1.a. You can write machine code as decimal numbers. please help me solve this
please code using risc-v language and make it as simple as
possible
Problem 1: (20 marks) Part 1: (15 points) Compile the RISC-V assembly code for the following C code. Assume that n and k are passed in x3 and x4 respectively. Values n and k are initialized to 14 and 14. Assume that result returned in register fl and that double precision numbers are used. After you are done store the result in address: 12(x3). Are you allowed to?...
Translate function f into MIPS assembly language. The code for function f is as follows: int func(int a, int b){ return a + b; } int f(int a, int b, int c, int d){ return func(func(a, b), c - d);
4. Convert the below C code snippet to LEGV8 assembly code. Base address of x is stored in register X19. Assume variables a, b, andc are stored in registers X20, X21, and X22 respectively. Assume all values are 64-bits. Do not use divide and multiply instructions in your code. Comment your assembly code. (30 Points) x[e] a x[1]; q [e]x x[a/2]b; x[2]; + x[1] x[2] x[c] C >> 4: x[1] +
4. Convert the below C code snippet to LEGV8...
1. (a) Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x){ int res = 0; int i = 0; for(i = 0; i != n; i++) if(a[i] == x) res = res + 1; return res; } Cannot use converter need hard copy!!!
For the following C statement, what is the corresponding MIPS assembly code? Assume that the variables f, g, h, and i are given and could be considered 32-bit integers as declared in a C program. Use a minimal number of MIPS assembly instructions: f g(h 5);
For the following C statement, what is the corresponding MIPS assembly code? Assume that the variables f, g, h, and i are given and could be considered 32-bit integers as declared in a C...
In MIPS assembly, write an assembly language version of the following C code segment: int A[100], B[100]; for (i=1; i < 100; i++) { A[i] = A[i-1] + B[i]; }