Skip to content

Instantly share code, notes, and snippets.

@zikosw
Created October 5, 2014 12:36
Show Gist options
  • Save zikosw/4d5788bbf7ee95fd061c to your computer and use it in GitHub Desktop.
Save zikosw/4d5788bbf7ee95fd061c to your computer and use it in GitHub Desktop.
wowFact.asm
fact: ; fact 3 r0=3
SUB sp,sp,#8 ;Adjust stack for 2 times
STR lr,[sp,#4] ;Save return address
STR r0,[sp,#0] ;Save argument n
CMP r0,#1 ;compare n to 1 ; r0=3 ~~~ 1
;BGE L1
SUB r0,r0,#1 ;else decrement n
BL fact ;Recursive caller ; r0=2
SUB sp,sp,#8 ;Adjust stack for 2 times
STR lr,[sp,#4] ;Save return address
STR r0,[sp,#0] ;Save argument n
CMP r0,#1 ;compare n to 1 ; r0=2 ~~~ 1
;BGE L1
SUB r0,r0,#1 ;else decrement n ; r0=1
BL fact ;Recursive call
SUB sp,sp,#8 ;Adjust stack for 2 times
STR lr,[sp,#4] ;Save return address
STR r0,[sp,#0] ;Save argument n
CMP r0,#1 ;compare n to 1 ; r0=1 ~~~ 1
MOV r0,#1 ;if so,return is 1
ADD sp,sp,#8 ;Pop 2 items form stack
MOV pc,lr ;Return to caller
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment