Created
October 5, 2014 11:45
-
-
Save zikosw/ef35815d7a7f248aef61 to your computer and use it in GitHub Desktop.
wow.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int fact(int n){ | |
| if (n<1) return 1; | |
| else | |
| return n*fact(n-1); | |
| } | |
| fact: | |
| 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 | |
| BGE L1 | |
| MOV r0,#1 ;if so,return is 1 | |
| ADD sp,sp,#8 ;Pop 2 items form stack | |
| MOV pc,lr ;Return to caller | |
| L1:SUB r0,r0,#1 ;else decrement n | |
| BL fact ;Recursive call | |
| MOV r12,r0 ;Restore original n | |
| LDR r0,[sp,#0] ;and return address | |
| LDR lr,[sp,#4] ;pop 2 items from stack | |
| ADD sp,sp,#8 ; | |
| MUL r0,r0,r12 ;Multiply to get result | |
| MOV pc,lr ;and return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment