Question

Implement the following C code in LEGv8 assembly. Hint: Remember that the stack pointer must remain...

Implement the following C code in LEGv8 assembly.

Hint: Remember that the stack pointer must remain aligned on a multiple of 16.

int fib (int n) {

if (n==0)

return 0;

else if (n == 1)

return 1;

else

return fib(n−1) + fib(n−2);

}

please "LEGv8" !

thanx :-)

Homework Answers

Answer #1

Solution:

    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                 

Please give thumbsup or do comment in case of any query. Thanks.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int...
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You must...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You...
Convert each of the below C code snippet to LEGv8 assembly code. Assume the variables a,...
Convert each of the below C code snippet to LEGv8 assembly code. Assume the variables a, b, and c are stored in registers X19, X20, and X21 respectively. Base address of d is stored in register X22. Comment your assembly code. a. if (a > b) d[a] = b + 8; else d[a] = b - 16; b. for (i=0;i 0) i++; a = d[i] + c; c. i = 0; while (d[i] == b) if ((a - i) >...
Convert each of the below C code snippet to LEGv8 assembly code. Assume variable a, b,...
Convert each of the below C code snippet to LEGv8 assembly code. Assume variable a, b, and c is stored in registers X19, X20, and X21 respectively. Base address of d is stored in register X22. Comment your assembly code. (24 Points) a. if (a > b) d[a] = b + 8; else d[a] = b - 16; b. for (i=0;i<a; i++) a = d[i] + c; c. i = 0; while ( d[i] == b) if(( a - i...
Please complete in MASM (x86 assembly language). Use the code below to get started. Write a...
Please complete in MASM (x86 assembly language). Use the code below to get started. Write a program that uses a loop to calculate the first seven values of the Fibonacci number sequence, described by the following formula: Fib(1) = 1, Fib(2) = 1, Fib(n) = Fib(n – 1) + Fib(n – 2). .386 .model flat,stdcall .stack 4096 ExitProcess PROTO,dwExitCode:DWORD .data    ; define your variables here .code main PROC    ; write your assembly code here    INVOKE ExitProcess,0 main...
Implement the following C++ code in assembly language. Substitute calls to WriteString for the printf() function...
Implement the following C++ code in assembly language. Substitute calls to WriteString for the printf() function calls: double X; double Y; if(x<Y) printf("X is lower\n"); else printf("X is not lower\n");
Translate the following C function into MIPS assembly code. Note that function f1 is defined somewhere...
Translate the following C function into MIPS assembly code. Note that function f1 is defined somewhere else. int f2(int x, int y) { int i, z = 0; for (i = x; i <= y; i++) z = z + f1(i, 5); return z; }
Translate the following C code into MIPS code. int test (int n) { if (n <...
Translate the following C code into MIPS code. int test (int n) { if (n < 2 ) return (10); else { k = 20 + test (n-1); k=k + n; return (k); } } Assume variable k is assigned to register $s1. Note: your code should be complete. please dont show me the software output,
a. Write MIPS assembly code for the following machine code. Assume that the segment is placed...
a. Write MIPS assembly code for the following machine code. Assume that the segment is placed starting at location 8000. Create labels for branch and jump instructions and indicate the 0001 0001 0010 1000 0000 0000 0000 0011 1000 1110 0001 0001 0000 0000 0000 1000 0010 0010 0010 1000 0000 0000 0000 0110 0000 1000 0000 0000 0000 0001 0000 0010 b. Translate the following C function into MIPS assembly code. int f1(int ar1[], int ar2[], int n) {...
Implement the following expression in assembly language:                                  &nb
Implement the following expression in assembly language:                                                 BX = –val2 + 7 - (- val3 + val1) * 2 Assume that val1, val2, and val3 are 8-bit integer variables Initialize val1 with 12, val2 with 9, and val3 with 2 You are only allowed to use 16-bit registers to hold intermediate results, whenever needed. Use ONLY mov, add, sub, movzx, movzx, or neg instructions whenever needed. Use the debugger to verify your answer. Please answer using this format for...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT