Assembly Language Programming(5) Condition, Loop

박서경박서경
4 min read

Condition

.global _start
_start:
    MOV R0, #3      ; Store 3 in R0
    MOV R1, #2      ; Store 2 in R1

    CMP R0, R1      ; Compare R0 and R1 (R0 - R1)

    BGT greater     ; Branch to 'greater' if R0 > R1
    BAL default     ; Always branch to 'default'

greater: 
    MOV R2, #1      ; Set R2 to 1 if R0 > R1

default:
    MOV R2, #2      ; Set R2 to 2 (default case)

1. MOV R0, #3 & MOV R1, #2

  • R0 = 3

  • R1 = 2

2. CMP R0, R1 (Compare)

  • Performs subtraction: R0 - R1

      3 - 2 = 1 (positive result)
    
  • Updates CPSR flags but does not store the result.

3. BGT greater (Branch if Greater)

  • Condition: If R0 > R1, jump to greater.

  • Since 3 > 2 is true, the processor jumps to greater.

4. BAL default (Branch Always)

  • If the condition was false, this instruction would always jump to default.

  • However, since BGT already jumped to greater, BAL default is never executed.

5. greater:

  • Since R0 > R1, execution jumps here.

  • MOV R2, #1R2 is set to 1.

6. default:

  • This label is ignored in this case, as execution already jumped to greater.

  • MOV R2, #2 is never executed.


Result

  • R2 = 1 (since R0 > R1)

CMP & BGT

InstructionMeaningCondition
CMP R0, R1Compare R0 - R1Updates CPSR flags
BGT labelBranch if GreaterIf R0 > R1 (N=0, Z=0, C=1)
BAL labelBranch AlwaysJumps unconditionally

✅ Summary

  • Compares R0 and R1 using CMP R0, R1.

  • If R0 > R1, jumps to greaterR2 = 1.

  • If R0 <= R1, it would jump to default (not executed in this case).

  • Final result: R2 = 1 since 3 > 2. 🚀

Loop

✅ Code Analysis (ARMv7 Assembly)

.global _start
.equ endlist, 0xaaaaaaaa  ; Define `endlist` as a constant with the value 0xAAAAAAAA

_start:
    LDR R0,=list    ; Load the address of `list` into R0
    LDR R3,=endlist ; Load the value of `endlist` (0xAAAAAAAA) into R3
    LDR R1,[R0]     ; Load the first value from `list` into R1
    ADD R2,R2,R1    ; Add R1 to R2 (sum initialization)

loop:
    LDR R1,[R0,#4]! ; Load the next word from `list` into R1 and increment R0 by 4
    CMP R1,R3       ; Compare R1 with `endlist` (0xAAAAAAAA)
    BEQ exit        ; If R1 == endlist, exit the loop
    ADD R2,R2,R1    ; Add R1 to R2 (accumulate sum)
    BAL loop        ; Always branch back to loop

exit:

.data
list:
    .word 1,2,3,4,5,6,6,7,8,9,10  ; Define an array of integers
    scott\0  ; Null-terminated string "scott"

✅ Explanation

1. .equ endlist, 0xaaaaaaaa

  • Defines a constant endlist with the value 0xAAAAAAAA.

  • This is used as a sentinel value to mark the end of the list.

2. LDR R0,=list & LDR R3,=endlist

  • LDR R0,=list: Loads the memory address of list into R0.

  • LDR R3,=endlist: Loads 0xAAAAAAAA into R3 (used for comparison).

3. LDR R1,[R0] & ADD R2,R2,R1

  • Loads the first element (list[0] = 1) into R1.

  • Adds R1 to R2 (initializing sum accumulation).


✅ Loop Execution

4. LDR R1, [R0, #4]!

  • Loads the next element of list into R1.

  • Auto-increments R0 by 4 bytes (moving to the next word in the list).

5. CMP R1, R3

  • Compares R1 (current value from list) with R3 (0xAAAAAAAA).

  • If they are equal, it branches to exit.

6. ADD R2, R2, R1

  • Accumulates the sum of all elements.

7. BAL loop

  • Unconditionally branches back to loop to continue summing.

✅ Exit Condition

8. BEQ exit

  • If the current value (R1) matches 0xAAAAAAAA, the loop exits.

✅ Data Section (.data)

1. list:

list:
    .word 1,2,3,4,5,6,6,7,8,9,10
  • This defines an integer array in memory.

2. scott\0

  • This defines a null-terminated string "scott", where:

      s c o t t \0
    
    • \0 (null terminator) marks the end of the string.

    • This is useful for C-style strings.


✅ Execution Summary

  • Loads and iterates over the list, summing its values.

  • Stops execution when 0xAAAAAAAA is encountered.

  • Utilizes auto-increment (LDR R1, [R0, #4]!) to iterate efficiently.


✅ Final Takeaways

  1. Summation Loop: Iterates over the integer list, adding values to R2.

  2. Sentinel Value (0xAAAAAAAA): Stops execution when encountered.

  3. Efficient Memory Access: Uses pre-indexed addressing ([R0, #4]!).

  4. Mixed Data: Contains both integer array and string (scott\0).

  5. Conditional Branching (CMP, BEQ): Stops execution based on a predefined condition.

This is an efficient method for iterating over a list with a sentinel value. 🚀

0
Subscribe to my newsletter

Read articles from 박서경 directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

박서경
박서경