MIPS 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)

My first QtSpim program printed “Level complete” and, right underneath, in the messages window, this appeared: Attempt to execute non-instruction. The text came out fine, so I thought it was a harmless warning. It wasn’t: when my instructions ran out, the processor carried on reading the memory that came next, which was empty, and ran into something that wasn’t an instruction. I was missing two lines: li $v0, 10 and syscall. A MIPS program doesn’t end on its own; you have to ask it to end. It’s the third snippet of “What’s wrong here?”.

Today we practice with three exercises of increasing difficulty, three snippets to debug, a quiz to check you’ve got it and a cheat sheet to take away. These exercises use the MIPS program structure and data theory and review the practice. You can do them without those, but it’ll be harder.

How to use this article

  1. Read the statement of a level and don’t look at the solution yet.
  2. Write your version in QtSpim (or in the playground at the end of the article), run it and compare your output with the expected one.
  3. If you get stuck, look only at the hint and try again.
  4. When you finish, compare with the solution and notice why it’s done that way, not just that it gives the same output.

No exercise reads from the keyboard: the data are fixed in .data. Always load with Reinitialize and Load File and, to check memory, use QtSpim’s Data window: the addresses and bytes must match the ones in the statement.

Level 1 · Basic: a character profile

Statement. Define in .data, in this order:

  1. name: the string "Aria", with its 0 byte.
  2. lives: a byte holding 5.
  3. gold: a half word holding 300.
  4. exp: a word holding 12500.
  5. backpack: 8 empty bytes for items.
  6. ammo: a word holding 40.

Then the program must print the name, the experience and the ammo, each datum on its own line. Before running it, work out the address where each datum lands and check it in the Data window.

Expected output:

Character: Aria
Experience:  12500
Ammo:     40

For the 8 empty bytes you have .space. The string is printed with syscall 4 and its address (la); the numbers, with syscall 1 and their value (lw). And notice that $v0 doesn’t change by itself: if two syscalls in a row are the same, loading it once is enough.

Level 2 · Intermediate: the shop’s memory

Statement. This program prints nothing: all that matters is memory. Write the data segment of the game shop so that, when loaded into QtSpim, the Data window shows exactly these two lines:

[10010000]    49544f50  00004e4f  00000096  0007000c    P O T I O N . . . . . . . . . .
[10010010]    00c0ffee  00000000  00000000  00000000    . . . . . . . . . . . . . . . .

There must be five labels: shop (the item’s name, as a string), price (a word), stock (a half word), level (a byte) and offer (a word you’ll write in hexadecimal). To finish, li $v0, 10 and syscall are enough.

Read the characters on the right first: the name is there. Then, each 8-digit hexadecimal word is 4 bytes that go from right to left (little endian). 00000096 is 150 and, in 0007000c, there are two data: the 000c on the right comes first in memory.

Level 3 · Challenge: a message written only with numbers

Statement. Write the message "GG WP!", with its newline and its 0 byte, using only .word directives (no string for the message). The program prints the message with syscall 4 and then the first word as a number.

Expected output:

GG WP!
The first word is 1461733191

First, write down the 8 bytes you want in memory, in order: 47 47 20 57 50 21 0A 00 (G, G, space, W, P, !, newline and the 0). Then group them 4 by 4 and, in each group, flip them: the first byte in memory is the least significant one of the word.

What’s wrong here?

Three snippets with one bug each. Before reading the explanation, do two things: predict whether it gives an error and, if it doesn’t, what it prints.

Snippet 1

coins.s

# Snippet 1: reading some coins stored as a .half with lw
        .data
lives:   .byte 3
level:   .byte 12
coins:   .half 1250
        .text
        .globl main
main:   lw   $a0, coins           # I want the coins in $a0
        li   $v0, 1
        syscall
        li   $v0, 10
        syscall

Think before reading on: we want to print the 1250 coins. What comes out on the console?

Explanation. The coins don’t come out:

  Exception 4  [Address error in inst/data fetch]  occurred and ignored
