write a complete ARM assembly program called “ my_fact.s ” using the ARMSim that
ID: 3697787 • Letter: W
Question
write a complete ARM assembly program called “my_fact.s” using the ARMSim that can be successfully compiled and executed to compute the factorial using iteration instead of recursion. The “my_fact.s” should contain a procedure named ifact that calculates the factorial using iteration according to the value of the input argument r0 to denote n. In the main procedure of your “my_fact.s”, you should set r0 to be 12 before calling ifact. After calling ifact, the result as n! should be stored to r0 before returning to the main procedure that will then terminate the program execution. In case r0 is set to be £ 0 by the main procedure, the result in r0 will be returned as 1 by ifact.
I just need the program code
Explanation / Answer
;myfact.s
; This program calculate the factorial of a number using iterative process
; We use the ifact procedure to calculate the factorial of a number given in parameter r0
REA main, CODE, READONLY
ENTRY ; mark first instructioin to be start execution here
start MOV r0,#12 ; initilize parameter r0 as n =12
BL ifact ; calling subroutine ifact
stop
MOV r0, #0x18 ; angel_SWIreason_ReportException
LDR r1, =0x20026 ; ADP_Stopped_ApplicationExit
SVC #0x123456 ; ARM semihosting (formerly SWI)
ifact ;Subroutine name
;Subroutine Code
cmp r0,#0
MOV r0,#1
BX lr ; if n = 0 then fact is returned to 1 no need to execute loop
;Return from subroutine
MOV r1,#1 ;inititlize variable R1 to 1 for loop
MOV r3,#1 ;intilize variable R3 to 1 for factorial calculation
.loop
MUL r3,r3,r1 ;calculate Factorial as R3=R3*R1
ADD r1,r1,#1 ;increment counter variable R1=R1+1
CMP r1,r0 ; Check the Varibale if R1<=R0 then go
;back to loop (which is recieved as parameter)
BLE loop
;end of loop
BX lr ; Return from subroutine
END ; mark as end of a file
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.