ARM Assembly
A collection of ARM 32-bit assembly learning resources: a programming guide in PDF format and corrected exercise solutions with complete assembly code.
ARM 32-bit Assembly Programming Guide
Read the guide in your browser or download the PDF for offline study.
ARM 32bit Assembly Exercises
All exercises and their complete source code are included below. Expand a program to inspect its solution.
Program 1: Sum 1 to 5
Question: Write an ARM assembly program to calculate the sum of the integers from 1 through 5 and return the result.
Expected: 15
.section .text
.global _start
_start:
mov r0, #0
mov r1, #1
mov r2, #5
loop:
add r0, r0, r1
add r1, r1, #1
cmp r1, r2
ble loop
exit:
mov r7, #1
svc #0
Program 2: Max element
Question: Given an integer array, write an ARM assembly program to find and return its maximum element.
Expected: 23
.section .data
nums: .word 4, 17, 9, 23, 5
len: .word 5
.section .text
.global _start
_start:
ldr r3, =nums
ldr r2, =len
ldr r2, [r2]
ldr r0, [r3]
mov r1, #1
loop:
cmp r1, r2
bge exit
ldr r4, [r3, r1, LSL #2]
cmp r4, r0
movgt r0, r4
add r1, r1, #1
b loop
exit:
mov r7, #1
svc #0
Program 3: Sum of all elements
Question: Given an integer array, write an ARM assembly program to calculate the sum of all its elements.
Expected: 30
.section .data
nums: .word 2, 4, 6, 8, 10
len: .word 5
.section .text
.global _start
_start:
mov r0, #0
mov r1, #0
ldr r2, =len
ldr r2, [r2]
ldr r3, =nums
arr_loop:
cmp r1, r2
bge exit
ldr r4, [r3, r1, LSL #2]
add r0, r0, r4
add r1, r1, #1
b arr_loop
exit:
mov r7, #1
svc #0
Program 4: Sum elements divisible by 3
Question: Given an integer array, write an ARM assembly program to add only the elements that are divisible by 3.
Expected: 24. Requires an ARM CPU supporting sdiv.
.section .data
nums: .word 3, 5, 9, 10, 12, 14
len: .word 6
.section .text
.global _start
_start:
mov r0, #0
mov r1, #0
ldr r2, =len
ldr r2, [r2]
mov r5, #3
ldr r3, =nums
arr_loop:
cmp r1, r2
bge exit
ldr r4, [r3, r1, LSL #2]
sdiv r6, r4, r5
mul r6, r6, r5
subs r6, r4, r6
bne next_loop
add r0, r0, r4
next_loop:
add r1, r1, #1
b arr_loop
exit:
mov r7, #1
svc #0
Program 5: Minimum element
Question: Given an integer array that may contain negative values, write an ARM assembly program to find its minimum element.
Expected: -8 (exit status 248)
.section .data
nums: .word 7, -3, 12, -8, 5
len: .word 5
.section .text
.global _start
_start:
ldr r2, =len
ldr r2, [r2]
ldr r3, =nums
ldr r0, [r3]
mov r1, #1
arr_loop:
cmp r1, r2
bge exit
ldr r4, [r3, r1, LSL #2]
cmp r4, r0
movlt r0, r4
add r1, r1, #1
b arr_loop
exit:
mov r7, #1
svc #0
Program 6: Maximum absolute value
Question: Given an integer array containing positive and negative values, write an ARM assembly program to find the largest absolute value among its elements.
Expected: 22. The loop comparison must be cmp r1, r2, not cmp r2, r1.
.section .data
nums: .word 4, -15, 9, -22, 7
len: .word 5
.section .text
.global _start
_start:
mov r1, #0
ldr r2, =len
ldr r2, [r2]
ldr r3, =nums
ldr r4, [r3, r1, LSL #2]
cmp r4, #0
rsblt r0, r4, #0
movge r0, r4
add r1, r1, #1
arr_loop:
cmp r1, r2
bge exit
ldr r4, [r3, r1, LSL #2]
cmp r4, #0
rsblt r5, r4, #0
movge r5, r4
cmp r5, r0
movgt r0, r5
add r1, r1, #1
b arr_loop
exit:
mov r7, #1
svc #0
Program 7: Index of maximum value
Question: Given an integer array, write an ARM assembly program to find the index of its maximum element. Use zero-based indexing.
Expected: 3
.section .data
nums: .word 6, 18, 3, 25, 10
len: .word 5
.section .text
.global _start
_start:
ldr r2, =len
ldr r2, [r2]
ldr r3, =nums
ldr r0, [r3]
mov r5, #0
mov r1, #1
arr_loop:
cmp r1, r2
bge exit
ldr r4, [r3, r1, LSL #2]
cmp r4, r0
ble next_loop
mov r0, r4
mov r5, r1
next_loop:
add r1, r1, #1
b arr_loop
exit:
mov r0, r5
mov r7, #1
svc #0
Program 8: Count values divisible by 4
Question: Given an integer array, write an ARM assembly program to count how many elements are divisible by 4.
Expected: 4
.section .data
nums: .word 4, 7, 12, 16, 5, 20
len: .word 6
.section .text
.global _start
_start:
mov r0, #0
mov r1, #0
ldr r2, =len
ldr r2, [r2]
ldr r3, =nums
arr_loop:
cmp r1, r2
bge exit
ldr r4, [r3, r1, LSL #2]
ands r5, r4, #3
bne next_loop
add r0, r0, #1
next_loop:
add r1, r1, #1
b arr_loop
exit:
mov r7, #1
svc #0
Program 9: Index of first even element
Question: Given an integer array, write an ARM assembly program to find the zero-based index of the first even element. Return -1 if no even element exists.
Expected: 3
.section .data
nums: .word 7, 9, 5, 12, 3, 8
len: .word 6
.section .text
.global _start
_start:
mov r0, #-1
mov r1, #0
ldr r2, =len
ldr r2, [r2]
ldr r3, =nums
arr_loop:
cmp r1, r2
bge exit
ldr r4, [r3, r1, LSL #2]
ands r5, r4, #1
bne next_loop
mov r0, r1
b exit
next_loop:
add r1, r1, #1
b arr_loop
exit:
mov r7, #1
svc #0
Program 10: Last index of element greater than 10
Question: Given an integer array, write an ARM assembly program to find the zero-based index of the last element greater than 10. Return -1 if no such element exists.
Expected: 5
.section .data
nums: .word 4, 7, 3, 15, 8, 20
len: .word 6
.section .text
.global _start
_start:
mov r0, #-1
mov r1, #0
ldr r2, =len
ldr r2, [r2]
ldr r3, =nums
arr_loop:
cmp r1, r2
bge exit
ldr r4, [r3, r1, LSL #2]
cmp r4, #10
ble next_loop
mov r0, r1
next_loop:
add r1, r1, #1
b arr_loop
exit:
mov r7, #1
svc #0
Program 11: Count negative numbers
Question: Given an integer array, write an ARM assembly program to count the number of negative elements.
Expected: 3
.section .data
nums: .word 5, -3, 12, -7, 9, -1
len: .word 6
.section .text
.global _start
_start:
mov r0, #0
mov r1, #0
ldr r2, =len
ldr r2, [r2]
ldr r3, =nums
arr_loop:
cmp r1, r2
bge exit
ldr r4, [r3, r1, LSL #2]
cmp r4, #0
bge next_loop
add r0, r0, #1
next_loop:
add r1, r1, #1
b arr_loop
exit:
mov r7, #1
svc #0
Program 12: Smallest negative number
Question: Given an integer array, write an ARM assembly program to find the smallest (most negative) negative element. Return 0 if the array contains no negative values.
Expected: -7 (exit status 249)
.section .data
nums: .word 5, -3, 12, -7, 9, -1
len: .word 6
.section .text
.global _start
_start:
mov r0, #0
mov r1, #0
ldr r2, =len
ldr r2, [r2]
ldr r3, =nums
arr_loop:
cmp r1, r2
bge exit
ldr r4, [r3, r1, LSL #2]
cmp r4, #0
bge next_loop
cmp r4, r0
bge next_loop
mov r0, r4
next_loop:
add r1, r1, #1
b arr_loop
exit:
mov r7, #1
svc #0
Program 13: Index of smallest negative number
Question: Given an integer array, write an ARM assembly program to find the zero-based index of the smallest negative element. Return -1 if no negative element exists.
Expected: 3
.section .data
nums: .word 5, -3, 12, -7, 9, -1
len: .word 6
.section .text
.global _start
_start:
mov r0, #-1
mov r5, #0
mov r1, #0
ldr r2, =len
ldr r2, [r2]
ldr r3, =nums
arr_loop:
cmp r1, r2
bge exit
ldr r4, [r3, r1, LSL #2]
cmp r4, #0
bge next_loop
cmp r0, #-1
beq update
cmp r4, r5
bge next_loop
update:
mov r5, r4
mov r0, r1
next_loop:
add r1, r1, #1
b arr_loop
exit:
mov r7, #1
svc #0
Program 14: Sum of even numbers
Question: Given an integer array, write an ARM assembly program to calculate the sum of all even elements, including negative even values.
Expected: 24
.section .data
nums: .word 5, -3, 12, -7, 9, -1, 8, 4
len: .word 8
.section .text
.global _start
_start:
mov r0, #0
mov r1, #0
ldr r2, =len
ldr r2, [r2]
ldr r3, =nums
arr_loop:
cmp r1, r2
bge exit
ldr r4, [r3, r1, LSL #2]
tst r4, #1
bne next_loop
add r0, r0, r4
next_loop:
add r1, r1, #1
b arr_loop
exit:
mov r7, #1
svc #0
Program 15: Count multiples of 8
Question: Given an integer array, write an ARM assembly program to count the elements that are multiples of 8.
Expected: 4
.section .data
nums: .word 5, 8, 12, 16, 24, 7, 32
len: .word 7
.section .text
.global _start
_start:
mov r0, #0
mov r1, #0
ldr r2, =len
ldr r2, [r2]
ldr r3, =nums
arr_loop:
cmp r1, r2
bge exit
ldr r4, [r3, r1, LSL #2]
ands r5, r4, #7
bne next_loop
add r0, r0, #1
next_loop:
add r1, r1, #1
b arr_loop
exit:
mov r7, #1
svc #0
Program 16: Structure, set READY bit
Question: Given a structure in memory with an ID field, a value field, and a status field, write an ARM assembly routine that sets bit 0 (READY) in the status field without changing the other status bits.
Expected: 1. Fixed .global _start spacing and pop {lr}.
.section .data
device:
.word 100
.word 55
.word 0
.section .text
.global _start
_start:
ldr r0, =device
bl set_ready
ldr r1, [r0, #8]
mov r0, r1
mov r7, #1
svc #0
set_ready:
push {lr}
ldr r1, [r0, #8]
orr r1, r1, #1
str r1, [r0, #8]
pop {lr}
bx lr
Program 17: Function sum_array
Question: Write an ARM assembly function named sum_array that accepts an array address and its length, then returns the sum of all array elements.
Expected: 100. Fixed array addressing to use loop index r4.
.section .data
nums: .word 10, 20, 30, 40
len: .word 4
.section .text
.global _start
_start:
ldr r0, =nums
ldr r1, =len
ldr r1, [r1]
bl sum_array
mov r7, #1
svc #0
sum_array:
push {r4-r6, lr}
mov r4, #0
mov r5, #0
arr_loop:
cmp r4, r1
bge done
ldr r6, [r0, r4, LSL #2]
add r5, r5, r6
add r4, r4, #1
b arr_loop
done:
mov r0, r5
pop {r4-r6, lr}
bx lr
Program 18: Function max_array
Question: Write an ARM assembly function named max_array that accepts an array address and its length, then returns the maximum element.
Expected: 99. Fixed invalid mov r5, [r0] to ldr r5, [r0] and corrected the max update direction.
.section .data
nums: .word 10, 55, 23, 99, 41
len: .word 5
.section .text
.global _start
_start:
ldr r0, =nums
ldr r1, =len
ldr r1, [r1]
bl max_array
mov r7, #1
svc #0
max_array:
push {r4-r6, lr}
ldr r5, [r0]
mov r4, #1
arr_loop:
cmp r4, r1
bge done
ldr r6, [r0, r4, LSL #2]
cmp r6, r5
ble next_loop
mov r5, r6
next_loop:
add r4, r4, #1
b arr_loop
done:
mov r0, r5
pop {r4-r6, lr}
bx lr
Program 19: Function min_max
Question: Write an ARM assembly function named min_max that accepts an array address and its length, then returns the minimum element in r0 and the maximum element in r1.
Expected: min = 3 (r0), max = 88 (r1). The exit code displays the low 8 bits of r0.
.section .data
nums: .word 42, 7, 19, 3, 88, 25
len: .word 6
.section .text
.global _start
_start:
ldr r0, =nums
ldr r1, =len
ldr r1, [r1]
bl min_max
mov r7, #1
svc #0
min_max:
push {r4-r7, lr}
ldr r6, [r0]
mov r7, r6
mov r4, #1
arr_loop:
cmp r4, r1
bge done
ldr r5, [r0, r4, LSL #2]
cmp r5, r6
movlt r6, r5
cmp r5, r7
movgt r7, r5
add r4, r4, #1
b arr_loop
done:
mov r0, r6
mov r1, r7
pop {r4-r7, lr}
bx lr