Write an ARM assembly language subroutine (named nfibo) to calculate and return
ID: 3573592 • Letter: W
Question
Write an ARM assembly language subroutine (named nfibo) to calculate and return the n-th Fibonacci number. Fibonacci numbers (or a Fibonacci sequence) are a series of numbers with a property that the next number in the series is a sum of previous two numbers. Starting the series from 0, 1 as the first two numbers we have 0, 1, (0 + 1) = 1, (1 + 1) = 2, (1 + 2) = 3, (2 + 3) = 5, (3 + 5) = 8, (5 + 8) = 13, (8 + 13) = 21, (13 + 21) = 34. The subroutine, nfibo, accepts n as an input in R0 and returns the corresponding Fibonacci number at the n-th position in R0. Don't try to use an array to map as Fibonacci is an infinite series. This programming can be done with loops, or a recursive subroutine. Recursive subroutine is shorter and easier to write then loops, but both are doable, you can choose either method. As always the subroutine should preserve all the registers except the return/output register. For example if n = 3, return value would be 2 if n = 6. return value would be 8Explanation / Answer
nfibo proc PUSH EBP ; save previous frame pointer MOV EBP, ESP ; set current frame pointer MOV EAX, [EBP+8] ; get argument N CMP EAX, 1 ; N 1 JMP exit Recurse: DEC EAX ; = N-1 MOV EDX, EAX ; = N-1 PUSH EDX ; save N-1 PUSH EAX ; set argument = N-1 CALL nfibo ; compute Fib(N-1) to ECX POP EAX ; pop N-1 DEC EAX ; = N-2 PUSH ECX ; save Fib(N-1) PUSH EAX ; set argument = N-2 CALL nfibo ; compute Fib(N-2) to ECX POP EAX ; = Fib(N-1) ADD ECX, EAX ; = Fib(N-1)+FIB(N-2) exit: MOV ESP,EBP ; reset stack to value at function entry POP EBP ; restore caller's frame pointer RET ; and returnRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.