Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Convert the following code into LEG V8 Given below is the C-style pseudo-code fo

ID: 3846357 • Letter: C

Question

Convert the following code into LEG V8

Given below is the C-style pseudo-code for calculating the n^th term in a Fibonacci sequence. Write the equivalent LEGv8 assembly code. For procedure calls, follow the LEGv8 register usage guidelines mentioned in Section 2.8 (i.e. registers X0-X7 are used for storing parameters, X9-X17 are temporary registers and X19-X28 are saved registers) long long int fib (long long int x) if x) { if (!x { return 0; } else if (x == 1 || x == 2) { return 1; } else { return fib(x-1) + fib(x-2) } } void main() { long long int I = 6; while (I

Explanation / Answer

Fibonacci:

push {X3, X4, X5, lr} ;                      function prolog

subs X4, X0, #0 ;                                r4 = r0 - 0

ble .L3 ;                                               if (X0 <= 0) goto .L3

cmp X4, #1 ;                                       Compare X4 to 1

beq .L4 ;                                              if (r4 == 1) goto .L4

add X0, X4, #4294967295 ;             X0 = X4 + #0xFFFFFFFF)

bl fibonacci ;                                       goto fibonacci @PC relative Address

mov X5, X0 ;                                      X5 = X0

sub X0, X4, #2 ;                                  X0 = X4 - 2

bl fibonacci ;                                       goto fibonacci @PC relative Address

adds X0, X5, X0

pop {X3, X4, X5, pc}

.L3:

mov X0, #0

pop {X3, X4, X5, pc}

.L4:

mov r0, #1

pop {X3, X4, X5, pc}