Files
cube/apps/simpleasm/frontend/src/lib/levels/09.yaml
T

53 lines
1.3 KiB
YAML
Raw Normal View History

id: 9
title: Loops
subtitle: The power of repetition
description: Use branches to build a loop
tutorial:
- title: What is a loop?
text: >
A loop runs the same code **over and over**. In assembly, a loop is just
**branching back to a label**!
- title: Loop structure
text: >
① initialize ② do work ③ update the counter ④ test + branch back:
code: |
MOV R4, #0 ; ① initialize
loop: ; loop start
ADD R4, R4, #1 ; ②③ counter += 1
CMP R4, #5 ; ④ reached 5?
BLE loop ; not yet, jump back
XHLT
- title: Watch out!
text: >
Forget to update the counter and you've made an **infinite loop**
(don't worry — execution stops automatically after 10000 steps).
goal: Compute **1+2+3+...+10** into **R0** (the answer is 55)
initialState: {}
testCases:
- init: {}
expected:
registers:
R0: 55
hints:
- "R0 accumulates the sum, R4 is the counter (1 to 10)"
- "Loop body: ADD R0, R0, R4 / ADD R4, R4, #1 / CMP R4, #10 / BLE loop"
- "Full: MOV R0, #0 / MOV R4, #1 / loop: ADD R0, R0, R4 / ADD R4, R4, #1 / CMP R4, #10 / BLE loop / XHLT"
starThresholds: [7, 9, 12]
starterCode: |
; Compute 1+2+3+...+10
; Result goes in R0
;
; Tip: use a register as the counter
XHLT
showMemory: false