This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Recursive Fibonacci sequence on M4 - improving the code

Hello,

I'm learning assembly on the ARM Cortex M4. I'm trying to write a code which will display the first 5 Fibonacci numbers in register R3 then go into an endless loop.

This would be the C code I'm trying to emulate:

I wrote the following assembly code in Keil:

Problem is that I was not able to make the code branch to the Stop loop after the first 4 numbers are displayed into the register R3. What happens is when I reach the 'BX LR' instruction in the 'Sum' subroutine the code keeps branching to the Sum and keeps on printing Fibonacci numbers forever. I could somehow make use of the R4 register to decrement it few times so when we have 4 numbers displayed in register R3 we branch to the stop loop but that is not a very elegant solution.

I just want the number of numbers to be printed in the register R0 and then once the first 4 numbers are displayed into R3 the code should return from the Sum(4) and go to the stop loop below.
I know I have to somehow make use of the LR register to achieve this but I was not able to figure it out. 
Can you please help me by helping me figure out how I could modify this code so that I can achieve what I want? Thank you for reading and sorry for my English I know it is not the best :D

0