0
0
Cnc-programmingProgramBeginner · 2 min read

ARM Assembly Program to Calculate Factorial of a Number

An ARM assembly program to calculate factorial uses a loop that multiplies numbers from 1 up to the input number, for example: mov r1, #1; mov r2, #1; loop: mul r1, r1, r2; add r2, r2, #1; cmp r2, r0; ble loop where r0 holds the input and r1 the factorial result.
📋

Examples

Input0
Output1
Input5
Output120
Input7
Output5040
🧠

How to Think About It

To calculate factorial in ARM assembly, start with a result of 1 and multiply it by each number from 1 up to the input number. Use registers to hold the current multiplier and the result, and repeat the multiplication in a loop until you reach the input number.
📐

Algorithm

1
Load the input number into a register.
2
Initialize a result register to 1 and a counter register to 1.
3
Multiply the result by the counter.
4
Increment the counter by 1.
5
Compare the counter with the input number.
6
Repeat steps 3-5 until the counter exceeds the input number.
7
Return the result.
💻

Code

arm_architecture
    .global _start
_start:
    mov r0, #5          @ Input number (change as needed)
    mov r1, #1          @ Result initialized to 1
    mov r2, #1          @ Counter initialized to 1
loop:
    mul r1, r1, r2      @ result = result * counter
    add r2, r2, #1      @ counter = counter + 1
    cmp r2, r0          @ compare counter and input
    ble loop            @ if counter <= input, repeat

    @ Exit syscall to return result in r0 (for demonstration)
    mov r0, r1          @ move factorial result to r0
    mov r7, #1          @ syscall number for exit
    svc #0              @ make syscall
Output
Program exits with r0 = 120 (factorial of 5)
🔍

Dry Run

Let's trace the factorial of 5 through the code.

1

Initialize registers

r0=5 (input), r1=1 (result), r2=1 (counter)

2

First loop iteration

r1 = 1 * 1 = 1; r2 = 1 + 1 = 2; compare 2 <= 5 (true)

3

Second loop iteration

r1 = 1 * 2 = 2; r2 = 2 + 1 = 3; compare 3 <= 5 (true)

4

Third loop iteration

r1 = 2 * 3 = 6; r2 = 3 + 1 = 4; compare 4 <= 5 (true)

5

Fourth loop iteration

r1 = 6 * 4 = 24; r2 = 4 + 1 = 5; compare 5 <= 5 (true)

6

Fifth loop iteration

r1 = 24 * 5 = 120; r2 = 5 + 1 = 6; compare 6 <= 5 (false), exit loop

Counter (r2)Result (r1)
11
22
36
424
5120
💡

Why This Works

Step 1: Initialize registers

We set r0 to the input number, r1 to 1 for the factorial result, and r2 to 1 as the multiplier counter.

Step 2: Multiply and increment

Inside the loop, multiply r1 by r2 to accumulate the factorial, then increase r2 by 1.

Step 3: Loop control

Compare r2 with the input r0; if r2 is less or equal, repeat the loop to continue multiplying.

Step 4: Return result

When the counter exceeds the input, the loop ends and r1 holds the factorial result.

🔄

Alternative Approaches

Recursive factorial using stack
arm_architecture
    .global _start
_start:
    mov r0, #5          @ Input number
    bl factorial
    mov r7, #1          @ exit syscall
    svc #0

factorial:
    cmp r0, #1
    ble end_factorial
    push {r0, lr}
    sub r0, r0, #1
    bl factorial
    pop {r1, lr}
    mul r0, r0, r1
    bx lr
end_factorial:
    mov r0, #1
    bx lr
Uses recursion and stack, more complex but demonstrates function calls; slower and uses more memory.
Using decrementing loop
arm_architecture
    .global _start
_start:
    mov r0, #5          @ Input number
    mov r1, #1          @ Result
loop:
    mul r1, r1, r0
    subs r0, r0, #1
    bne loop
    mov r0, r1
    mov r7, #1
    svc #0
Counts down from input to 1, simpler loop control but modifies input register.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops from 1 to n, performing one multiplication per iteration, so time grows linearly with input size.

Space Complexity

Uses a fixed number of registers, so space is constant regardless of input size.

Which Approach is Fastest?

The iterative loop approach is fastest and simplest; recursion adds overhead and uses stack memory.

ApproachTimeSpaceBest For
Iterative loopO(n)O(1)Simple and efficient factorial calculation
Recursive functionO(n)O(n)Demonstrating recursion but less efficient
Decrementing loopO(n)O(1)Alternative loop style, modifies input register
💡
Use a loop with a counter and multiply step-by-step to calculate factorial efficiently in ARM assembly.
⚠️
Beginners often forget to initialize the result register to 1, causing incorrect factorial results.