Question

For the following C statement, write the corresponding LEGv8 assembly code. long long int func( long...

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

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
For the following C statement, write the corresponding LEGv8 assembly code. long long int func( long...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT