MIPS program structure and data — practice with the inventory map, a game over banner and the same memory read two ways (and the banner I printed with four syscalls)
The week before IC1’s first marked lab session I was building the game’s end screen: a box with “GAME OVER” and the points underneath. I did it the way I knew: one .asciiz string per line and, for each one, its li $v0, 4, its la $a0 and its syscall. Twelve lines of code to print four lines of text. It worked, but when I showed it to a classmate he said: “you can print that with a single syscall”. He was right, and the trick lies in the 0 byte that .asciiz adds at the end and .ascii doesn’t. You’ll see it in program 2.
This practice puts to work what we saw in the MIPS program structure and data theory with three complete programs about the same video game inventory. The first prints the address where each datum ended up and how many bytes it is from the first one, to check alignment with real numbers. The second builds a banner from .ascii pieces and prints it in one go. The third is a detective: it reads the same four bytes as text and as a number and proves that QtSpim is little endian. All three are intermediate level and use data with gaps and mixed sizes, like the ones in the lab exercises.
Table of Contents
Before you start
Make sure you’re clear on:
- What
.dataand.textdo, and that labels are addresses. - How much each directive takes (
.byte1,.half2,.word4,.asciizcharacters + 1) and SPIM’s alignment rule. - The difference between
la(the address) andlw(the value). - Syscalls 1 (print an integer), 4 (print a string) and 10 (end).
If any of them sounds shaky, review the theory before going on.
For these programs, QtSpim must be set up as in the lab: Simulator → Settings → MIPS, with Accept Pseudo Instructions and Load Exception Handler ticked and Bare Machine unticked (the Simple Machine button leaves it like that). And always load with Reinitialize and Load File, so the data of one program don’t mix with the previous one’s. If you’d rather not install anything, the site’s MIPS playground works the same.
Program 1: the inventory memory map
The program prints, for five inventory data, their address in decimal and the distance in bytes from the first one (lives). What makes it tricky is that the data have different sizes (bytes, a half word, words and a string) and, between the string and the last word, SPIM slips in a padding byte that the program has to uncover.
The idea
If labels are addresses, the distance between two data is an address subtraction. We keep the address of lives in a register that isn’t touched for the whole program ($s0) and, for each datum, we do the same thing: la for its address, syscall 1 to print it and sub for the distance. It’s the same recipe five times, changing only the label and the text.
Before programming, work out by hand where each datum should land, using the table in the theory. That way, when you run it, you’ll know whether the output is what you expected or whether you got it wrong (or SPIM did, which it rarely does).
The code
# map.s: prints the address where each inventory datum ended up
# and how many bytes it is from the first one (lives)
.data
lives: .byte 3 # 1 byte
level: .byte 12 # 1 byte
coins: .half 1250 # 2 bytes, at an even address
points: .word 48000 # 4 bytes, at a multiple of 4
weapon: .asciiz "Dagger" # 7 bytes (6 letters + 0 byte)
record: .word 99999 # 4 bytes: there will be padding before it
t_liv: .asciiz "lives -> "
t_coi: .asciiz "coins -> "
t_poi: .asciiz "points -> "
t_wea: .asciiz "weapon -> "
t_rec: .asciiz "record -> "
t_plus: .asciiz " (+"
t_end: .asciiz " bytes)\n"
.text
.globl main
main: la $s0, lives # $s0 = address of lives: everything is measured from here
li $v0, 4 # the name of the datum
la $a0, t_liv
syscall
la $t0, lives # $t0 = address of the datum
li $v0, 1 # the address, as a decimal integer
move $a0, $t0
syscall
li $v0, 4 # " (+"
la $a0, t_plus
syscall
li $v0, 1 # distance = address of the datum - address of lives
sub $a0, $t0, $s0
syscall
li $v0, 4 # " bytes)" and a newline
la $a0, t_end
syscall
li $v0, 4 # the name of the datum
la $a0, t_coi
syscall
la $t0, coins # $t0 = address of the datum
li $v0, 1 # the address, as a decimal integer
move $a0, $t0
syscall
li $v0, 4 # " (+"
la $a0, t_plus
syscall
li $v0, 1 # distance = address of the datum - address of lives
sub $a0, $t0, $s0
syscall
li $v0, 4 # " bytes)" and a newline
la $a0, t_end
syscall
li $v0, 4 # the name of the datum
la $a0, t_poi
syscall
la $t0, points # $t0 = address of the datum
li $v0, 1 # the address, as a decimal integer
move $a0, $t0
syscall
li $v0, 4 # " (+"
la $a0, t_plus
syscall
li $v0, 1 # distance = address of the datum - address of lives
sub $a0, $t0, $s0
syscall
li $v0, 4 # " bytes)" and a newline
la $a0, t_end
syscall
li $v0, 4 # the name of the datum
la $a0, t_wea
syscall
la $t0, weapon # $t0 = address of the datum
li $v0, 1 # the address, as a decimal integer
move $a0, $t0
syscall
li $v0, 4 # " (+"
la $a0, t_plus
syscall
li $v0, 1 # distance = address of the datum - address of lives
sub $a0, $t0, $s0
syscall
li $v0, 4 # " bytes)" and a newline
la $a0, t_end
syscall
li $v0, 4 # the name of the datum
la $a0, t_rec
syscall
la $t0, record # $t0 = address of the datum
li $v0, 1 # the address, as a decimal integer
move $a0, $t0
syscall
li $v0, 4 # " (+"
la $a0, t_plus
syscall
li $v0, 1 # distance = address of the datum - address of lives
sub $a0, $t0, $s0
syscall
li $v0, 4 # " bytes)" and a newline
la $a0, t_end
syscall
li $v0, 10 # end of the program
syscall
The program is long but repetitive: after la $s0, lives come five identical blocks. This is the one for coins, to look at it calmly:
li $v0, 4 # the name of the datum la $a0, t_coi syscall la $t0, coins # $t0 = address of the datum li $v0, 1 # the address, as a decimal integer move $a0, $t0 syscall li $v0, 4 # " (+" la $a0, t_plus syscall li $v0, 1 # distance = address of the datum - address of lives sub $a0, $t0, $s0 syscall li $v0, 4 # " bytes)" and a newline la $a0, t_end syscall
la $t0, coinsleaves in$t0the address of the coins. We don’t want their value (1250): we want where they are.move $a0, $t0+ syscall 1 prints that address as a decimal integer.sub $a0, $t0, $s0works out the distance: address of the coins minus address of the lives. The result goes straight into$a0, which is where the syscall wants it.$s0doesn’t change during the whole program: that’s why we chose it. The$sregisters are the ones used for values that must last.
The coins block, instruction by instruction. $s0 already holds 268500992 (0x10010000, the address of lives) since the first line of main:
| Instructions | What changes | What appears on the console | Why |
|---|---|---|---|
li $v0, 4 · la $a0, t_coi · syscall | $v0 = 4, $a0 = address of t_coi | coins -> | Syscall 4 wants the address of the string in $a0, not the string |
la $t0, coins | $t0 = 268500994 | Nothing | It’s the address of the coins (0x10010002); the 1250 isn’t touched |
li $v0, 1 · move $a0, $t0 · syscall | $v0 = 1, $a0 = 268500994 | 268500994 | Syscall 1 prints whatever is in $a0 in decimal |
li $v0, 4 · la $a0, t_plus · syscall | $a0 = address of t_plus | (+ | Text again: the 4 is needed in $v0 once more |
li $v0, 1 · sub $a0, $t0, $s0 · syscall | $a0 = 268500994 − 268500992 = 2 | 2 | $t0 still holds the address: no syscall has touched it |
li $v0, 4 · la $a0, t_end · syscall | $a0 = address of t_end | bytes) and the newline | Closes the line for the next datum |
Notice that $a0 changes four times in a single block. That’s why the address of the datum is kept in $t0 and not straight in $a0: otherwise we’d have lost it when printing " (+" and the subtraction would have nothing to work with.
Output
lives -> 268500992 (+0 bytes) coins -> 268500994 (+2 bytes) points -> 268500996 (+4 bytes) weapon -> 268501000 (+8 bytes) record -> 268501008 (+16 bytes)
Checking by hand. 268500992 in hexadecimal is 0x10010000: the start of the data, where lives is. From there:
| Datum | Distance | Why |
|---|---|---|
lives | +0 | It’s the first one |
coins | +2 | lives (+0) and level (+1) take one byte each; the next free one, +2, is already even |
points | +4 | The coins take 2 bytes (+2 and +3); +4 is already a multiple of 4 |
weapon | +8 | The points take 4 bytes (+4 to +7); a string needs no alignment |
record | +16 | “Dagger” with its 0 is 7 bytes (+8 to +14); the next free one is +15, which isn’t a multiple of 4: one padding byte and +16 |
The difference between weapon (+8) and record (+16) is 8 bytes, although the string only takes 7. That extra byte is the padding, and this program lays it bare.
What would happen if the coins were a .word instead of a .half?
With coins: .word 1250, the next free byte after level is still +2, but a word needs a multiple of 4: two padding bytes and the coins move to +4. Everything behind them shifts:
| Datum | Before | Now | Why |
|---|---|---|---|
coins | +2 | +4 | +2 and +3 are left as padding |
points | +4 | +8 | The coins now take 4 bytes (+4 to +7) |
weapon | +8 | +12 | The points take +8 to +11; a string isn’t aligned |
record | +16 | +20 | “Dagger” goes from +12 to +18; the next free one, +19, isn’t a multiple of 4: one padding byte and +20 |
I ran it and the last line comes out as record -> 268501012 (+20 bytes). The program doesn’t change a single instruction: since it only uses labels, the assembler works out the new addresses.
Questions you might ask
Why doesn’t level appear in the output? Because we don’t print it, but it’s there: it takes byte +1. If you want to see it, add one more block with la $t0, level; it’ll show +1.
Why are the addresses printed in decimal and not in hexadecimal? Because syscall 1 prints integers in decimal. QtSpim has no syscall to print in hexadecimal; to check, look at register $t0 in the registers panel, which is hexadecimal by default.
What if the label were somewhere else in memory? The program would still work: it doesn’t use any address written by hand. That’s the value of labels: the assembler works out the addresses for you.
What would happen with lw $t0, coins instead of la? It would print a number that isn’t an address, and it would also fail: the coins are at an even address that isn’t a multiple of 4, and lw only reads aligned words. It’s snippet 1 of the exercises for this topic.
Working out the distance with lw instead of la, or subtracting the wrong way round (sub $a0, $s0, $t0), which gives negative numbers. The subtraction is always “datum minus base”.
Try it yourself
- Move
recordright afterpointsand predict the new output before running it. - Change
"Dagger"to"Mace"(4 letters) and explain whyrecordstill lands at +16, but now with 3 padding bytes.
Program 2: the game over banner
The program prints a box with “GAME OVER”, the points line and the weapon line. The box is three lines of text, but they’re printed with a single syscall. What makes it tricky is that in the middle there’s a number (the points), which isn’t text and is printed with another syscall, and some quotes inside a string.
The idea
Syscall 4 prints from the address you give it up to the first 0 byte. If the three lines of the box use .ascii, which adds no 0 byte, they sit next to each other in memory, one after the other, as if they were a single string. Only the last piece, "\tPoints: ", uses .asciiz: that’s where the only 0 is and that’s where the syscall stops. Then we print the number with syscall 1 and the rest with another syscall 4.
Before programming, think about where you want each syscall 4 to stop. Each stop needs its 0 byte, and each 0 byte is an .asciiz.
The code
# banner.s: the game over screen, built from .ascii pieces
# and a single .asciiz at the end: one syscall prints it all at once
.data
banner: .ascii "+----------------+\n" # .ascii: no 0 byte, the next line follows
.ascii "| GAME OVER |\n"
.ascii "+----------------+\n"
.asciiz "\tPoints: " # the only 0 byte: syscall 4 stops here
points: .word 48000
extra: .asciiz "\n\tWeapon: \"Dagger\"\n" # \t tab, \" quotes inside the string
.text
.globl main
main: li $v0, 4 # prints from banner up to the first 0 byte
la $a0, banner
syscall
li $v0, 1 # the points, as an integer
lw $a0, points
syscall
li $v0, 4 # the rest, with its tab and its quotes
la $a0, extra
syscall
li $v0, 10
syscall
Output
+----------------+ | GAME OVER | +----------------+ Points: 48000 Weapon: "Dagger"
The three lines of the box and the start of the points line, with a single syscall: that’s what the .ascii pieces do. The \t comes out as a tab at the start of the last two lines and the \" as real quotes.
This is how memory ends up. Look at the fourth and fifth lines: 0a is the newline, 09 the tab and, after P o i n t s : and the space, the 0 byte at 0x10010042; two bytes later the points word starts:
[10010000] 2d2d2d2b 2d2d2d2d 2d2d2d2d 2d2d2d2d + - - - - - - - - - - - - - - - [10010010] 7c0a2b2d 47202020 20454d41 45564f20 - + . | G A M E O V E [10010020] 20202052 2d2b0a7c 2d2d2d2d 2d2d2d2d R | . + - - - - - - - - - [10010030] 2d2d2d2d 2b2d2d2d 6f50090a 73746e69 - - - - - - - + . . P o i n t s [10010040] 0000203a 0000bb80 6557090a 6e6f7061 : . . . . . . . . W e a p o n [10010050] 4422203a 65676761 000a2272 00000000 : " D a g g e r " . . . . . .
Byte count: each line of the box is 18 characters plus the \n, 19 bytes; the three of them, 57. "\tPoints: " is 9 bytes plus the 0: 67 bytes in total, from 0x10010000 to 0x10010042. The next free one, 0x10010043, isn’t a multiple of 4, so points lands at 0x10010044 (the word 0000bb80) after one padding byte.
The same count, piece by piece:
| Piece | Bytes | Addresses | Why |
|---|---|---|---|
| 1st line of the box | 19 | 0x10010000 to 0x10010012 | 18 characters and the \n; .ascii adds no 0 |
| 2nd line | 19 | 0x10010013 to 0x10010025 | It starts right where the first one ended: there’s no padding between strings |
| 3rd line | 19 | 0x10010026 to 0x10010038 | Its \n is the 0a you see at 0x10010038 |
"\tPoints: " | 10 | 0x10010039 to 0x10010042 | \t is a single byte (09) and .asciiz puts the 0 at the end |
| Padding | 1 | 0x10010043 | What comes next is a .word |
points | 4 | 0x10010044 to 0x10010047 | 48000 = 0xBB80; in memory, 80 bb 00 00 |
extra | 20 | From 0x10010048 | 0x10010048 is already a multiple of 4, and a string isn’t aligned anyway |
To go from bytes to addresses you just add in hexadecimal: 19 bytes are 0x13, so the second line starts at 0x10010000 + 0x13 = 0x10010013.
Questions you might ask
Why isn’t the first line an .asciiz too? Because then the syscall would stop at the end of that line and you’d need four syscalls, which is exactly what I was doing.
Why is points a .word and not a string "48000"? Because it’s a number the game has to be able to do sums with (add points, compare them with the record). As a string it would be five characters, which can’t be added.
What if I drop the .asciiz from "\tPoints: "? The syscall would carry on printing bytes: those of points (80 bb 00 00), which aren’t printable characters, and it would stop at the first 0. You’d get rubbish at the end of the line.
How many bytes does \" take? One: the quote character (34). The backslash only tells the assembler that those quotes don’t close the string.
Printing the points with la $a0, points instead of lw. I tried it: the line comes out as Points: 268501060, which is 0x10010044, the address where the 48000 lives. Syscall 1 prints whatever is in $a0 without asking whether it’s a value or an address.
Forgetting the .asciiz at the end of a string made of .ascii pieces. The syscall doesn’t stop where you want, but at the first 0 it finds, which may be much further on.
Try it yourself
- Add a fourth line
.ascii "| press START |\n"and check that one syscall is still enough. - Change
"\tPoints: "to.asciiand look at what it prints now: where does it stop?
Program 3: the memory detective
The program stores the word 0x544F4F4C, prints it as text with syscall 4 and as a number with syscall 1, and then reads as a number the first four bytes of the string "LOOT". If QtSpim is little endian, the three things have to match. What makes it tricky is that the string has to be aligned so it can be read with lw.
The idea
In little endian, the word 0x544F4F4C stores the bytes 4C 4F 4F 54 in memory, in that order: in ASCII, L, O, O, T. If we put a 0 byte after it, syscall 4 will print it as “LOOT”. And the other way round: the first four bytes of the string "LOOT" read with lw must give the same number.
This is how the data end up in memory, byte by byte:
| Address | Byte | Character | Why |
|---|---|---|---|
| 0x10010000 | 4c | L | In little endian the least significant byte of 0x544F4F4C goes first |
| 0x10010001 | 4f | O | The next byte of the word |
| 0x10010002 | 4f | O | Same again |
| 0x10010003 | 54 | T | The most significant one, last |
| 0x10010004 | 00 | None | The .byte 0: syscall 4 stops here |
| 0x10010005 to 0x10010007 | 00 | None | Padding from .align 2 up to the next multiple of 4 |
| 0x10010008 to 0x1001000c | 4c 4f 4f 54 00 | L, O, O, T and the 0 | The string "LOOT": stored letter by letter in the order you write it |
The four bytes at 0x10010000 and the four at 0x10010008 are identical. The first come from a number and the second from a text, but memory keeps no mark of which was which.
Before programming, think about what each syscall expects to receive: syscall 4 an address (la) and syscall 1 a value (lw). This program uses both on the same label.
The code
# detective.s: the same memory read as text and as a number
# The word 0x544F4F4C stores, in little endian, the bytes 4C 4F 4F 54: "LOOT"
.data
loot: .word 0x544F4F4C # a word written as a number...
.byte 0 # ...and a 0 byte so it can be printed as text
.align 2 # the string starts at a multiple of 4: so lw can read it
str: .asciiz "LOOT" # the same word written as a string
t1: .asciiz "As text: "
t2: .asciiz "\nAs number: "
t3: .asciiz "\nThe string \"LOOT\" as number: "
newline: .asciiz "\n"
.text
.globl main
main: li $v0, 4 # 1) the word printed as text
la $a0, t1
syscall
la $a0, loot # syscall 4 gets the ADDRESS (la)
syscall
li $v0, 4 # 2) the same word printed as a number
la $a0, t2
syscall
li $v0, 1
lw $a0, loot # syscall 1 gets the VALUE (lw)
syscall
li $v0, 4 # 3) the first four bytes of the string, as a number
la $a0, t3
syscall
li $v0, 1
lw $a0, str
syscall
li $v0, 4
la $a0, newline
syscall
li $v0, 10
syscall
Output
As text: LOOT As number: 1414483788 The string "LOOT" as number: 1414483788
Checking by hand: 0x544F4F4C = 5 × 16⁷ + 4 × 16⁶ + 4 × 16⁵ + 15 × 16⁴ + 4 × 16³ + 15 × 16² + 4 × 16 + 12 = 1414483788. And the string "LOOT" gives exactly the same number: they’re the same four bytes in the same order. This is the memory:
[10010000] 544f4f4c 00000000 544f4f4c 20734100 L O O T . . . . L O O T . A s
Questions you might ask
What is the .align 2 for? To make str start at a multiple of 4. Without it, str would land at 0x10010005, right after the 0 byte, and lw can’t read a word that isn’t aligned. This is what happens if you remove it:
As text: LOOT As number: 1414483788 The string "LOOT" as number: Exception 4 [Address error in inst/data fetch] occurred and ignored 0
And in the messages:
Exception occurred at PC=0x00400074 Unaligned address in inst/data fetch: 0x10010005
The program doesn’t stop: the exception handler writes “Exception 4 [Address error in inst/data fetch] occurred and ignored” on the console, carries on, and syscall 1 prints whatever was left in $a0 (a 0). The messages window says where it happened (the PC) and which address it tried to read. In an exam, that 0 is a wrong result.
Why “LOOT” and not “TOOL”? Because the first byte in memory is the least significant one of the word, 4C: the L. If QtSpim were big endian, the same word would print “TOOL”.
What would happen if you wrote the word “in reading order”, .word 0x4C4F4F54?
Memory would hold the bytes 54 4f 4f 4c: the least significant one, 54, goes first, and it’s the T. I ran it with that single change:
As text: TOOL As number: 1280266068 The string "LOOT" as number: 1414483788
The text comes out backwards and the two numbers no longer match: the string "LOOT" hasn’t changed and still gives 1414483788. It’s the slip of reading the word in the order a person would write it. For a .word to print a text, you write it starting with the code of the last letter.
Why doesn’t the string align itself, like words do? Because SPIM only aligns .half and .word. A string can start at any byte; if you’re going to read it with lw, aligning it is up to you.
Can I print loot with syscall 4 without the .byte 0? It would work by chance if the next byte in memory happened to be a 0, but that’s not guaranteed. With the explicit .byte 0 you don’t depend on luck.
Reading a string label with lw without aligning it. QtSpim doesn’t stop the program: it reports the exception, carries on, and the value you print is a 0 that looks like a result.
Try it yourself
- Write the word that prints “GAME” and check it (hint: the bytes are 47 41 4D 45).
- Change
.align 2to.align 3and look in the Data window at wherestrlands now.
Common mistakes in this practice
- Using
lwwherelawas needed (or vice versa): you get a huge number (an address) or a 0 with an exception, never an error when loading. - Forgetting the 0 byte of a string: syscall 4 keeps printing whatever comes after it in memory.
- Not counting the padding when working out addresses: every
.halfand every.wordjumps to its alignment. - Reading with
lwsomething that isn’t aligned to 4, such as a string or a.half: “Unaligned address” in the messages window. - Subtracting addresses the wrong way round: the distance is “datum minus base”.
- Loading with Load File instead of Reinitialize and Load File: the previous program’s data get mixed in and the addresses stop adding up.
Try it in QtSpim (or right here)
The playground below comes with the detective loaded. Use it to step through the three programs (or copy them into your QtSpim):
Loading the MIPS playground… If it doesn’t appear, copy detective.s into your QtSpim.
- Load detective.s and, in the Data tab, find
544f4f4con the first line. In the character column, the same line starts withL O O T. - Step through (F10) until the first
syscalland look at$a0in the registers panel: it holds10010000, an address. At the second print-a-number syscall,$a0holds544f4f4c, a value. - Remove the
.align 2, load again and run: look at the Messages window and the last line of the console. - Paste map.s and check in the Data tab the padding byte at 0x1001000F, the one that makes
recordland at +16.
Summary
- A label is an address:
la+subgive the distance between two data. - SPIM aligns
.half(even address) and.word(multiple of 4) by itself; strings, it doesn’t. - Syscall 4 prints up to the first 0 byte: several
.asciiin a row and one.asciizat the end are a single string. - In little endian, the first byte of a word in memory is the least significant one: 0x544F4F4C reads “LOOT”.
lwonly reads words at addresses that are multiples of 4; otherwise, “Unaligned address” and a useless value.- Syscall 1 wants a value and syscall 4 an address.
Checklist: have you practised it?
- Have you run the three programs and checked the outputs?
- Can you work out by hand where each datum lands, padding included?
- Can you explain why the banner prints with a single syscall?
- Do you know which word prints “GAME” and why?
- Have you seen in the messages window what happens when
lwreads an unaligned address? - Have you tried the “Try it yourself” tasks without looking at the theory?
If you answered yes to almost everything, you’re ready for the MIPS program structure and data exercises, where I no longer give you the code. See you there!
Next step: exercisesMIPS program structure and data — exercises with a character profile, a shop’s memory and a message written only with numbers (and the program that didn’t know how to end)Did this article help you? Here is how you can support SergioLearns →