0

And in the messages window:

Exception occurred at PC=0x00400028
  Unaligned address in inst/data fetch: 0x10010002

lives takes 0x10010000 and level 0x10010001, so coins, which is a .half, lands at the next even address: 0x10010002. lw reads 4 bytes and can only do it from an address that is a multiple of 4. 0x10010002 isn’t, so the exception is raised, QtSpim reports it and carries on, and $a0 keeps what it had: a 0.

Why lw fails on coinslives at 0x10010000, level at 0x10010001 and coins, 2 bytes, at 0x10010002. lw needs an address that is a multiple of 4 and 0x10010002 is not: unaligned address exception.03…000c…01e2…0204…0300…0400…0500…0600…07lives · levelcoins (.half)wordwordlw wants 4 bytes from 0x10010002: not a multiple of 40x10010000
The green lines mark where each word starts: lw can only start there.

Reading with lw a datum that isn’t a .word. The size of the instruction must match the size of the datum, and the datum must be aligned.

Fix. If the coins are going to be read with lw, make them a word: SPIM will align it to 0x10010004 by itself.

# Snippet 1 fixed: if you are going to read it with lw, make it a word
        .data
lives:   .byte 3
level:   .byte 12
coins:   .word 1250                 # .word: 4 bytes at an address that is a multiple of 4
        .text
        .globl main
main:   lw   $a0, coins
        li   $v0, 1
        syscall
        li   $v0, 10
        syscall
1250

There’s also lh (load half), which reads a half word from an even address. It’s the other way to fix it, and you’ll see it when we get to the load instructions.

Snippet 2

name.s

# Snippet 2: the character's name and class
        .data
name:   .ascii "Aria"
role:   .ascii "Warrior\n"
        .text
        .globl main
main:   li   $v0, 4                 # I only want to print the name
        la   $a0, name
        syscall
        li   $v0, 10
        syscall

Think before reading on: we only want to print the name. What comes out?

Explanation. Too much comes out:

AriaWarrior

.ascii adds no 0 byte at the end. Syscall 4 starts at name and keeps printing up to the first 0 it finds, so it also swallows the class, which comes right after in memory. Here we were lucky: after "Warrior\n" the memory is zero. If there were another datum, it would come out too, as weird characters.

Using .ascii for a string that is printed on its own. In the practice banner it was exactly what we wanted (gluing pieces together); here, it isn’t.

Fix. .asciiz in both strings (and a newline in the name, so the output looks clean):

# Snippet 2 fixed: .asciiz adds the 0 byte that stops syscall 4
        .data
name:   .asciiz "Aria\n"
role:   .asciiz "Warrior\n"
        .text
        .globl main
main:   li   $v0, 4
        la   $a0, name
        syscall
        li   $v0, 10
        syscall
Aria

Snippet 3

no_end.s

# Snippet 3: a program that prints and... does it end?
        .data
msg:    .asciiz "Level complete\n"
        .text
        .globl main
main:   li   $v0, 4
        la   $a0, msg
        syscall

Think before reading on: does the program end properly?

Explanation. It’s the opening bug. The text comes out:

Level complete

But the messages window shows:

Attempt to execute non-instruction at 0x00400030

After the last syscall, the processor carries on with the next address, 0x00400030, where there’s no instruction any more. It isn’t a harmless warning: it’s the program executing memory that doesn’t belong to it.

Thinking the program ends when it reaches the last line. In MIPS, ending is one more syscall.

Fix. Syscall 10 at the end, always:

# Snippet 3 fixed: the last instruction is always syscall 10
        .data
msg:    .asciiz "Level complete\n"
        .text
        .globl main
main:   li   $v0, 4
        la   $a0, msg
        syscall
        li   $v0, 10                # service 10: end
        syscall
Level complete

Start all your programs from a template that already has the li $v0, 10 and the syscall at the end. That way you’ll never forget them. You’ll find it in the playground at the end of the article.

Solutions

