Branching and Loops
Laboratory Manual
The lab manual contains the full theory, circuit diagrams, and requirements for this experiment - keep it open while you work. Download Experiment 3 PDF
Examples
Example 1: Find Maximum Element in Array
This example demonstrates how to find the maximum element in an array using a standard for loop structure.
AREA RESET, CODE, READONLY
EXPORT __Vectors
__Vectors
DCD 0x20001000 ; Initial SP
DCD Reset_Handler ; Reset vector
ALIGN
AREA MYCODE, CODE, READONLY
ENTRY
EXPORT Reset_Handler
; Find maximum element in ARR[0..LEN-1]
; Result (max) is written to MAXRES.
Reset_Handler
LDR R0, =ARR ; R0 = &ARR[0]
MOV R1, #0 ; R1 = i (index)
LDR R2, [R0] ; R2 = max = ARR[0]
for_start
CMP R1, #LEN ; i >= LEN ?
BGE for_end ; yes -> done
LDR R3, [R0, R1, LSL #2]; R3 = ARR[i]
CMP R3, R2 ; if ARR[i] > max
MOVGT R2, R3 ; max = ARR[i]
ADD R1, R1, #1 ; i++
B for_start
for_end
LDR R4, =MAXRES ; store result for easy checking
STR R2, [R4]
STOP B STOP
AREA CONSTANTS, DATA, READONLY
ARR DCD 10, 20, 30, -5, 11, 0
LEN EQU 6
AREA MYDATA, DATA, READWRITE
MAXRES SPACE 4 ; expect 30
ENDCheck: Verify that the maximum element is correctly identified and stored in MAXRES.
IT blocks
MOVGT is a conditional non-branch instruction, which Thumb-2 only allows inside an IT (If-Then) block. The Keil assembler normally inserts the IT for you. Look for it in the Disassembly window when you single-step this example.
Example 2: Count Uppercase Letters in String
This example demonstrates how to process a null-terminated string and count the number of uppercase letters (A-Z) using a while-loop structure.
AREA RESET, CODE, READONLY
EXPORT __Vectors
__Vectors
DCD 0x20001000 ; Initial SP
DCD Reset_Handler ; Reset vector
ALIGN
AREA MYCODE, CODE, READONLY
ENTRY
EXPORT Reset_Handler
; Count uppercase ASCII letters in the null-terminated string MYSTR.
; Result (count) is written to UPPERCOUNT.
Reset_Handler
LDR R0, =MYSTR ; R0 = ptr to string
MOV R1, #0 ; R1 = count
while_next
LDRB R2, [R0], #1 ; R2 = *p++; post-increment pointer
CBZ R2, while_end ; if '\0' -> exit
CMP R2, #'A' ; below 'A'?
BLT while_next
CMP R2, #'Z' ; above 'Z'?
BGT while_next
ADD R1, R1, #1 ; count++
B while_next
while_end
LDR R3, =UPPERCOUNT
STR R1, [R3]
STOP B STOP
AREA CONSTANTS, DATA, READONLY
MYSTR DCB "Hello ARM World!", 0
AREA MYDATA, DATA, READWRITE
UPPERCOUNT SPACE 4 ; expect 5 ('H','A','R','M','W')
ENDCheck: Verify that the program correctly counts the uppercase letters and stores the result in UPPERCOUNT.
Tasks
Task 1: Count Vowels and Non-Vowels in a String
Write a program that scans the following null-terminated string, stored with DCB:
MYSTR DCB "ARM assembly language is important to learn!", 0- Use R0 as the pointer to the current character.
- Count vowels (a, e, i, o, u, upper- or lowercase) in R1.
- Count every other character (consonants, spaces, and punctuation, but not the terminating
0) in R2. - Run the program and record the final values of R1 and R2. As a self-check, R1 + R2 must equal the string length.
Hint: Converting a letter to lowercase first (ORR R3, R3, #0x20) halves the number of comparisons. Think about why this is safe for letters, and why it doesn't turn a space or ! into a vowel.
Task 2: Signed Array Statistics
Given the array of signed words:
ARR DCD 7, -3, 12, -8, 0, 5, -1
LEN EQU 7Using a single loop over the array, compute:
- the sum of all elements in R1,
- the minimum element in R2,
- the number of negative elements in R3.
Then change the condition you used to update the minimum from the signed form (LT/GE) to the unsigned form (LO/HS) and run the program again. Report the new value of R2 and explain why it changed.