MIPS system calls — practice in QtSpim: the character sheet, a class menu that can’t be fooled and 20 points to share out (and the greeting that split over two lines)
My first program with read_string was a greeting. The player types their character’s name and the game welcomes them. Five syscalls, nothing else:
# hook.s: the character's greeting (my first version)
.data
name: .space 16
pname: .asciiz "Name: "
hello: .asciiz "Welcome, "
rest: .asciiz ". Your adventure starts now.\n"
.text
.globl main
main: la $a0, pname
li $v0, 4
syscall
la $a0, name
li $a1, 16
li $v0, 8 # read_string
syscall
la $a0, hello
li $v0, 4
syscall
la $a0, name
li $v0, 4
syscall
la $a0, rest
li $v0, 4
syscall
li $v0, 10
syscall
I typed «Kira» and QtSpim gave me this:
Name: Kira Welcome, Kira . Your adventure starts now.
The full stop and the rest of the sentence had gone down to the next line. My first thought was that I’d left a \n in the rest string, but no: it starts with a full stop. The newline came from inside the name. When I pressed Enter to finish typing «Kira», read_string stored that Enter too, and print_string printed it with everything else. Kira, newline, full stop.
The lesson, which shows up in all three programs today: what the user types isn’t ready to use; first you have to clean it and check it. The Enter in a name, an option that isn’t on the menu, more points than you have left…
This is the practice for the MIPS system calls theory. We’re going to create a character in three medium-level programs: the sheet with name, guild and age; the class menu with description and confirmation; and sharing 20 points among four attributes, with bars drawn on the console. The data is typed the way a real player would: a guild with spaces, an option that isn’t on the menu, a number that goes over the limit. The inputs you see in the outputs are the ones I typed in the single run of each program.
Table of Contents
Before you start
It helps to be clear on:
- The syscall sequence: arguments in
$a0/$a1, code in$v0,syscalland collecting the result before the next one. - That
read_stringneeds a buffer reserved with.spaceand its size in$a1, and that it leaves the\nat the end if it fits. - That
print_intwants the value (lw) andprint_stringthe address (la). - Indexing with
sllfrom topic 4: address = base + i · size.
If any of these sounds shaky, go over the theory before carrying on.
All three programs wait for you to type in the console. In QtSpim the Console window waits with the cursor; in the web playground the «Input» row appears with the Send button. If the program «does nothing», check whether it’s asking you for something.
Program 1: the character sheet
The game asks for the name, the guild and the age, and then shows the sheet with the three things lined up. What makes it tricky: there are two texts read, each with its \n at the end, the guild has spaces («Northern Hawks») and the task asks for the age to be stored in memory, not just in a register.
The idea
All the reads first and everything else afterwards. That way each piece of data is stored in its place as soon as it arrives: the texts already go to their buffer, and the age, with an sw right after its syscall. Then the two texts are cleaned with the loop from the theory (once for the name and once for the guild) and, finally, the sheet is printed with tabs.
Before coding, think about the size of each buffer. How many letters can a name have? And a guild? Put a comfortable number in the .space and the same one in $a1.
The code
# sheet.s: the character sheet (name, guild and age)
.data
name: .space 16 # up to 15 characters
guild: .space 20 # up to 19 characters
age: .word 0
pname: .asciiz "Name: "
pguild: .asciiz "Guild: "
page: .asciiz "Age: "
thead: .asciiz "\n--- Character sheet -------\n"
tname: .asciiz "Name:\t"
tguild: .asciiz "\nGuild:\t"
tage: .asciiz "\nAge:\t"
tend: .asciiz "\n---------------------------\n"
.text
.globl main
main: # --- 1. ask for the data ---
la $a0, pname
li $v0, 4
syscall
la $a0, name # read_string(name, 16)
li $a1, 16
li $v0, 8
syscall
la $a0, pguild
li $v0, 4
syscall
la $a0, guild # read_string(guild, 20)
li $a1, 20
li $v0, 8
syscall
la $a0, page
li $v0, 4
syscall
li $v0, 5 # read_int
syscall
sw $v0, age # to memory before any other syscall
# --- 2. remove the '\n' from the name ---
la $t0, name
q1: lbu $t1, 0($t0)
beq $t1, '\n', f1 # the newline: I swap it for '\0'
beqz $t1, h1 # full string: no newline
addi $t0, $t0, 1
j q1
f1: sb $zero, 0($t0)
h1: # --- 3. the same with the guild ---
la $t0, guild
q2: lbu $t1, 0($t0)
beq $t1, '\n', f2
beqz $t1, h2
addi $t0, $t0, 1
j q2
f2: sb $zero, 0($t0)
h2: # --- 4. print the sheet ---
la $a0, thead
li $v0, 4
syscall
la $a0, tname
li $v0, 4
syscall
la $a0, name
li $v0, 4
syscall
la $a0, tguild
li $v0, 4
syscall
la $a0, guild
li $v0, 4
syscall
la $a0, tage
li $v0, 4
syscall
lw $a0, age # the value stored in memory
li $v0, 1
syscall
la $a0, tend
li $v0, 4
syscall
li $v0, 10
syscall
The cleaning loop is the same twice; only the buffer and the labels change:
# --- 2. remove the '\n' from the name ---
la $t0, name
q1: lbu $t1, 0($t0)
beq $t1, '\n', f1 # the newline: I swap it for '\0'
beqz $t1, h1 # full string: no newline
addi $t0, $t0, 1
j q1
f1: sb $zero, 0($t0)
h1: # --- 3. the same with the guild ---
la $t0, guild
q2: lbu $t1, 0($t0)
beq $t1, '\n', f2
beqz $t1, h2
addi $t0, $t0, 1
j q2
f2: sb $zero, 0($t0)
Each loop has its own labels (q1, f1, h1 and q2, f2, h2) because in MIPS a label can’t be repeated. Repeating the loop is ugly, I know. In the subroutines topic we’ll turn it into a function called twice with a jal; for now, copying and changing labels is the honest way.
This is how the first loop walks through the name «Kira». name starts at 0x10010000, and read_string has left K, i, r, a, the Enter and the ‘\0’ in it:
| Pass | $t0 | Byte read | What it does | Why |
|---|---|---|---|---|
| 1 | 0x10010000 | 75 (‘K’) | addi and j q1 | It isn’t 10 or 0: it’s a letter of the name |
| 2 | 0x10010001 | 105 (‘i’) | addi and j q1 | Same again |
| 3 | 0x10010002 | 114 (‘r’) | addi and j q1 | Same again |
| 4 | 0x10010003 | 97 (‘a’) | addi and j q1 | Last letter |
| 5 | 0x10010004 | 10 (‘\n’) | Jumps to f1 | It’s the Enter I pressed |
| — | 0x10010004 | 0 is written | sb $zero | The name now ends at the a |
The beqz $t1, h1 never jumps with «Kira»: it’s only there for when the name fills the buffer and there’s no room left for the Enter. The guild works the same way, but its Enter is 14 positions past the start, at 0x1001001E.
Output
Name: Kira Guild: Northern Hawks Age: 19 --- Character sheet ------- Name: Kira Guild: Northern Hawks Age: 19 ---------------------------
Checking by hand: «Northern Hawks» has 14 characters (8 + 1 + 5), so with the Enter it’s 15 and with the '\0', 16: it fits comfortably in the .space 20. If the guild had 19 characters or more, read_string would store 19, no Enter, and the loop would leave through beqz without touching anything. And the age: the 19 has been in memory since the sw, and it’s what lw $a0, age reads at the end, long after $v0 has changed a dozen times.
Look at the drawing: the guild starts at 0x10010010 (+16) even though «Kira» only takes 5 bytes. The .space reserves the whole gap, used or not. That’s why every piece of data has a fixed address you can work out before running.
What if the guild didn’t fit in the buffer?
I ran it with «Brotherhood of the Red Dawn», which has 27 characters. read_string stores the first 19, «Brotherhood of the » with the trailing space, and puts the ‘\0’ in byte 20. The cleaning loop finds no 10 and leaves through beqz: the guild is cut short, but the program carries on.
The strange part shows up in the age. «Red Dawn» and its Enter haven’t been read: they stay waiting, and the next read_int finds them before my 19. Since «Red Dawn» isn’t a number, it returns 0, and the sheet says Age: 0. With just one letter too many the same thing happens: only the Enter is left waiting, and read_int reads an empty line. That’s why the .space should be roomy: whatever doesn’t fit isn’t lost, the next read swallows it.
Questions you might ask
Why do I store the age with sw $v0, age and not with move? Because here the task asks for the data to be in memory. The sw does both things at once: it takes the number out of $v0 before the next syscall and leaves it at its label. If you need it in a register later, lw.
Why are all the reads together, before removing the newlines? They could be interleaved (read name, clean, read guild, clean). The result is the same. Grouping them leaves the program in three blocks you can read at a glance: ask, clean, print.
What happens if the user types an empty guild (just Enter)? The buffer holds a \n and a '\0'. The loop finds the \n on the first pass, changes it to 0 and the guild becomes an empty string: the sheet would show «Guild:» with nothing after it. Nothing breaks, but if you wanted to force them to type something, you’d check whether the first byte is 0 and ask again.
Why does "\nGuild:\t" start with the newline? Because that way each line of the sheet is opened by its own string, and the name (which no longer has a \n) stays on its line. It’s the same rule as in the theory’s card.
Removing the newline only from the name. The guild goes at the end of its line and, since "\nAge:\t" comes next and starts with another newline, a blank line appears between «Guild» and «Age». If you see strange gaps in an output, suspect a \n left inside a text that was read.
Try it yourself
- Add a fourth question, «Home town», with its own 24-byte buffer and its own cleaning loop (
q3,f3,h3). - Count how many letters the name has (the cleaning loop already walks the string: use
$t0when it exits) and print «Name: Kira (4 letters)».
Program 2: the class menu
Now the player chooses a class: Warrior, Mage or Archer. The program shows the description of the chosen one and asks whether to keep it; if they say no, back to the menu. What makes it tricky: options that don’t exist have to be rejected, the description has to come from the same position as the name, and the confirmation is a yes/no question, with the Enter trap we saw in the theory.
The idea
Two tables of addresses in the same order: names and descs. The option is validated (1 to 3), turned into an offset (option − 1) · 4 that’s kept in $s1 and used in both tables. The confirmation is read with read_string into an 8-byte buffer and the first letter is checked. The program has a loop with two ways back to the start: the option is invalid, or the player says no.
Before coding, sketch the flow: menu → valid option? → no: message and menu; yes: name and description → confirm? → no: menu; yes: end. Each arrow back is a j menu.
The code
# classes.s: class menu with description and confirmation
.data
ans: .space 8 # y/n answer
names: .word cg, cm, ca # table of names
descs: .word dg, dm, da # table of descriptions (same index)
cg: .asciiz "Warrior"
cm: .asciiz "Mage"
ca: .asciiz "Archer"
dg: .asciiz "Lots of health and heavy armour. Slow."
dm: .asciiz "Area spells. Little health."
da: .asciiz "Attacks from afar and dodges well."
pmenu: .asciiz "\nChoose a class:\n 1) Warrior\n 2) Mage\n 3) Archer\nOption: "
tbad: .asciiz "Invalid option: type 1, 2 or 3.\n"
tsep: .asciiz ": "
pconf: .asciiz "\nKeep this class? (y/n): "
tend: .asciiz "Class confirmed: "
.text
.globl main
main: # --- 1. ask for an option until it is valid ---
menu: la $a0, pmenu
li $v0, 4
syscall
li $v0, 5 # read_int
syscall
move $s0, $v0
blt $s0, 1, bad
bgt $s0, 3, bad
# --- 2. the same offset works for both tables ---
addi $s1, $s0, -1 # index from 0
sll $s1, $s1, 2 # $s1 = (option - 1) * 4
lw $a0, names($s1) # address of the name
li $v0, 4
syscall
la $a0, tsep
li $v0, 4
syscall
lw $a0, descs($s1) # address of the description
li $v0, 4
syscall
# --- 3. confirm: read the whole line and look at the first letter ---
la $a0, pconf
li $v0, 4
syscall
la $a0, ans
li $a1, 8
li $v0, 8 # read_string
syscall
lbu $t0, ans
beq $t0, 'y', okay
beq $t0, 'Y', okay
j menu # anything else: back to the menu
bad: la $a0, tbad
li $v0, 4
syscall
j menu
okay: la $a0, tend
li $v0, 4
syscall
lw $a0, names($s1) # $s1 is still intact: no syscall touches it
li $v0, 4
syscall
li $v0, 10
syscall
The part that does the work with the tables:
blt $s0, 1, bad bgt $s0, 3, bad # --- 2. the same offset works for both tables --- addi $s1, $s0, -1 # index from 0 sll $s1, $s1, 2 # $s1 = (option - 1) * 4 lw $a0, names($s1) # address of the name li $v0, 4 syscall la $a0, tsep li $v0, 4 syscall lw $a0, descs($s1) # address of the description li $v0, 4 syscall
$s1 is worked out once and used three times: for the name, for the description and, at the end, again for the confirmed name. No syscall touches the $s registers (only $v0 and whatever you put in $a0/$a1), so $s1 still holds the same value ten lines later.
Output
Choose a class: 1) Warrior 2) Mage 3) Archer Option: 4 Invalid option: type 1, 2 or 3. Choose a class: 1) Warrior 2) Mage 3) Archer Option: 1 Warrior: Lots of health and heavy armour. Slow. Keep this class? (y/n): n Choose a class: 1) Warrior 2) Mage 3) Archer Option: 2 Mage: Area spells. Little health. Keep this class? (y/n): y Class confirmed: Mage
Check: the 4 is rejected at bgt $s0, 3, bad without touching the tables. With 1, $s1 = (1 − 1) · 4 = 0: the first word of each table, Warrior and its description. I answer «n», the first letter isn’t y or Y and it goes back to the menu. With 2, $s1 = 4: Mage. I answer «y» and «Class confirmed: Mage» appears, read again from names($s1).
The real addresses, to see that the same $s1 lands in both tables. ans takes 8 bytes from 0x10010000, so names starts at 0x10010008 and descs, three words later, at 0x10010014:
| Option | $s1 | names($s1) | descs($s1) | Why |
|---|---|---|---|---|
| 1 | (1 − 1) · 4 = 0 | 0x10010008 → cg | 0x10010014 → dg | First word of each table |
| 2 | (2 − 1) · 4 = 4 | 0x1001000C → cm | 0x10010018 → dm | Second word: Mage |
| 3 | (3 − 1) · 4 = 8 | 0x10010010 → ca | 0x1001001C → da | Third word: Archer |
In both columns, lw adds $s1 to the label’s address. What changes between tables is the base; the offset is the same.
Questions you might ask
Why two tables and not one string with name and description together? Because at the end I need the name on its own («Class confirmed: Mage»). With two tables in the same order, each piece of data is separate and the same index finds them all. You could add a third (each class’s starting health as .word 120, 60, 80) without touching the logic.
Why doesn’t the confirmation use read_char? Because after the menu (a read_int) and before going back to it, any leftover Enter would end up in the next read. Reading the whole line, the Enter stays inside ans and bothers nobody. It also accepts «y», «yes» or «Y» without changing anything.
Why does ans go first in .data? It’s the habit in this topic: buffers in front, at addresses that are easy to find in the Data tab (0x10010000), and the texts behind.
What happens if I answer with an empty line? The buffer holds a \n (10) in its first byte. It isn’t y or Y, so it goes back to the menu. That’s exactly what you want: when in doubt, nothing gets confirmed.
Validating after using the index: working out $s1, loading the name, printing it and then checking whether the option was good. By then lw has already read outside the table. The check goes right after the read_int, before any sum with the option.
What if you removed the blt and the bgt from the validation?
I ran it typing a 4. $s1 = (4 − 1) · 4 = 12, and names + 12 is 0x10010014: that’s no longer the table of names, it’s the first word of descs. The program prints the warrior’s description as if it were a name, Lots of health and heavy armour. Slow.: , and in the next lw, descs + 12 lands on the bytes of «Warr». Read as a word they’re 0x72726157, which isn’t any address in the program, and execution stops with Memory address out of bounds.
With a 0 it’s worse: $s1 = −4 and lw reads the last 4 bytes of ans, which are 0. print_string with address 0 fails straight away, without printing anything.
Try it yourself
- Add a fourth class («Bard», with its description) and change the validation. How many lines outside
.datadid you have to touch? - Add a third table
health: .word 120, 60, 80and print «Starting health: 60» after the description, with the same$s1and anlw(careful: here the table holds the value, not an address).
Program 3: sharing out the points
Last step of the creation: the player has 20 points to share among Strength, Magic, Agility and Health. In each question the program says how many are left and doesn’t accept a negative number or one larger than what’s left. At the end it shows the array and a bar of # per attribute. What makes it tricky: the reading loop doesn’t always move on, because an invalid answer repeats the same question.
The idea
An array stats of 4 words and a table names with the addresses of the four names, all padded with spaces to 9 characters (so the bars start in the same column without using tabs). In the loop, i is only incremented if the value is good. Then two more passes: one to print the array with commas and one for the bars, with a loop inside another (the inner one prints as many # as stats[i] is worth).
Before coding, think about what changes and what doesn’t when the value is invalid. Do you have to undo anything? If the sw and the subtraction come after the checks, no.
The code
# points.s: share 20 points among 4 attributes
.data
stats: .word 0, 0, 0, 0 # strength, magic, agility, health
names: .word sf, sm, sa, sv
sf: .asciiz "Strength " # all 9 characters long: the bars
sm: .asciiz "Magic " # start in the same column
sa: .asciiz "Agility "
sv: .asciiz "Health "
tleft: .asciiz "(points left "
tcol: .asciiz "): "
tbad: .asciiz "You don't have that many points.\n"
tvec: .asciiz "\nStats: ["
comma: .asciiz ", "
tclose: .asciiz "]\n"
.text
.globl main
main: li $s0, 0 # i = 0
li $s2, 20 # points left
ask: beq $s0, 4, see # 4 attributes
sll $s1, $s0, 2 # $s1 = i * 4, for both tables
lw $a0, names($s1) # "Strength "...
li $v0, 4
syscall
la $a0, tleft
li $v0, 4
syscall
move $a0, $s2 # how many are left
li $v0, 1
syscall
la $a0, tcol
li $v0, 4
syscall
li $v0, 5 # read_int
syscall
move $t0, $v0
bltz $t0, bad # negative: not valid
bgt $t0, $s2, bad # more than are left: not valid
sw $t0, stats($s1) # stats[i] = value
sub $s2, $s2, $t0 # left -= value
addi $s0, $s0, 1 # only move on if the value was good
j ask
bad: la $a0, tbad
li $v0, 4
syscall
j ask # same i: ask the same thing again
see: # --- the array on one line ---
la $a0, tvec
li $v0, 4
syscall
li $s0, 0
list: beq $s0, 4, bars
beqz $s0, num # no comma before the first one
la $a0, comma
li $v0, 4
syscall
num: sll $s1, $s0, 2
lw $a0, stats($s1)
li $v0, 1
syscall
addi $s0, $s0, 1
j list
bars: la $a0, tclose
li $v0, 4
syscall
# --- one bar of '#' per attribute ---
li $s0, 0
row: beq $s0, 4, quit
sll $s1, $s0, 2
lw $a0, names($s1) # 9-character name
li $v0, 4
syscall
lw $t1, stats($s1) # how many hashes
hash: beqz $t1, endrow
li $a0, '#'
li $v0, 11 # print_char
syscall
addi $t1, $t1, -1
j hash
endrow: li $a0, '\n'
li $v0, 11
syscall
addi $s0, $s0, 1
j row
quit: li $v0, 10
syscall
The heart of it is the validation:
li $v0, 5 # read_int
syscall
move $t0, $v0
bltz $t0, bad # negative: not valid
bgt $t0, $s2, bad # more than are left: not valid
sw $t0, stats($s1) # stats[i] = value
sub $s2, $s2, $t0 # left -= value
addi $s0, $s0, 1 # only move on if the value was good
j ask
bad: la $a0, tbad
li $v0, 4
syscall
j ask # same i: ask the same thing again
Comparing with 20 instead of with $s2: bgt $t0, 20, bad. I ran it with 6 and 15: both are accepted and −1 are left. The next questions come out with «(points left -1)» and «(points left -4)», and it ends with Stats: [6, 15, 3, 2], which adds up to 26. The limit is what’s left, not what there was at the start.
And the bars, one loop inside another:
li $s0, 0
row: beq $s0, 4, quit
sll $s1, $s0, 2
lw $a0, names($s1) # 9-character name
li $v0, 4
syscall
lw $t1, stats($s1) # how many hashes
hash: beqz $t1, endrow
li $a0, '#'
li $v0, 11 # print_char
syscall
addi $t1, $t1, -1
j hash
endrow: li $a0, '\n'
li $v0, 11
syscall
addi $s0, $s0, 1
j row
The Agility row, with stats[2] = 3, step by step:
| Step | $t1 | What comes out | Why |
|---|---|---|---|
| Name | — | Agility | lw $a0, names($s1) with $s1 = 8 |
lw $t1, stats($s1) | 3 | — | The same $s1 works for stats |
Pass 1 of hash | 3 → 2 | # | 3 isn’t 0: print and subtract |
| Pass 2 | 2 → 1 | # | Same again |
| Pass 3 | 1 → 0 | # | Same again |
beqz $t1, endrow | 0 | The newline | No hashes left |
The inner loop uses up $t1 until it’s 0; the outer one, $s0, isn’t touched, and that’s why the next row starts with Health.
Output
Strength (points left 20): 6 Magic (points left 14): 15 You don't have that many points. Magic (points left 14): 9 Agility (points left 5): 3 Health (points left 2): 2 Stats: [6, 9, 3, 2] Strength ###### Magic ######### Agility ### Health ##
| Question | i | Left | I type | Valid? | Left afterwards |
|---|---|---|---|---|---|
| 1 | 0 | 20 | 6 | Yes | 14 |
| 2 | 1 | 14 | 15 | No: 15 > 14 | 14 |
| 3 | 1 | 14 | 9 | Yes | 5 |
| 4 | 2 | 5 | 3 | Yes | 2 |
| 5 | 3 | 2 | 2 | Yes | 0 |
Five questions for four attributes: the second doesn’t count. Check: 6 + 9 + 3 + 2 = 20, all the points spent, and the bars have 6, 9, 3 and 2 hashes.
Notice that the validation is bgt, not bge. If I had typed 14 for Magic, it would be accepted: 14 isn’t greater than 14. Spending exactly what’s left is fine; going one over isn’t.
Questions you might ask
Why is there a move $t0, $v0 if only comparisons come next? The comparisons don’t touch $v0, so here it would work just the same without the move. I move it to $t0 out of habit: if tomorrow you add a message between the read and the sw (say, «You put 6 into Strength»), that print_string would overwrite $v0 and you’d store a 4.
What if the player doesn’t spend all the points? This program allows it: with «0» in Health you’d finish with unused points. If you want to force them to be spent, the last question can skip asking and assign whatever is left (try it below).
Why do the names have spaces inside the string? To line things up without tabs. «Strength» has 8 letters; with one space it’s 9, and the others are padded to 9. A tab would also work here, but padding with spaces gives you exact control of the column.
What happens if stats[i] is 0? The hash loop exits on the first check (beqz $t1, endrow) and the line has just the name. That’s why the check goes before printing and not after: a loop that prints and then counts would add one hash too many.
Putting the addi $s0, $s0, 1 before the checks. Then the rejected value skips to the next attribute: Magic would stay at 0 and the program would ask about Agility with 14 points. The index only moves on when the data is good.
Try it yourself
- Make Health not be asked: it automatically gets whatever points are left (no read syscall,
sw $s2directly). - At the end, print «Unspent points: N» if there are any, and nothing if they’re 0.
Common mistakes in this practice
- Not removing the
\nfrom a text that was read. Newlines appear in the middle of sentences or blank lines in the sheets. Loop up to 10 andsb $zero. - Using
$v0after another syscall. Anyli $v0(even one to print an error message) wipes what had been read.moveorswas soon as it arrives. - Validating too late. The menu option is checked before working out the offset; an attribute’s value, before storing and subtracting it.
- Moving the index on when the data is invalid. The rejected question has to be repeated with the same
i. $a1different from the.space. If it’s larger,read_stringcan write over whatever comes next; if it’s smaller, it cuts texts that would fit.- Repeated labels when copying a loop. SPIM gives an error when assembling if two lines have the same label: change them all (
q1/q2,f1/f2…).
Try it in QtSpim (or right here)
Sharing out the points is the most fun to type. Open it in QtSpim or in the playground below and try to fool it:
Loading the MIPS playground… If it doesn’t appear, copy points.s into your QtSpim.
- Type a negative number in the first question: it’s rejected and «Strength (points left 20)» appears again.
- Spend all 20 points on Strength and see what happens with the other three: they only accept 0.
- After running, open the Data tab: at 0x10010000 are the four values of
statsand, right behind them, the four addresses of thenamestable. - Set a breakpoint on
sw $t0, stats($s1)and watch$s1at each stop: 0, 4, 8 and 12. On the rejected question it doesn’t stop. - Change
bgt $t0, $s2, badtobge $t0, $s2, bad: now you can’t spend exactly the points you have left.
Summary
- What the user types is cleaned (
\nout) and checked (ranges) before using it. - Each piece of data read is saved as soon as it arrives:
swto its label ormoveto an$sregister. - A
.spacereserves a fixed gap: the data in.datahas addresses you can work out even when the text is short. - With two tables of addresses in the same order, a single offset finds all the data for an option.
- For yes/no questions,
read_stringinto a small buffer and look at the first letter. - In a reading loop with validation, the index only moves on when the data is good.
Checklist: have you practised it?
- Have you run the three programs and checked the outputs?
- Could you explain why my first greeting came out on two lines?
- Could you work out by hand the address where
guildstarts and where the age is in the sheet? - Why can
$s1be used after several syscalls in the menu and$v0can’t? - What would change in the points program if
addi $s0, $s0, 1came before the checks? - Have you tried the «Try it yourself» ideas without looking at the theory?
If you answered yes to almost everything, you’re ready for the system calls exercises, where I no longer give you the code. See you there!
Next step: exercisesMIPS system calls — exercises with a potion shop, a friend code with its check digit and a guild register (and the menu that brought out the final boss with a 7)Did this article help you? Here is how you can support SergioLearns →