Look at this section only when you’ve tried the exercise. If your code is different but gives the same output (and the same memory), it’s a valid solution too.

Level 1 solution

The approach before the code: first you lay out the memory by hand, datum by datum, and only then do you write the program. There is a single rule: each datum starts at the next free byte, unless its size forces it to jump to an even address (.half) or a multiple of 4 (.word).

DatumDirectiveTakesNext freeAddressWhy
name.asciiz "Aria"5 bytes0x100100000x10010000it’s the first one: the start of .data
lives.byte 51 byte0x100100050x10010005a byte fits at any address
gold.half 3002 bytes0x100100060x10010006it must be even, and 6 already is
exp.word 125004 bytes0x100100080x10010008it must end in 0, 4, 8 or C, and it ends in 8
backpack.space 88 bytes0x1001000C0x1001000C.space doesn’t align: it reserves bytes wherever it is
ammo.word 404 bytes0x100100140x100100140x1001000C + 8 = 0x10010014, which ends in 4

This time not a single padding byte is needed: the order in the statement is designed so that every datum lands already aligned. With the table done, the Data window stops being a surprise and becomes a check.

level1.s

# level1.s: a character's profile (level 1 solution)
        .data
name:     .asciiz "Aria"            # 4 letters + 0 byte = 5 bytes
lives:    .byte 5                   # 1 byte, right after
gold:     .half 300                 # 2 bytes, at an even address (it already is here)
exp:      .word 12500               # 4 bytes: at a multiple of 4
backpack: .space 8                  # 8 empty bytes for items
ammo:     .word 40                  # after .space 8 it is already aligned
t_nam:    .asciiz "Character: "
t_exp:    .asciiz "\nExperience: "
t_amm:    .asciiz "\nAmmo: "
newline:  .asciiz "\n"
        .text
        .globl main
main:   li   $v0, 4                 # "Character: "
        la   $a0, t_nam
        syscall
        la   $a0, name              # the string: syscall 4 wants its ADDRESS
        syscall
        la   $a0, t_exp             # $v0 is still 4
        syscall
        li   $v0, 1                 # the experience: syscall 1 wants the VALUE
        lw   $a0, exp
        syscall
        li   $v0, 4
        la   $a0, t_amm
        syscall
        li   $v0, 1
        lw   $a0, ammo
        syscall
        li   $v0, 4
        la   $a0, newline
        syscall
        li   $v0, 10
        syscall

This is how memory ends up:

