I need to make an ARM assembly program which will print out the Fibonacci Sequence and i'm unsure of how to approach it. The program will ask the user for a number, and when they input that number, the program should print out the Fibonacci sequence for that amount of numbers, so for example, if the user inputs 10, the program will run through printing
"Fibonacci number 1 is 1."
"Fibonacci number 2 is 1."
And so on.
Currently my code for this looks like this:
B main
maxF DEFW 0
enterI DEFB "Please enter the number of fibonacci numbers to print: ",0
newline DEFB "\n",0
fibbo DEFB "Fibonacci number ",0
is DEFB " is ",0
end DEFB ".\n",0
errorM DEFB "Error, try again!\n",0
ALIGN
main ADR R0, enterI
SWI 3
MOV R1, #0
MOV R2, #10
MOV R3, #0 ;lastNumber variable
MOV R4, #1 ;numberbeforeLast variable
MOV R5, #0 ;currentNumber variable
start SWI 1 ;take user input
CMP R0, #10 ;compare R0 with #10 (enter)
BEQ _end ;if equal, go to _end
CMP R0, #48 ;compare R0 with #48 (0)
BLT _error ;if less than, go to _error
CMP R0, #57 ;compare R0 with #57 (9)
BGT _error ;if greater than, go to _error
SUB R0, R0, #48 ;R0 = R0 - #48
SWI 4 ;print the above
MUL R1, R1, R2 ;Multiply the R1 register by R2 and store in R1
ADD R1, R1, R0 ;Add the R1 register to R0 and store in R1
B while_cond
while_loop
ADD R5, R3, R4 ;currentnumber = lastnumber + numberbeforelast
ADR R0, fibbo
SWI 3
STR R5, maxF
LDR R0, value
SWI 4
ADR R0, is
SWI 3
while_cond
CMP R0, #0
BGT while_loop
_end SWI 2
_error ADR R0, errorM
SWI 3
B main
I've been thinking of an approach for this and I have something but i'm unsure of how to do it. I was thinking that the program expects an input from the user for the number, and then it does the calculation for that number and then prints out the line for number currently in the register, branches back up to the top where the register is overwritten with the next value and then does the same thing until the value of that register is equal to the value that the user specified, which is when it stops.