Loading...
Translate a bounded loop into register updates, a comparison and a branch. Check the zero-iteration path before relying on a trace of a typical positive input.
What you'll be able to do
CMP R0,#0 compares the value in R0 with zero and updates the status information used by conditional branches. It does not overwrite R0 with a Boolean result. BEQ branches when the last comparison reports equality; BNE uses inequality, BGT greater than and BLT less than in the stated machine model. B always branches to its target label.
A conditional branch can fall through to the next instruction when its condition is false. Labels identify target instruction locations and do not themselves add an arithmetic step. Read the most recent relevant comparison rather than assuming a branch tests whichever register was most recently mentioned. In these original examples, small non-negative counts avoid signed-overflow complications.
The program computes the sum of integers from 1 to n for a non-negative integer n that fits the example’s arithmetic range. R0 holds the remaining descending count and R1 holds the accumulated sum. It starts R1 at zero, checks whether R0 is zero, adds the current count, decrements it and branches back to the check.
For n=4, the loop-end states (R0,R1) are (3,4), (2,7), (1,9) and (0,10). The invariant is that R1 contains exactly the already processed terms while R0 names the next descending term to process. The final zero comparison exits without adding another term. Verify n=0: the first comparison branches directly to done, preserving the correct empty-sum result zero.
R0 decreases by one on each iteration and cannot pass below zero before the exit check for this input contract. This gives a termination argument for non-negative n. A negative initial count violates that contract and can create a very long wraparound loop in a fixed-width machine. State the valid domain instead of claiming that every integer is handled.
If the comparison were moved after an unconditional first addition, the loop would become a post-test design and the zero case would need separate reasoning. A good trace includes the comparison result, branch decision and relevant register values. A label named done is a useful description, but only HALT actually stops execution. Test off-by-one changes by comparing small results against n(n+1)/2, not just by counting printed source lines.
MOV R0, #4 ; Non-negative count n for this demonstration
MOV R1, #0 ; Running sum
check:
CMP R0, #0
BEQ done
ADD R1, R1, R0
SUB R0, R0, #1
B check
done:
HALTScroll horizontally to see the full code or table.| Completed iteration | Remaining R0 | Sum R1 |
|---|---|---|
| 0 | 4 | 0 |
| 1 | 3 | 4 |
| 2 | 2 | 7 |
| 3 | 1 | 9 |
| 4 | 0 | 10 |
Attempt the task before opening its worked response. Record each change of state, explain the rule used, and check the result against the original requirements. Code examples labelled Python 3 use that language’s conventions; pseudocode states its own assumptions.
Common mistakes to avoid
Key takeaways
Test yourself
Answer a few questions, then review anything you missed.
Start with the quiz above to check the key ideas. You can also choose another topic from the practice library.