[10010000]    61697241  012c0500  000030d4  00000000    A r i a . . , . . 0 . . . . . .
[10010010]    00000000  00000028  72616843  65746361    . . . . ( . . . C h a r a c t e

Why it’s done this way:

  • name takes 0x10010000 to 0x10010004: four letters and their 0 (41 72 69 61, reversed in the word 61697241, and the 00 at the start of the next one).
  • lives lands right after the 0 byte, at 0x10010005: the 05.
  • gold is a .half: the next free one, 0x10010006, is already even, so no padding is needed. That’s why the second word is 012c0500: from right to left, the 0 of the string, the 5 of the lives, and 2c 01, which is 300 (0x012C).
  • exp is a .word: 0x10010008 is already a multiple of 4. 000030d4 is 12500.
  • backpack takes 8 zero bytes, from 0x1001000C to 0x10010013, and ammo lands at 0x10010014, which is already aligned: the 00000028 (40).
  • The second and third syscalls are both string ones, so there’s no need to load $v0 with 4 again between them: it’s still 4.

Why la for the name and lw for the numbers: syscall 1 prints a number, and a number fits in $a0; syscall 4 prints a string, which can be any length and fits in no register, so what it receives is where it starts. The question to ask at every syscall is “does it want the datum or its address?”.

Writing la $a0, exp instead of lw $a0, exp. The program doesn’t fail: it prints Experience: 268501000. That’s 0x10010008 in decimal, the address of exp from the table above. Whenever you see a nine-digit number starting with 2685, it’s an address in the data segment.

Level 2 solution

The approach before writing directives: this exercise goes the opposite way to level 1. You don’t work out the memory from the data, but the data from the memory. Turn each word into bytes in address order (from right to left) and read what’s in each place:

AddressWordBytes in memoryWhat it isWhy
0x1001000049544f5050 4F 54 49P, O, T, Ithe characters on the right confirm it
0x1001000400004e4f4F 4E 00 00O, N, the string’s 0 and a padding bytethe string ends at the first 0; what follows is no longer text
0x100100080000009696 00 00 00price = 1500x96 = 9 × 16 + 6 = 150, and it takes a whole word
0x1001000C0007000c0C 00 07 00stock = 12 and level = 7the half word goes at 0x1001000C and the byte at 0x1001000E; the last 00 is padding
0x1001001000c0ffeeEE FF C0 00offer = 0x00C0FFEEa word, aligned by itself

With the table, the order of the directives follows on its own: string, word, half word, byte and word.

level2.s

# level2.s: the data segment of the game shop (level 2 solution)
        .data
shop:    .asciiz "POTION"           # 6 letters + 0 byte: from 0x10010000 to 0x10010006
         .align 2                   # jumps to 0x10010008
price:   .word 150                  # 0x00000096
stock:   .half 12                   # 0x000c at 0x1001000c
level:   .byte 7                    # at 0x1001000e
         .align 2                   # jumps to 0x10010010
offer:   .word 0x00C0FFEE           # a word written in hexadecimal
        .text
        .globl main
main:   li   $v0, 10                # we only care about the memory
        syscall

Why it’s done this way:

  • The characters on the right say P O T I O N: a 6-letter string, from 0x10010000 to 0x10010005, and its 0 byte at 0x10010006.
  • The first word that isn’t text is 00000096 (150) at 0x10010008, so there’s a padding byte at 0x10010007. Writing .align 2 makes it clear where that padding is; in SPIM it isn’t essential, because the .word would align itself.
  • 0007000c is two data: the 000c (12, the half word) at 0x1001000C and the 07 (the byte) at 0x1001000E. The leftmost 00 is padding.
  • offer is written as is in hexadecimal, .word 0x00C0FFEE, and SPIM stores it at 0x10010010.
  • If yours came out the same but with the directives in a different order, look at the Data window: the order of the labels decides the order in memory, and the statement fixes it.

Putting level before stock. The byte lands at 0x1001000C, the half word jumps to the next even address, 0x1001000E, and the fourth word comes out as 000c0007 instead of 0007000c. The same two data, in a different memory order. And if you declare stock with .word, it takes 4 bytes: the line shows 0000000c and level and offer move one word further on.

With .ascii "POTION" instead of .asciiz the Data window comes out identical, because the 00 at 0x10010006 would be put there anyway by the padding of .align 2. Memory can’t tell a 0 placed on purpose from a padding one. That’s why .asciiz is used: the 0 is guaranteed even if tomorrow you change what comes after it.

Level 3 solution

The approach before the code: the message is turned into bytes before thinking about words, and only at the end are they flipped.

StepWhat you getWhy
The message, character by characterG, G, space, W, P, !, newline, 0the newline and the 0 are bytes too: 8 in total
To ASCII in hexadecimal47 47 20 57 50 21 0A 00it’s what must be in memory, in address order
In groups of 447 47 20 57 · 50 21 0A 00each .word takes 4 bytes: 8 bytes are exactly two words
Each group, flipped0x57204747 · 0x000A2150in little endian the first byte in memory is the least significant one, the one on the right

Why writing the bytes as they are isn’t enough: .word takes a number, not a list of bytes. The one who places the bytes in memory is the assembler, and it does so starting with the least significant one.

level3.s

# level3.s: "GG WP!" written only with .word (level 3 solution)
# Bytes we want, in address order: 47 47 20 57 | 50 21 0A 00
# In little endian, the first byte is the LEAST significant of each word.
        .data
message: .word 0x57204747           # bytes 47 47 20 57 -> "GG W"
         .word 0x000A2150           # bytes 50 21 0A 00 -> "P!\n" and the 0 byte
t_num:   .asciiz "The first word is "
newline: .asciiz "\n"
        .text
        .globl main
main:   li   $v0, 4                 # we print the two words as text
        la   $a0, message
        syscall
        la   $a0, t_num             # and the first one, as a number
        syscall
        li   $v0, 1
        lw   $a0, message
        syscall
        li   $v0, 4
        la   $a0, newline
        syscall
        li   $v0, 10
        syscall

Why it’s done this way:

  • The bytes 47 47 20 57, flipped, are the word 0x57204747; the bytes 50 21 0A 00, the word 0x000A2150.
  • The last byte of the second word is the 0 that syscall 4 needs to stop. We wrote it inside the word, so no separate .byte 0 is needed.
  • The first word as a number is 0x57204747 = 1461733191. If you get another number with the message written correctly, check that you print with lw and not with la.
  • If instead of “GG WP!” you get “W GG”, you wrote the bytes in memory order without flipping them: that would be big endian.

The step that gets skipped most is turning 0x57204747 into decimal. It’s done byte by byte, each one by what its position is worth: 0x57 = 87, 0x20 = 32 and 0x47 = 71, so 87 × 16777216 + 32 × 65536 + 71 × 256 + 71 = 1459617792 + 2097152 + 18176 + 71 = 1461733191.

Putting the 0 at the wrong end: .word 0x0A215000. The bytes in memory end up as 00 50 21 0A, and the 0 that stops syscall 4 comes before the P. The output is GG WThe first word is 1461733191, all on one line: the message stops at “GG W” and the newline never gets printed.

Quiz to practice

Twelve questions to check you’ve got it. It isn’t an exam: it doesn’t count for anything and you can repeat it as many times as you like. Every answer comes with its explanation, including the ones you get right.

Question 1Single choice

In QtSpim, with the lab settings, at which address does the first .data label land?

  1. 0x10010000
  2. 0x00400000
  3. 0x00400024
  4. 0x00000000
Solution

Answer: 0x10010000

Data start at 0x10010000 (268500992 in decimal) and the code of main at 0x00400024, after the start-up instructions.

Question 2Short answer

How many bytes does .asciiz "Potion" take in memory?

Solution

Answer: 7

Six letters, one byte each, plus the 0 byte that .asciiz adds at the end: 7 bytes.

Question 3Single choice

With these data, at which address does gold land?

.data
lives: .byte 3
gold:  .half 300
  1. 0x10010001
  2. 0x10010002
  3. 0x10010004
  4. 0x10010000
Solution

Answer: 0x10010002

SPIM aligns .half to an even address by itself: it leaves a padding byte at 0x10010001.

Question 4True or false

Mark each statement as true or false (QtSpim, lab settings).

  1. A .word is placed at an address that is a multiple of 4 by itself.
  2. An .asciiz string is placed at an address that is a multiple of 4 by itself.
  3. la $t0, x doesn’t read memory: it only works out the address of x.
  4. In QtSpim, the first byte of a word in memory is the most significant one.
  5. Syscall 4 prints from the address in $a0 up to the first 0 byte.
Solution
  • T — A .word is placed at an address that is a multiple of 4 by itself.
  • F — An .asciiz string is placed at an address that is a multiple of 4 by itself.
  • T — la $t0, x doesn’t read memory: it only works out the address of x.
  • F — In QtSpim, the first byte of a word in memory is the most significant one.
  • T — Syscall 4 prints from the address in $a0 up to the first 0 byte.

SPIM only aligns .half and .word; strings start at any byte. QtSpim is little endian: the first byte of a word is the least significant one. la is an address; lw reads the value.

Question 5Fill in the gaps

Fill in with the words from the bank.

To end the program you put service ____ in $v0. To print an integer, service ____, and $a0 holds its ____. To print a string, service ____, and $a0 holds its ____.

Words:

  • name
  • 8
  • 10
  • 4
  • address
  • value
  • 5
  • 1
Solution

To end the program you put service 10 in $v0. To print an integer, service 1, and $a0 holds its value. To print a string, service 4, and $a0 holds its address.

Syscall 1 gets the number (loaded with lw or li); syscall 4, where the string starts (loaded with la); syscall 10 ends the program.

Question 6Single choice

Which byte is at the address of x?

.data
x: .word 0x11223344
  1. 0x44
  2. 0x11
  3. 0x22
  4. 0x00
Solution

Answer: 0x44

Memory holds 44 33 22 11, from the lowest address to the highest. The Data window shows the word written as 11223344, but the characters on the right come out in memory order.

Question 7Single choice

What does this program print?

.data
lives: .word 3
.text
.globl main
main: la   $a0, lives
      li   $v0, 1
      syscall
      li   $v0, 10
      syscall
  1. 268500992
  2. 3
  3. 0
  4. Nothing: it fails to load
Solution

Answer: 268500992

It’s the most typical bug of the topic: la instead of lw. Syscall 1 wants a value and we gave it an address.

Question 8Match

Match each directive with how much it takes.

.byte 7
.half 7
.word 7
.space 8
.ascii "ab"

Definitions:

  • 1 byte, at any address
  • 4 bytes, at a multiple of 4
  • 2 bytes, at an even address
  • 3 bytes, with its 0 byte
  • 8 zero bytes, at any address
  • 2 bytes, no 0 byte
Solution
  • .byte 7 → 1 byte, at any address
  • .half 7 → 2 bytes, at an even address
  • .word 7 → 4 bytes, at a multiple of 4
  • .space 8 → 8 zero bytes, at any address
  • .ascii "ab" → 2 bytes, no 0 byte

The spare definition belongs to .asciiz "ab": two letters and its 0 byte.

Question 9Single choice

What does syscall 4 print with $a0 at the address of greeting?

.data
greeting: .ascii  "Hola"
          .asciiz "!\n"
  1. Hola! (and a newline)
  2. Hola
  3. ! (and a newline)
  4. Nothing: the 0 byte is missing
Solution

Answer: Hola! (and a newline)

It’s the banner trick from the practice: several .ascii in a row and one .asciiz at the end are a single string.

Question 10Calculation

Work out how many bytes from a each datum lands.

.data
a: .byte   1
b: .word   2
c: .asciiz "ok"
d: .half   5
DatumDistance from a (bytes)
a
b
c
d
Solution
DatumDistance from a (bytes)
a0
b4
c8
d12

a takes +0; b is a .word and jumps to +4 (3 padding bytes); c takes +8, +9 and +10 (“ok” and its 0); the next free one is +11, odd, so d jumps to +12.

Question 11Short answer

Which word must you write with .word so that, in QtSpim, its four bytes read “HOLA”? (The ASCII codes are H = 48, O = 4F, L = 4C, A = 41, in hexadecimal.) Write it in hexadecimal.

Solution

Answer: 0x414C4F48

In little endian, the first byte in memory (the H, 48) is the least significant one: it goes on the right. Reading backwards, 41 4C 4F 48.

Question 12Single choice

What happens in QtSpim if you do lw on an address that isn’t a multiple of 4?

  1. An unaligned address exception is raised: QtSpim reports it and carries on, and the register doesn’t get the datum
  2. It reads the four bytes anyway
  3. The program doesn’t even load
  4. It rounds the address down to the previous multiple of 4
Solution

Answer: An unaligned address exception is raised: QtSpim reports it and carries on, and the register doesn’t get the datum

The console shows “Exception 4 [Address error in inst/data fetch] occurred and ignored” and the messages window shows the PC and the unaligned address.

Try it in QtSpim (or right here)

The playground comes with the template for every exercise: write your data in .data and your code in main. The hints tell you what each instruction and directive does as you type; if you’d rather do without them, turn them off.

Loading the MIPS playground… If it doesn’t appear, copy the template into your QtSpim.

  1. Do level 1 and, before running it, jot down on paper the address you expect for each datum.
  2. Assemble and, in the Data tab, compare your addresses with the ones in memory.
  3. Run and compare the output with the expected one.
  4. Do level 2 and compare the Data tab with the two lines of the statement, character by character.
  5. Paste the “What’s wrong here?” snippets and look in the messages tab for each one’s warning.
IC1 exam coming up?The theory and MIPS packs with new solved exams, their cheat sheets, blank exams and .s solutions, for free.See the pack

Cheat sheet: MIPS program structure and data

The essentials in 30 seconds

  • .data opens the data area (from 0x10010000) and .text the code area; main goes in .text and is declared with .globl main.
  • A label is an address, not a value.
  • la loads the address; lw reads the word at that address.
  • Alignment: SPIM aligns .half (even) and .word (multiple of 4) by itself; strings and bytes, it doesn’t.
  • Little endian: the first byte of a word in memory is the least significant one.
  • Syscall 4 prints up to the first 0 byte: .asciiz adds it; .ascii doesn’t.
  • The program ends with li $v0, 10 and syscall, always.

Data directives

DirectiveTakesAligns itselfExample
.byte1 byte per valueNolives: .byte 3
.half2 bytes per valueTo an even addressgold: .half 300
.word4 bytes per valueTo a multiple of 4exp: .word 12500
.ascii "…"1 byte per characterNo.ascii "Hola" (4 bytes)
.asciiz "…"1 byte per character + 1No.asciiz "Hola" (5 bytes)
.space nn zero bytesNobackpack: .space 8
.align nJumps to a multiple of 2ⁿ—.align 2 (multiple of 4)

This topic’s syscalls

I want to…$v0ArgumentLoaded with
Print an integer1$a0 = the valuelw, li, move
Print a string4$a0 = the addressla
End the program10——

Program template

        .data
datum:  .word 42                    # your data
msg:    .asciiz "Result: "
        .text
        .globl main
main:   li   $v0, 4                 # print a string: its address
        la   $a0, msg
        syscall
        li   $v0, 1                 # print an integer: its value
        lw   $a0, datum
        syscall
        li   $v0, 10                # end, always
        syscall

Working out a datum’s address

  1. The first datum is at 0x10010000.
  2. Each datum starts at the next free byte after the previous one.
  3. If it’s a .half and the free byte is odd, skip 1. If it’s a .word and it isn’t a multiple of 4, skip to the next multiple of 4.
  4. A string takes its characters, plus 1 if it’s .asciiz.
  5. Always check in the Data window: the address on the left is the one of the line’s first byte.

Most frequent mistakes

SymptomLikely cause
A huge number comes out (268500992…)You print an address: you used la where lw was needed
“Unaligned address in inst/data fetch” and a 0 comes outlw on a datum that isn’t a .word, or on an unaligned string
The string comes out with more text after itThe 0 byte is missing: you used .ascii instead of .asciiz
“Attempt to execute non-instruction” at the endSyscall 10 is missing
The word comes out “backwards”Little endian: the first byte in memory is the least significant one
The addresses don’t match the ones you worked outYou didn’t count the padding, or you loaded without Reinitialize
syntax error on a line that looks rightA label with a reserved name (such as b) or a mistake on the previous line

Before handing in, ask yourself

  1. Does each syscall get what it expects: a value for syscall 1 and an address for syscall 4?
  2. Do all the strings I print on their own end in a 0 byte?
  3. Do I read with lw only aligned words?
  4. Does my program end with li $v0, 10 and syscall?

If you want to review the topic from the ground up, go back to the MIPS program structure and data theory.

Keep goingAll MIPS articles

Did this article help you? Here is how you can support SergioLearns →

Ready for the exam?IC1 practical mock exam — MIPS in QtSpim with other data, subroutines and keyboard input (and why part 1 decides almost everything)