Hello I am very new to writing assembly and have a question regarding my attempt at writing a recursive function to compute the factorial of n.
Here is my attempt at writing the factorial function:
.global main
main:
MOV r1, #3
fact:
SUB sp, sp, #8
STR lr, [sp, #0]
STR r1, [sp,#4]
CMP r1, #1
BGT Else
ADD sp, sp, #8
MOV pc, lr
Else:
SUB r1, r1, #1
BL fact
MOV r2, r1
LDR r1, [sp, #4]
LDR lr, [sp, #0]
ADD sp, sp, #8
MUL r1, r2, r1
MOV pc, lr
MOV r0, #1
SWI 0x6b
SWI 0x11
The issue is this: i successfully can compute that 3 factorial is 6 and it gets stored in r1 at the end of the program; however, I can never get passed the last "MOV pc, lr" statement in the third execution of the loop and I cannot understand the logic behind why.
When I get to the third loop of MOV pc, lr I get an error stating: "PC out of valid memory range" but I am not sure why this is the case. Any pointers in the right direction would be greatly appreciated because I am an absolute beginner and cannot understand why this error is occurring. Thank you for your time!