COA Notes
Practical assembly language programs demonstrating common algorithms and programming patterns.
Introduction
The best way to learn assembly is through complete, working programs. This page presents progressively complex examples that demonstrate how common programming tasks translate to assembly instructions. Each example includes the algorithm logic, the complete code, and a trace showing how registers change during execution. These are not just code snippets — they are complete programs you can run in MARS or any MIPS simulator.
Example 1: Swap Two Numbers
The simplest demonstration of register operations:
.data
x: .word 25
y: .word 42
msg1: .asciiz "Before swap: x="
msg2: .asciiz ", y="
msg3: .asciiz "\nAfter swap: x="
newline: .asciiz "\n"
.text
.globl main
main:
# Load values
lw $t0, x # $t0 = 25
lw $t1, y # $t1 = 42
# Swap using XOR (no temporary needed!)
xor $t0, $t0, $t1 # $t0 = 25 XOR 42
xor $t1, $t0, $t1 # $t1 = (25 XOR 42) XOR 42 = 25
xor $t0, $t0, $t1 # $t0 = (25 XOR 42) XOR 25 = 42
# Store back
sw $t0, x # x = 42
sw $t1, y # y = 25
# Print result
la $a0, msg3
li $v0, 4
syscall
move $a0, $t0
li $v0, 1
syscall
la $a0, msg2
li $v0, 4
syscall
move $a0, $t1
li $v0, 1
syscall
li $v0, 10
syscallArchitecture insight: XOR swap uses no memory, no temporary register — pure ALU operations.
Example 2: Fibonacci Sequence
Generate first N Fibonacci numbers:
Output: 0 1 1 2 3 5 8 13 21 34
Example 3: Bubble Sort
Architecture insight: Notice the nested loops generate many branches — this is where branch prediction matters. Also, the array accesses show indexed addressing in action.
Example 4: String Length (Pointer Traversal)
Architecture insight: This demonstrates register indirect addressing with auto-increment — the pointer walks through memory byte by byte.
Example 5: Power Function (Recursive)
Calculate base^exponent using recursion:
.text
.globl main
main:
li $a0, 2 # base = 2
li $a1, 10 # exponent = 10
jal power # Call power(2, 10)
move $a0, $v0 # Print result
li $v0, 1
syscall # Should print 1024
li $v0, 10
syscall
# power(base, exp): returns base^exp
power:
addi $sp, $sp, -12
sw $ra, 8($sp)
sw $a0, 4($sp) # Save base
sw $a1, 0($sp) # Save exponent
# Base case: exp == 0 → return 1
beq $a1, $zero, power_base
# Recursive: return base * power(base, exp-1)
addi $a1, $a1, -1 # exp - 1
jal power # Recursive call
lw $a0, 4($sp) # Restore base
mul $v0, $a0, $v0 # base * power(base, exp-1)
j power_return
power_base:
li $v0, 1 # Return 1
power_return:
lw $ra, 8($sp)
addi $sp, $sp, 12
jr $raExample 6: Binary Search
Key Takeaways
- Complete assembly programs require data declarations (.data), code (.text), system calls for I/O, and proper program termination
- Array operations in assembly are explicit pointer arithmetic: base_address + index × element_size
- Loops use branch instructions for both the condition check and the back-jump
- Recursive functions require careful stack management — saving and restoring $ra and arguments on each call
- String operations work byte-by-byte using load byte (lb) and pointer increment
- These patterns (loops, conditionals, function calls, array access) are the building blocks of ALL programs — understanding them in assembly reveals exactly what the CPU does
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Assembly Program Examples.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Computer Organization & Architecture topic.
Search Terms
computer-organization, computer organization & architecture, computer, organization, assembly, language, program, examples
Related Computer Organization & Architecture Topics