Branches, conditionals and loops in MIPS — practice in QtSpim: the wave report, three spells with “and” and “or” and a fight that stops in time (and the hero who ended with negative energy)
The first time I wrote my game’s fight in MIPS, the hero won… and ended with −75 energy. He started with 200 and each enemy cost him as much energy as its HP: 35, 80 and 40 were paid without trouble (200 − 35 − 80 − 40 = 45), but the fourth one had 120. My loop subtracted first and asked afterwards: 45 − 120 = −75, and since it had already paid, it counted it as defeated. A hero with no energy beating a boss. I took the lesson to every loop in this topic: the question goes before doing anything, and in a loop with two conditions, each one gets its own branch.
This practice takes to QtSpim what we saw in the branches, conditionals and loops theory with the same wave of eight enemies (35, 80, 40, 120, 15, 60, 90 and 200 HP). There are three mid-level programs: a report enemy by enemy with an if-else inside a for loop, three spells that question each enemy with “and”, “or” and a mix of both, and the fight with a while loop with a compound condition that can stop early. The data has its awkward cases on purpose: one enemy with exactly 40 HP (right on the hit’s limit) and another with 120 (right on the ice’s limit).
Table of Contents
Before you start
It helps to be clear about:
- That a branch is written with the opposite condition of the if, and that an if-else needs a
j endifat the end of the then part. - How “and” (every opposite to
endif) and “or” (the first one as it is tothen) are written. - The loop template: stop at the top with
beq, read withlw, move the pointer 4 bytes and the index 1, andjback to the start. - The system calls to print:
li $v0, 4with a string in$a0andli $v0, 1with an integer.
If any of these sounds shaky, go over the theory before carrying on.
Load the programs in QtSpim with Reinitialize and Load File every time you change something in Notepad++. If you only press F5, you run the previous version (and go crazy looking for a bug you had already fixed).
Program 1: the wave report
The first program walks through the wave and, for each enemy, prints its number and whether it falls to a 40-point hit or resists. At the end it says how many fall in one hit out of how many there are. What makes it harder: inside the loop there is an if-then-else, so the loop labels (loop, end) live together with the if labels (resists, endif), and enemy number 3 has exactly 40 HP.
The idea
It is the loop-with-if template with two changes: the if has an else and we print every lap. The order of each lap:
- Stop: if
i == n, to the end. - Read the HP of enemy
iinto$t3. - Print “Enemy ” and the number
i + 1(people count from 1; the index, from 0). - If
hp <= hit, print “falls in one hit” and add 1; otherwise print “resists”. - Move pointer and index, and go back up.
Before writing code, think about where each branch goes: the stop beq goes to end, the if bgt goes to resists and the j of the then part goes to endif, which is where the loop’s step starts.
The code
# report.s: the wave report, enemy by enemy
.data
wave: .word 35, 80, 40, 120, 15, 60, 90, 200 # each enemy's HP
n: .word 8 # how many enemies there are
hit: .word 40 # the sword's damage
t_ene: .asciiz "Enemy "
t_fal: .asciiz ": falls in one hit\n"
t_res: .asciiz ": resists\n"
t_tot: .asciiz "Fall in one hit: "
t_of: .asciiz " of "
.text
.globl main
main: la $t0, wave # $t0 = pointer to the current enemy
lw $t1, n # $t1 = 8, how many laps
lw $t5, hit # $t5 = 40
li $t2, 0 # $t2 = i, the index (starts at 0)
li $s0, 0 # $s0 = how many fall in one hit
loop: beq $t2, $t1, end # stop: i == n, no enemies left
lw $t3, 0($t0) # $t3 = enemy i's HP
li $v0, 4 # "Enemy "
la $a0, t_ene
syscall
li $v0, 1 # its number: i + 1 (people count from 1)
addi $a0, $t2, 1
syscall
bgt $t3, $t5, resists # opposite of (hp <= hit): to else
li $v0, 4 # then: it falls in one hit
la $a0, t_fal
syscall
addi $s0, $s0, 1 # one more that falls
j endif # without this, it would also print "resists"
resists:
li $v0, 4 # else: it resists
la $a0, t_res
syscall
endif: addi $t0, $t0, 4 # next word of the array
addi $t2, $t2, 1 # i = i + 1
j loop
end: li $v0, 4 # "Fall in one hit: 3 of 8"
la $a0, t_tot
syscall
li $v0, 1
move $a0, $s0
syscall
li $v0, 4
la $a0, t_of
syscall
li $v0, 1
move $a0, $t1
syscall
li $v0, 10
syscall
The heart of the program is the if-else inside the loop. Let’s look at it on its own:
bgt $t3, $t5, resists # opposite of (hp <= hit): to else
li $v0, 4 # then: it falls in one hit
la $a0, t_fal
syscall
addi $s0, $s0, 1 # one more that falls
j endif # without this, it would also print "resists"
resists:
li $v0, 4 # else: it resists
la $a0, t_res
syscall
endif: addi $t0, $t0, 4 # next word of the array
addi $t2, $t2, 1 # i = i + 1
j loop
bgt $t3, $t5, resistsis the opposite of “hp <= hit”. If the enemy has more than 40, it jumps to else.- If it does not jump, the then part runs: it prints “falls in one hit”, adds 1 and jumps to
endif. Thatjis the only thing that stops the then part from falling into the three instructions of the else. endifis stuck to the loop’s step: both branches of the if end in the same place, which is where the loop prepares the next lap.
Why is the number printed with addi $a0, $t2, 1 and not with move $a0, $t2? Because $t2 goes from 0 to 7 and the report numbers from 1 to 8. And notice it is worked out straight into $a0, without touching $t2: if we did addi $t2, $t2, 1 here, the index would move twice per lap.
The drawing shows the wave exactly as it is in memory: each enemy is a word, which is why the pointer jumps 4 by 4 (+0, +4… +28) while the index goes 1 by 1. The green ones fall in one hit.
Lap by lap:
| Lap | i | $t0 (address) | HP | HP > 40? | Branch | $s0 after |
|---|---|---|---|---|---|---|
| 1 | 0 | 0x10010000 | 35 | no | then | 1 |
| 2 | 1 | 0x10010004 | 80 | yes | else | 1 |
| 3 | 2 | 0x10010008 | 40 | no | then | 2 |
| 4 | 3 | 0x1001000c | 120 | yes | else | 2 |
| 5 | 4 | 0x10010010 | 15 | no | then | 3 |
| 6 | 5 | 0x10010014 | 60 | yes | else | 3 |
| 7 | 6 | 0x10010018 | 90 | yes | else | 3 |
| 8 | 7 | 0x1001001c | 200 | yes | else | 3 |
On the next lap i is 8, the beq jumps to end and the total is printed.
Lap 3 is the edge case, the enemy with 40. This is how the processor walks through it, instruction by instruction, starting with i = 2 and $s0 = 1:
| Instruction | What happens | Why |
|---|---|---|
beq $t2, $t1, end | Does not jump | 2 is not equal to 8: there are enemies left |
lw $t3, 0($t0) | $t3 = 40 | $t0 holds 0x10010008, the third word of the wave |
li $v0, 4 · la $a0, t_ene · syscall | Enemy is printed | Syscall 4 prints the string in $a0 |
li $v0, 1 · addi $a0, $t2, 1 · syscall | 3 is printed | 2 + 1, worked out in $a0; $t2 is still 2 |
bgt $t3, $t5, resists | Does not jump | 40 is not greater than 40: we carry on into the then part |
li $v0, 4 · la $a0, t_fal · syscall | : falls in one hit and the line break are printed | It is the first instruction of the then part |
addi $s0, $s0, 1 | $s0 = 2 | The second enemy that falls in one hit |
j endif | Skips the three instructions of resists | Without it, it would also print “: resists” |
addi $t0, $t0, 4 · addi $t2, $t2, 1 · j loop | $t0 = 0x1001000c, i = 3 | Ready for the enemy with 120 |
On an else lap, the bgt does jump and the five instructions of the then part, j endif included, are not run.
Output
Enemy 1: falls in one hit Enemy 2: resists Enemy 3: falls in one hit Enemy 4: resists Enemy 5: falls in one hit Enemy 6: resists Enemy 7: resists Enemy 8: resists Fall in one hit: 3 of 8
Checking it by hand: 35, 40 and 15 fall, enemies 1, 3 and 5. That is 3 of 8. Enemy 3 is the edge case: 40 is not greater than 40, so the bgt does not jump and it counts as falling. If we had used bge (the opposite of “<“), that enemy would resist and we would get 2.
What if you forgot the addi $t0, $t0, 4?
The loop would still do its 8 laps, but $t0 would stay at 0x10010000 and the lw would always read the first enemy, the one with 35. I ran it: all eight “fall in one hit” and the total is Fall in one hit: 8 of 8.
The numbering comes out right because it depends on $t2, which does move. If a report repeats the same result on every line, check the pointer first.
Questions you might ask
Why does the if use bgt and not ble? Because the branch has to take us to else, which is what happens when the if condition does not hold. The condition is “hp <= hit”; its opposite, “hp > hit”, is bgt.
Can I put the resists: label on the same line as the li? Yes: resists: li $v0, 4 works the same. Here it sits alone on its line because it is long and the code stays aligned. For SPIM a label is the address of the next instruction, wherever it is.
Why does the syscall not spoil $t3 or $t2? The print system calls only read $v0 and $a0. The $t registers stay as they were, so the loop can keep using them after the syscall.
What would happen with a wave of 0 enemies? With n = 0, the first check is already 0 == 0: the beq jumps to end without reading anything and the total is 0 of 0. That is why the stop goes at the top.
Forgetting the j endif of the then part. Without it, the enemies that fall in one hit print “: falls in one hit” and, on the next line, “: resists”. You will see it in snippet 1 of the exercises.
Try it yourself
- Change the hit to 60 (
hit: .word 60) and predict the result before running it: 35, 40, 15 and 60 fall, so 4 of 8. - Add a third case to the report: “is a boss” when the HP is 100 or more (hint: an if-else inside the else).
Program 2: three spells with “and” and “or”
The second program asks each enemy three questions and stores the three counts in memory (with sw) before printing them:
- Two hits:
40 < hp <= 80(an “and”). - Freezable:
hp < 40 or hp >= 120(an “or”; ice works on the small ones and on the very big ones, which are slow). - Potions:
(40 < hp <= 80) or hp >= 200(two-hit enemies and the final boss drop a potion).
What makes it harder: the three questions come one after another inside the same lap, so each one needs its own labels, and the third mixes the “and” with the “or”.
The idea
Each question is an independent if-then whose then part is just an addi of its counter. The key is that the last label of one question is the start of the next one: that way, whatever happens in question 1, we reach question 2. The labels carry the question number (end1, then2, end2, other3, then3, end3) so they do not repeat, because in SPIM a label can only exist once.
Before writing code, draw each condition on a line from 0 to 200 and mark the ranges that meet it. An “and” is a single range; an “or”, two separate ranges.
The code
# spells.s: three questions to each enemy with "and" and "or" conditions
.data
wave: .word 35, 80, 40, 120, 15, 60, 90, 200 # each enemy's HP
n: .word 8 # how many enemies there are
res: .space 12 # +0 two hits, +4 freezable, +8 potions
t_dos: .asciiz "Two-hit enemies: "
t_ice: .asciiz "\nCan be frozen: "
t_pot: .asciiz "\nDrop a potion: "
.text
.globl main
main: la $t0, wave # pointer to the current enemy
lw $t1, n # $t1 = 8
li $t2, 0 # i = 0
li $s0, 0 # two hits: 40 < hp <= 80
li $s1, 0 # freezable: hp < 40 or hp >= 120
li $s2, 0 # potions: (40 < hp <= 80) or hp >= 200
loop: beq $t2, $t1, end
lw $t3, 0($t0) # $t3 = enemy i's HP
# --- question 1, with "and": both opposites jump to the same endif ---
ble $t3, 40, end1 # opposite of (hp > 40)
bgt $t3, 80, end1 # opposite of (hp <= 80)
addi $s0, $s0, 1
end1:
# --- question 2, with "or": the first one jumps AS IT IS to then ---
blt $t3, 40, then2 # hp < 40: good enough
blt $t3, 120, end2 # opposite of (hp >= 120): neither holds
then2: addi $s1, $s1, 1
end2:
# --- question 3, (a and b) or c: if the "and" fails, c is left ---
ble $t3, 40, other3 # a fails: we try c
ble $t3, 80, then3 # a and b: good enough
other3: blt $t3, 200, end3 # opposite of c (hp >= 200)
then3: addi $s2, $s2, 1
end3: addi $t0, $t0, 4 # next enemy
addi $t2, $t2, 1 # i = i + 1
j loop
end: la $t4, res # store the three results in memory
sw $s0, 0($t4)
sw $s1, 4($t4)
sw $s2, 8($t4)
li $v0, 4
la $a0, t_dos
syscall
li $v0, 1
move $a0, $s0
syscall
li $v0, 4
la $a0, t_ice
syscall
li $v0, 1
move $a0, $s1
syscall
li $v0, 4
la $a0, t_pot
syscall
li $v0, 1
move $a0, $s2
syscall
li $v0, 10
syscall
The three questions, together:
# --- question 1, with "and": both opposites jump to the same endif ---
ble $t3, 40, end1 # opposite of (hp > 40)
bgt $t3, 80, end1 # opposite of (hp <= 80)
addi $s0, $s0, 1
end1:
# --- question 2, with "or": the first one jumps AS IT IS to then ---
blt $t3, 40, then2 # hp < 40: good enough
blt $t3, 120, end2 # opposite of (hp >= 120): neither holds
then2: addi $s1, $s1, 1
end2:
# --- question 3, (a and b) or c: if the "and" fails, c is left ---
ble $t3, 40, other3 # a fails: we try c
ble $t3, 80, then3 # a and b: good enough
other3: blt $t3, 200, end3 # opposite of c (hp >= 200)
then3: addi $s2, $s2, 1
end3: addi $t0, $t0, 4 # next enemy
- Question 1 (“and”): both opposites (
ble 40andbgt 80) jump toend1. If both fail, the HP is between 41 and 80, and it is added. - Question 2 (“or”):
blt $t3, 40, then2jumps straight to adding if the HP is less than 40. If not,blt $t3, 120, end2is the opposite of the second part: if it is not >= 120 either, out. - Question 3 (mixed): if the first part of the “and” fails (hp <= 40), the boss part is still left, so it jumps to
other3, not toend3. If the whole “and” holds, it jumps tothen3. If the HP is greater than 80, neither of them jumps and it reachesother3by simply falling through. end1andend2are labels alone on their line: they mark the address of the next instruction, which is the first one of the next question.
Question 3 is the hardest one to follow. Four enemies, each down a different path:
| HP | ble $t3, 40, other3 | ble $t3, 80, then3 | other3: blt $t3, 200, end3 | Added? | Why |
|---|---|---|---|---|---|
| 35 | Jumps to other3 | Not run | Jumps to end3 | No | The “and” fails and it is not the boss either |
| 60 | Does not jump | Jumps to then3 | Not run | Yes | It meets both parts of the “and” |
| 90 | Does not jump | Does not jump | Jumps to end3 | No | It is over 40 but also over 80, and it does not reach 200 |
| 200 | Does not jump | Does not jump | Does not jump | Yes | It reaches then3 by falling through, with no jump at all |
There are two ways to reach then3: jumping from the second ble or falling through from other3. That is why it sits right above the addi.
Look at the end too: la $t4, res and three sw with offsets 0, 4 and 8, the three-word structure we saw in the previous topic.
The drawing answers all three questions at once: each line has painted what meets its condition and the eight enemies as dots. The “and” of the first one is a single range; the “or” of the second one is two ranges with a huge gap in between.
Lap by lap (A = adds, — = does not add):
| Lap | HP | Two hits (40, 80] | Freezable < 40 or >= 120 | Potion (40, 80] or >= 200 |
|---|---|---|---|---|
| 1 | 35 | — (leaves at ble 40) | A (gets in through blt 40) | — (goes through other3) |
| 2 | 80 | A | — (leaves at blt 120) | A (gets in through ble 80) |
| 3 | 40 | — (40 is not > 40) | — (40 is not < 40) | — |
| 4 | 120 | — (leaves at bgt 80) | A (120 is not < 120) | — |
| 5 | 15 | — | A | — |
| 6 | 60 | A | — | A |
| 7 | 90 | — | — | — |
| 8 | 200 | — | A | A (gets in through other3) |
| Total | 2 | 4 | 3 |
Output
Two-hit enemies: 2 Can be frozen: 4 Drop a potion: 3
And the data segment after running it, with the three results stored in the third row, right after the wave and n:
[10010000] 00000023 00000050 00000028 00000078 # . . . P . . . ( . . . x . . . [10010010] 0000000f 0000003c 0000005a 000000c8 . . . . < . . . Z . . . . . . . [10010020] 00000008 00000002 00000004 00000003 . . . . . . . . . . . . . . . .
At 0x10010020 there is n (8) followed by res: 00000002, 00000004 and 00000003. Checking it by hand: two hits, 80 and 60 (2). Freezable, 35, 120, 15 and 200 (4). Potions, 80, 60 and 200 (3). The enemy with 40 gets into none of them: it is neither greater than 40 nor less than 40.
Questions you might ask
Why does the first condition of question 2 go as it is and not with the opposite? Because in an “or” one holding is enough. If the HP is less than 40, we already know the answer and jump straight to the addi. With the opposite to endif, we would lose the second chance.
Why does the first branch of question 3 go to other3 and not to end3? Because the “and” failing decides nothing: the other half of the “or” still has to be checked. Only the last condition, the boss one, can send it to end3.
What if the first branch of question 3 went to end3?
With this data, nothing: I ran it and it still prints Drop a potion: 3. Whatever jumps at the ble $t3, 40 has 40 or less, never 200, so the boss part would not have added it anyway.
But that is a coincidence. With the “Try it yourself” variant, (hp > 40 and hp <= 80) or hp < 20, the last part does hold when the “and” has failed: the enemy with 15. Jumping to end3, it would be lost and you would get Drop a potion: 2. With other3, the code works for any condition you put after it.
Could I reuse the label endif in the three questions? No. SPIM would give an error when loading, because the same label cannot mark three different addresses. That is why each question carries its number.
Why do I not compare with registers, as in program 1? I could, but with fixed numbers (40, 80, 120, 200) it is handier to use the immediate version of the pseudo-instructions: ble $t3, 40, end1. The assembler turns it into an slti with 41 and a beq, as we saw in the theory.
Why are the results stored in .space 12 and not in three .word 0? Both reserve 12 bytes set to zero. .space makes it clearer that it is an area the program is going to fill.
Writing question 2 with both opposites to end2 (bge $t3, 40 and blt $t3, 120), as if it were an “and”. It would only add an enemy that had both less than 40 and 120 or more at the same time, and that enemy does not exist: the counter stays at 0.
Try it yourself
- Add a fourth question: the “rare” enemies, whose HP is exactly 40 or exactly 90 (hint:
beqwith an immediate also exists as a pseudo-instruction). - Change question 3 to
(hp > 40 and hp <= 80) or hp < 20and check by hand that you get 3 (80, 60 and 15).
Program 3: the fight
The third program is the fight from the start, done right: the hero starts with 200 energy and attacks the enemies in order while there are enemies left and he has energy for the next one. Each enemy costs as much energy as its HP. At the end it prints how many he defeated, the energy he has left, the toughest enemy he defeated and why he stopped: if it was because the enemies ran out, “Wave defeated”; if it was the energy, in front of which enemy he got stuck.
What makes it harder: the loop can end for two different reasons, and afterwards we have to find out which one it was.
The idea
- The while has a compound “and” condition: two branches with the opposite to
endw. First the counter one (if there are no enemies left, there is nothing to read) and then the energy one, which needs the HP we have just read. - Only if both hold is the energy paid and the maximum checked (another if, inside the loop).
- Outside the loop, an if-else decides the final message: if
i == n, the enemies ran out; otherwise the energy did. Since the loop does not touch$t3after reading it, its value is still the HP of the enemy that stopped it.
Before writing code, think about the two “whys” of the output. The only value that tells them apart is i: if it reached n, it was not the energy.
The code
# fight.s: I fight while there are enemies left and my energy is enough
.data
wave: .word 35, 80, 40, 120, 15, 60, 90, 200 # each enemy's HP
n: .word 8 # how many enemies there are
energy: .word 200 # the hero's energy at the start
t_def: .asciiz "Defeated: "
t_ene: .asciiz "\nEnergy left: "
t_max: .asciiz "\nToughest defeated: "
t_won: .asciiz "\nWave defeated"
t_none: .asciiz "\nOut of energy before enemy "
t_hp: .asciiz " (hp "
t_par: .asciiz ")"
.text
.globl main
main: la $t0, wave # pointer to the current enemy
lw $t1, n # $t1 = 8
lw $s1, energy # $s1 = 200
li $t2, 0 # i = 0 (at the end, i = defeated)
li $s2, 0 # the toughest defeated (none yet)
while:
beq $t2, $t1, endw # opposite of (i < n)
lw $t3, 0($t0) # enemy i's HP
blt $s1, $t3, endw # opposite of (energy >= hp): early stop
sub $s1, $s1, $t3 # I pay the energy of the fight
ble $t3, $s2, nomax # opposite of (hp > maximum)
move $s2, $t3 # new maximum
nomax: addi $t0, $t0, 4 # next enemy
addi $t2, $t2, 1 # i = i + 1
j while
endw: li $v0, 4 # the report
la $a0, t_def
syscall
li $v0, 1
move $a0, $t2
syscall
li $v0, 4
la $a0, t_ene
syscall
li $v0, 1
move $a0, $s1
syscall
li $v0, 4
la $a0, t_max
syscall
li $v0, 1
move $a0, $s2
syscall
bne $t2, $t1, noenergy # why did it stop? if i != n, because of the energy
li $v0, 4 # then: no enemies left
la $a0, t_won
syscall
j quit
noenergy: li $v0, 4 # else: out of energy
la $a0, t_none
syscall
li $v0, 1 # enemy's number: i + 1
addi $a0, $t2, 1
syscall
li $v0, 4
la $a0, t_hp
syscall
li $v0, 1 # its HP is still in $t3
move $a0, $t3
syscall
li $v0, 4
la $a0, t_par
syscall
quit: li $v0, 10
syscall
The loop, on its own:
while:
beq $t2, $t1, endw # opposite of (i < n)
lw $t3, 0($t0) # enemy i's HP
blt $s1, $t3, endw # opposite of (energy >= hp): early stop
sub $s1, $s1, $t3 # I pay the energy of the fight
ble $t3, $s2, nomax # opposite of (hp > maximum)
move $s2, $t3 # new maximum
nomax: addi $t0, $t0, 4 # next enemy
addi $t2, $t2, 1 # i = i + 1
j while
beq $t2, $t1, endwis the opposite of “i < n”.blt $s1, $t3, endwis the opposite of “energy >= hp”: if the energy is less than the enemy’s HP, it is not enough. This is the early stop: the loop ends before reaching the end of the array.subgoes after both checks. That was the bug at the start: with thesubin front, the hero paid the 120 without having them.ble $t3, $s2, nomax+moveis an if-then that keeps the maximum: if the HP is not greater than the maximum, it skips themove. Since$s2starts at 0, the first enemy defeated is already a maximum.
And the final decision:
bne $t2, $t1, noenergy # why did it stop? if i != n, because of the energy li $v0, 4 # then: no enemies left la $a0, t_won syscall j quit
bne $t2, $t1, noenergy is the opposite of “i == n”: if i did not reach n, it jumps to the energy message. If it did, it prints “Wave defeated” and jumps to quit so it does not print the other message too (the usual j endif, under another name).
The drawing is the loop’s trace: each bar is the energy on reaching an enemy, in coral what it costs and in green what is left. The fourth enemy (120) does not fit in the 45 that are left.
| Lap | i | HP | i < 8? | energy >= HP? | energy | maximum |
|---|---|---|---|---|---|---|
| 1 | 0 | 35 | yes | 200 >= 35, yes | 165 | 35 |
| 2 | 1 | 80 | yes | 165 >= 80, yes | 85 | 80 |
| 3 | 2 | 40 | yes | 85 >= 40, yes | 45 | 80 |
| 4 | 3 | 120 | yes | 45 >= 120, no | 45 | 80 |
Lap 4, the one that stops the fight, instruction by instruction (we arrive with i = 3, $s1 = 45 and $s2 = 80):
| Instruction | What happens | Why |
|---|---|---|
beq $t2, $t1, endw | Does not jump | 3 is not equal to 8: there are still enemies left |
lw $t3, 0($t0) | $t3 = 120 | $t0 holds 0x1001000c, the fourth word |
blt $s1, $t3, endw | Jumps to endw | 45 is less than 120: the energy is not enough |
sub $s1, $s1, $t3 | Not run | The jump has left it behind: $s1 stays at 45 |
The three prints of endw | 3, 45 and 80 are printed | $t2, $s1 and $s2 have not changed since lap 3 |
bne $t2, $t1, noenergy | Jumps to noenergy | 3 is not equal to 8: it stopped because of the energy |
addi $a0, $t2, 1 and move $a0, $t3 | 4 and 120 are printed | The enemy that stopped it: index 3 plus 1, and the HP read on this lap |
Output
Defeated: 3 Energy left: 45 Toughest defeated: 80 Out of energy before enemy 4 (hp 120)
Checking it by hand: 200 − 35 − 80 − 40 = 45, and 45 < 120. He defeated 3, he has 45 left, the toughest was the one with 80 (the one with 40 does not beat it) and he stopped in front of enemy 4 (index 3 + 1) with 120 HP. Enemies 5 to 8 are never read.
Questions you might ask
Why does the counter check go before the energy check? Because the energy check needs to read enemy i‘s HP. If there are no enemies left, lw $t3, 0($t0) would read the word after the array (here, n) and compare it with the energy as if it were an enemy.
Why blt $s1, $t3 and not blt $t3, $s1? Because the while condition is “energy >= hp” and its opposite is “energy < hp”: the first register is the energy. The other way round you would be asking whether the HP is less than the energy, which is exactly when you can attack.
What if the energy is exactly equal to the HP? Then you can attack: the condition is >=, so the blt does not jump, it is paid and the energy stays at 0. With the next enemy, 0 < hp and it stops.
Why do we look at i after the loop and not at the energy? Because the energy says nothing for sure: there could be 45 left both if it stopped at a 120 enemy and if the wave ended with exactly 45 to spare. i == n only happens if all of them were walked through.
What if the hero started with 640 energy, exactly what the wave costs?
35 + 80 + 40 + 120 + 15 + 60 + 90 + 200 = 640. Against the boss there are 200 left, and 200 is not less than 200: the blt does not jump, it is paid and 0 is left. I ran it: Defeated: 8, Energy left: 0, the toughest 200 and “Wave defeated”.
With 639, the first seven cost 440 and 199 are left against the boss: Defeated: 7, 199 energy, the toughest 120 and “Out of energy before enemy 8 (hp 200)”. With 0 energy he won and with 199 he lost: that is why the reason is asked of i.
Subtracting before checking (the hero with −75 from the start). In a while, the check goes at the top and what the body does, below. If you put it at the end, you have a do-while, which always does at least one lap… even if it cannot.
Try it yourself
- Change the energy to 700 and predict the output: the whole wave costs 640, so “Defeated: 8”, 60 energy, the toughest 200 and “Wave defeated”.
- Make the hero recover 10 energy every time he defeats an enemy with 40 or less (one more if-then inside the loop) and work out by hand where he stops now.
Common mistakes in this practice
- The branch with the if condition. The branch takes you to else or endif, so it is written with the opposite:
bgtfor a “<=”,bltfor a “>=”. - A then part without
j endif. The program runs both branches and the report comes out with two lines per enemy. - The “or” written as an “and”. Both opposites to endif: with separate ranges nobody gets in.
- Repeated labels. Three questions with the same
endif:are an error when loading; number them. - Paying before asking. In a while, first the checks and then the body.
- Moving the index twice. Work out
i + 1in$a0, not in$t2.
Try it in QtSpim (or right here)
The fight is the best program to see an early stop live. Open it in QtSpim or here:
Loading the MIPS playground… If it does not show up, copy fight.s into your QtSpim.
- Put a breakpoint on the
sub $s1, $s1, $t3and press F5: it stops three times. At each stop, look at$s1(the energy) in the register panel:c8(200),a5(165) and55(85), before paying. - On the fourth lap, step with F10 from the
blt: the PC jumps straight toendwwithout going through thesub. - Change
energy: .word 200to.word 700and run it: “Wave defeated”. - Swap the order of both checks (first the
lwand theblt, then thebeq) with 700 energy: the result is still right, but look in the Data tab at which word is read on the last lap,0x10010020, which isn. - Load
report.sand remove thej endif: count how many lines the report has now.
Summary
- An if-else inside a loop has two sets of labels: the loop ones (
loop,end) and the if ones (resists,endif). Theendifis usually the start of the loop’s step. - In an “and”, every opposite goes to the exit; in an “or”, the first one goes as it is to then and the last one, with the opposite, to the exit.
- In a mix
(a and b) or c, if the “and” fails you jump to the part of the “or” that is left, not to the exit. - Several ifs in a row in the same lap need different labels: number them.
- A while with “and” is two branches to the exit; first the counter one and then the one that reads from the array.
- If a loop can end for two reasons, you tell them apart afterwards with the index:
i == nonly happens if everything was walked through.
Checklist: have you practised it?
- Have you run the three programs and checked the outputs?
- Can you explain why the enemy with 40 falls in one hit but is neither a “two-hit” enemy nor freezable?
- Can you write the pattern of an “or” with two conditions without looking?
- Do you know why the fight’s
subgoes after both checks? - Have you tried the “Try it yourself” items without looking at the theory?
If you answered yes to almost everything, you are ready for the branches, conditionals and loops exercises, where I no longer give you the code. See you there!
Next step: exercisesBranches, conditionals and loops in MIPS — exercises with the wave’s bosses, three kinds of enemy and the arm’s stamina (and the wave of 6 that had 7 enemies)Did this article help you? Here is how you can support SergioLearns →



