ARM Assembly Program to Add Two Numbers
In ARM assembly, you can add two numbers using the
ADD instruction like this: ADD R0, R1, R2 which adds the values in R1 and R2 and stores the result in R0.Examples
InputR1=5, R2=3
OutputR0=8
InputR1=0, R2=0
OutputR0=0
InputR1=10, R2=20
OutputR0=30
How to Think About It
To add two numbers in ARM assembly, you first place the numbers in registers. Then use the
ADD instruction to add the values from two registers and store the result in a third register. This is a simple arithmetic operation done directly by the CPU.Algorithm
1
Load the first number into register R12
Load the second number into register R23
Use the ADD instruction to add R1 and R2, store result in R04
End the program or return the resultCode
arm_architecture
AREA AddTwoNumbers, CODE, READONLY
ENTRY
MOV R1, #5 ; Load first number 5 into R1
MOV R2, #3 ; Load second number 3 into R2
ADD R0, R1, R2 ; Add R1 and R2, store result in R0
; Normally here you would return or end
BX LR ; Return from subroutine
ENDOutput
R0 contains 8 after execution
Dry Run
Let's trace adding 5 and 3 through the code
1
Load first number
R1 = 5
2
Load second number
R2 = 3
3
Add numbers
R0 = R1 + R2 = 5 + 3 = 8
| Step | R1 | R2 | R0 |
|---|---|---|---|
| After MOV R1, #5 | 5 | undefined | undefined |
| After MOV R2, #3 | 5 | 3 | undefined |
| After ADD R0, R1, R2 | 5 | 3 | 8 |
Why This Works
Step 1: Loading numbers into registers
The MOV instruction places immediate values into registers R1 and R2 to prepare for addition.
Step 2: Adding the numbers
The ADD instruction adds the values in R1 and R2 and stores the result in R0.
Step 3: Returning from the program
The BX LR instruction returns control to the calling function or ends the program.
Alternative Approaches
Using immediate value addition
arm_architecture
AREA AddImmediate, CODE, READONLY
ENTRY
MOV R1, #5
ADD R0, R1, #3 ; Add immediate value 3 to R1, store in R0
BX LR
ENDThis method adds a constant directly without loading it into a register first, saving one instruction.
Using stack to pass numbers
arm_architecture
AREA AddWithStack, CODE, READONLY
ENTRY
PUSH {R1, R2}
MOV R1, #5
MOV R2, #3
ADD R0, R1, R2
POP {R1, R2}
BX LR
ENDThis approach saves registers on the stack before use, useful in larger programs to preserve register values.
Complexity: O(1) time, O(1) space
Time Complexity
The addition uses a single CPU instruction, so it runs in constant time regardless of input.
Space Complexity
Only a few registers are used, so space usage is constant and minimal.
Which Approach is Fastest?
Using immediate addition is fastest as it reduces instructions, but loading both numbers into registers is more flexible.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Register Addition | O(1) | O(1) | Adding two variable numbers |
| Immediate Addition | O(1) | O(1) | Adding a constant to a variable |
| Stack Preservation | O(1) | O(1) | Preserving registers in complex programs |
Use
ADD with immediate values to save instructions when one number is constant.Forgetting to load numbers into registers before adding causes incorrect results.