0
0
Cnc-programmingProgramBeginner · 2 min read

ARM Assembly Program to Find Largest of Three Numbers

An ARM assembly program to find the largest of three numbers loads the numbers into registers, compares them using cmp and bgt instructions, and stores the largest value in a register; for example: ldr r0, =num1; ldr r1, =num2; ldr r2, =num3; cmp r0, r1; bgt check_r0_r2; mov r3, r1; b check_end; check_r0_r2: cmp r0, r2; bgt set_r0; mov r3, r2; b check_end; set_r0: mov r3, r0; check_end: where r3 holds the largest number.
📋

Examples

Inputnum1=5, num2=3, num3=7
OutputLargest number is 7
Inputnum1=10, num2=20, num3=15
OutputLargest number is 20
Inputnum1=8, num2=8, num3=8
OutputLargest number is 8
🧠

How to Think About It

To find the largest of three numbers, compare the first two numbers and keep the larger one. Then compare this larger number with the third number. The result of the second comparison is the largest number among all three.
📐

Algorithm

1
Load the three numbers into registers.
2
Compare the first number with the second number.
3
Keep the larger of the two.
4
Compare the kept number with the third number.
5
Keep the larger number from this comparison.
6
Return or store the largest number.
💻

Code

arm_architecture
    .data
num1: .word 5
num2: .word 3
num3: .word 7
result: .word 0

    .text
    .global _start
_start:
    ldr r0, =num1
    ldr r1, [r0]
    ldr r0, =num2
    ldr r2, [r0]
    ldr r0, =num3
    ldr r3, [r0]

    cmp r1, r2
    bgt check_r1_r3
    mov r4, r2
    b check_end

check_r1_r3:
    mov r4, r1

check_end:
    cmp r4, r3
    bgt store_result
    mov r4, r3

store_result:
    ldr r0, =result
    str r4, [r0]

    /* Exit syscall for Linux */
    mov r7, #1
    mov r0, #0
    svc #0
Output
Program stores 7 in 'result' memory location for input 5,3,7
🔍

Dry Run

Let's trace input num1=5, num2=3, num3=7 through the code

1

Load numbers

r1=5, r2=3, r3=7

2

Compare r1 and r2

5 > 3 is true, so r4 = 5

3

Compare r4 and r3

5 > 7 is false, so r4 = 7

4

Store result

Store 7 in memory location 'result'

StepRegister/MemoryValue
1r1, r2, r35, 3, 7
2r4 after cmp r1,r25
3r4 after cmp r4,r37
4result memory7
💡

Why This Works

Step 1: Load numbers into registers

The program loads each of the three numbers from memory into registers r1, r2, and r3 to prepare for comparison.

Step 2: Compare first two numbers

Using cmp and bgt, it checks if r1 is greater than r2 and stores the larger in r4.

Step 3: Compare with third number

It then compares the current largest (in r4) with r3 and updates r4 if r3 is larger.

Step 4: Store the largest number

Finally, it stores the largest number from r4 back into memory for later use or output.

🔄

Alternative Approaches

Using conditional execution instructions
arm_architecture
    ldr r0, =num1
    ldr r1, [r0]
    ldr r0, =num2
    ldr r2, [r0]
    ldr r0, =num3
    ldr r3, [r0]

    mov r4, r1
    cmp r2, r4
    movgt r4, r2
    cmp r3, r4
    movgt r4, r3

    ldr r0, =result
    str r4, [r0]
This approach uses ARM's conditional move instructions <code>movgt</code> to reduce branches, making code faster and cleaner.
Using stack and loop (more complex)
arm_architecture
    ldr r0, =num1
    ldr r1, [r0]
    ldr r0, =num2
    ldr r2, [r0]
    ldr r0, =num3
    ldr r3, [r0]

    push {r1, r2, r3}
    pop {r4, r5, r6}

    cmp r4, r5
    movgt r7, r4
    movle r7, r5
    cmp r7, r6
    movgt r7, r7
    movle r7, r6

    ldr r0, =result
    str r7, [r0]
This method uses stack operations and multiple comparisons but is more complex and less efficient for this simple task.

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

Time Complexity

The program performs a fixed number of comparisons and moves, so it runs in constant time regardless of input.

Space Complexity

Only a few registers and a small fixed memory space are used, so space complexity is constant.

Which Approach is Fastest?

Using conditional execution instructions is fastest because it avoids branching, reducing pipeline stalls.

ApproachTimeSpaceBest For
Basic compare and branchO(1)O(1)Simple and clear logic
Conditional executionO(1)O(1)Faster execution with fewer branches
Stack and loopO(1)O(1)More complex, less efficient for small fixed inputs
💡
Use ARM's conditional execution instructions like movgt to simplify comparisons and reduce branching.
⚠️
Beginners often forget to load the actual values from memory addresses before comparing, leading to wrong results.