0

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Entilo
  • 1
  • 1
  • Where do you expect the branch to main instruction to be loaded into memory? Please show us your best attempt at writing code for this problem. This is a standard homework question, so we'd like to see you make some effort. – Elliot Alderson Nov 11 '18 at 21:36
  • If you want to delete your question, delete it. But don't edit it into nonsense by removing all the code. – Peter Cordes Nov 14 '18 at 18:40

1 Answers1

1

Right now the code stands no chance of working because you don't ever update the values of r3 and r4 (your previous two Fibonacci numbers), and you overwrite r0 (intended to be your loop counter) in a number of places. There may be other problems too.

It's not really clear where you're stuck - it seems perhaps to be a combination of inexperience with assembly language and inexperience with the process of developing an algorithm. My advice would be to separate the two processes. Write some code in C (or another language, but C is the closest you'll get to assembly language without actually using it!) that computes the first n Fibonacci numbers. Once that works, start thinking about how you would implement the same thing in assembly language.

cooperised
  • 2,404
  • 1
  • 15
  • 18
  • Sorry, that's still not clear enough. If you've got an algorithm in C that you understand, then there's no reason for the first of the errors that I mentioned in my answer: you're not updating `r3` and `r4` so there's no way your code can logically work. That's not an assembly language problem, that's an algorithmic problem. – cooperised Nov 12 '18 at 15:47