Arrays and strings in MIPS — practice in QtSpim: the high-score table, a sign that lights up in capitals and a mirror for messages (and the sign that went dark at the first space)
The arcade machine in my game needed a sign: when someone beats a high score, the notice arrives in lowercase (“new record from leo: 15600!”) and the machine has to show it in capitals. I had just learnt that there are 32 between 'a' and 'A', so it took me two minutes: walk the notice, subtract 32 from every character, write it into the sign and close it with sb $zero. My first version:
# all32.s: the sign subtracting 32 from everything (my first version)
.data
notice: .asciiz "new record from leo: 15600!"
sign: .space 32
tsign: .asciiz "Sign: "
.text
.globl main
main: la $t0, notice
la $t1, sign
loop: lbu $t2, 0($t0)
beqz $t2, end
addi $t2, $t2, -32 # to capitals... everything
sb $t2, 0($t1)
addi $t0, $t0, 1
addi $t1, $t1, 1
j loop
end: sb $zero, 0($t1)
li $v0, 4
la $a0, tsign
syscall
li $v0, 4
la $a0, sign
syscall
li $v0, 10
syscall
And the console:
Sign: NEW
Just “NEW”? The rest of the notice had vanished. No exception, no message. I opened the data tab and saw it: after NEW there was a byte set to 0. Of course: the space is 32, and 32 − 32 = 0. My program had turned the first space into a '\0', and syscall 4 prints up to the first 0. The digits did not escape either: the '1' (49) had become 17, which cannot even be printed. The lesson, which you will see in all three programs today: before you transform a character, check that it is one of the ones that has to be transformed.
This is the practice for the arrays and strings theory. There are three medium-level programs on the same arcade machine: the high-score table (two arrays of different sizes walked with one index), the marquee (turning into capitals only what needs it, counting digits and printing letter by letter with syscall 11) and the mirror (turning a message round without its spaces and looking for names that read the same backwards). The data looks like real data: unsorted scores, notices with spaces, a colon and an exclamation mark, three-letter names. Those “awkward” cases are what break programs that only work with the pretty example.
Table of Contents
Before you start
You should be clear on:
- How much each type takes (
.word4 bytes,.half2,.byte1) and what reads it (lw,lh,lb/lbu). - The address of an element by index: base + i · size, with
sll+add. - The string loop:
lbu+beqzand a +1 step, and closing withsb $zeroa string you build yourself. - Compound conditions with “and” and the opposite condition, from the branches, conditionals and loops theory.
If any of them sounds shaky, go over the theory before carrying on.
Write each program in Notepad++ (or in the web environment, at the end of the article), save it with the .s extension and load it into QtSpim with File → Reinitialize and Load File. Before running, look at the Data tab: seeing where each array and each string has been placed saves you half the mistakes in this topic.
Program 1: the high-score table
The arcade machine stores six scores as halfwords (.half) and the six three-letter names as strings packed together, 4 bytes each. The program has to print the table with the place, the name and the score, flag the scores over 10000, count them and add up all the points. What makes it tricky: each row needs one value from each array, and the two arrays have elements of different sizes.
The idea
If I walked both arrays with two pointers, I would have to remember to move one by +2 and the other by +4 on every lap (and not mix them up). With an index it is cleaner: a single i that goes from 0 to 5 and, on every lap, two conversions to bytes:
i · 4(sllby 2) + base ofnames→ address of the name, which goes straight into$a0for syscall 4.i · 2(sllby 1) + base ofscores→ address of the score, which is read withlh.- The place is
i + 1, and it comes for free: I already havei. - If the score is
>= 10000, I print the flag and count it. With the opposite condition:bltjumps over it.
Before programming, think about which registers keep what cannot change between laps (the two bases, n, the minimum, the counter and the sum). The syscall uses $a0 and $v0, so everything that has to survive goes in $s0–$s5.
The code
# ranking.s: the high-score table, with one index for two arrays
.data
scores: .half 9850, 15600, 7320, 12400, 11050, 8800 # 6 scores (2 bytes each)
n: .word 6
minimum: .word 10000 # from here on, highlighted score
names: .asciiz "SAM", "LEO", "ANA", "MAX", "ZOE", "KAI" # 4 bytes per name
dot: .asciiz ". "
spc: .asciiz " "
flag: .asciiz " <- over 10000"
nl: .asciiz "\n"
tcount: .asciiz "Scores over 10000: "
ttot: .asciiz "\nPoints from everyone: "
.text
.globl main
main: la $s2, scores # base of the scores
la $s3, names # base of the names
lw $s4, n # $s4 = 6
lw $s5, minimum # $s5 = 10000
li $s0, 0 # how many beat the minimum
li $s1, 0 # sum of all the points
li $t2, 0 # i = 0
loop: beq $t2, $s4, end
li $v0, 1 # place = i + 1
addi $a0, $t2, 1
syscall
li $v0, 4
la $a0, dot
syscall
sll $t5, $t2, 2 # i * 4: offset in names
add $a0, $s3, $t5 # $a0 = address of names[i]
li $v0, 4
syscall
li $v0, 4
la $a0, spc
syscall
sll $t4, $t2, 1 # i * 2: offset in scores
add $t4, $t4, $s2 # $t4 = address of scores[i]
lh $t3, 0($t4) # $t3 = scores[i]
add $s1, $s1, $t3 # sum = sum + scores[i]
li $v0, 1
move $a0, $t3
syscall
blt $t3, $s5, noflag # opposite of (scores[i] >= 10000)
addi $s0, $s0, 1 # count the highlighted score
li $v0, 4
la $a0, flag
syscall
noflag: li $v0, 4
la $a0, nl
syscall
addi $t2, $t2, 1 # with an index, only i moves on
j loop
end: li $v0, 4
la $a0, tcount
syscall
li $v0, 1
move $a0, $s0
syscall
li $v0, 4
la $a0, ttot
syscall
li $v0, 1
move $a0, $s1
syscall
li $v0, 10
syscall
The hard part is the loop body. First, the name: $a0 gets the computed address directly, because syscall 4 wants an address, not a value:
sll $t5, $t2, 2 # i * 4: offset in names add $a0, $s3, $t5 # $a0 = address of names[i] li $v0, 4 syscall
Then, the score: here you do have to read the halfword with lh, because syscall 1 prints the number in $a0, not what is stored at an address:
sll $t4, $t2, 1 # i * 2: offset in scores add $t4, $t4, $s2 # $t4 = address of scores[i] lh $t3, 0($t4) # $t3 = scores[i] add $s1, $s1, $t3 # sum = sum + scores[i] li $v0, 1 move $a0, $t3 syscall
And the flag, with the opposite condition from topic 3: if the score is below 10000, blt jumps to noflag and skips the flag and the counter:
blt $t3, $s5, noflag # opposite of (scores[i] >= 10000) addi $s0, $s0, 1 # count the highlighted score li $v0, 4 la $a0, flag syscall
Output
1. SAM 9850 2. LEO 15600 <- over 10000 3. ANA 7320 4. MAX 12400 <- over 10000 5. ZOE 11050 <- over 10000 6. KAI 8800 Scores over 10000: 3 Points from everyone: 65020
Checking by hand: the ones over 10000 are LEO (15600), MAX (12400) and ZOE (11050): 3. The sum: 9850 + 15600 + 7320 + 12400 + 11050 + 8800 = 65020. Both figures match. And each name goes with its score: row 2 is LEO with 15600, which is exactly what the hook in the theory could not manage.
The trace of the six laps, with the two addresses that are computed (the base of scores is 0x10010000 and the base of names, 0x10010014):
| i | sll 2 → name | Address of the name | sll 1 → score | Address of the score | lh | Flag? |
|---|---|---|---|---|---|---|
| 0 | 0 | 0x10010014 | 0 | 0x10010000 | 9850 | No |
| 1 | 4 | 0x10010018 | 2 | 0x10010002 | 15600 | Yes (1) |
| 2 | 8 | 0x1001001c | 4 | 0x10010004 | 7320 | No |
| 3 | 12 | 0x10010020 | 6 | 0x10010006 | 12400 | Yes (2) |
| 4 | 16 | 0x10010024 | 8 | 0x10010008 | 11050 | Yes (3) |
| 5 | 20 | 0x10010028 | 10 | 0x1001000a | 8800 | No |
Why do the names start at 0x10010014? Because before them come the 12 bytes of scores and two words, n and minimum (at 0x1001000c and 0x10010010). You can check it in the dump:
[10010000] 3cf0267a 30701c98 22602b2a 00000006 z & . < . . p 0 * + ` " . . . . [10010010] 00002710 004d4153 004f454c 00414e41 . ' . . S A M . L E O . A N A .
Read word by word. QtSpim shows each word in little endian: the byte at the lowest address is the one on the right.
| Address | Word | What is inside | Why |
|---|---|---|---|
| 0x10010000 | 3cf0267a | scores[0] = 0x267a = 9850 and scores[1] = 0x3cf0 = 15600 | Each .half takes 2 bytes: two fit in a word, and the one at +0 is the right half |
| 0x10010004 | 30701c98 | scores[2] = 0x1c98 = 7320 and scores[3] = 0x3070 = 12400 | Same again: +4 on the right, +6 on the left |
| 0x10010008 | 22602b2a | scores[4] = 0x2b2a = 11050 and scores[5] = 0x2260 = 8800 | This word finishes the 12 bytes of scores |
| 0x1001000c | 00000006 | n = 6 | A whole .word |
| 0x10010010 | 00002710 | minimum = 0x2710 = 10000 | Another .word |
| 0x10010014 | 004d4153 | “SAM” and its '\0' | 0x53 = ‘S’ on the right, then ‘A’ (0x41), ‘M’ (0x4d) and the 00 on the left |
| 0x10010018 | 004f454c | “LEO” and its '\0' | Each name fills exactly one word: that is why the step is 4 |
Questions you might ask
Why not use one pointer per array? It would work, but you would have three things to move on every lap (two pointers and the counter) and two different steps. With the index only i moves, and the size of each array appears just once, in its sll.
Why add $a0, $s3, $t5 and not lw $a0, …? Because syscall 4 does not want the name, it wants where it is. If you loaded it with lw, $a0 would hold the 4 bytes of "SAM\0" as a number (0x004d4153) and the syscall would try to print from that address, which has nothing to do with it.
Why lh and not lhu for the scores? Both would work here, because no score goes over 32767. But lh is the one that matches a signed number, and if one day there were a negative penalty, it would be read correctly.
Why does the flag go before the line break? Because the flag goes on the same line as the score. If you printed it after nl, it would appear at the start of the next row.
What if I set minimum with li $s5, 10000? That works too. Keeping it in .data has one advantage: you change the minimum without touching the code, which is what many statements ask for (“for any array”).
Using the same sll for both arrays (for example, copying the score lines for the names). With sll ..., 1 on the names, row 1 would point at names + 2, the M of SAM, and you would get “M” instead of LEO.
What if a player were called ANNA?
With “ANNA” instead of “ANA”, and nothing else changed:
1. SAM 9850 2. LEO 15600 <- over 10000 3. ANNA 7320 4. 12400 <- over 10000 5. 11050 <- over 10000 6. 8800 Scores over 10000: 3 Points from everyone: 65020
The last three rows lose their name. “ANNA” takes 5 bytes (four letters and the '\0'), so everything after it shifts by one: MAX starts at +13, ZOE at +17 and KAI at +21. But the sll still computes i · 4:
| i | i · 4 | What is in that byte | What syscall 4 prints |
|---|---|---|---|
| 3 | +12 | The '\0' of ANNA | An empty string |
| 4 | +16 | The '\0' of MAX | An empty string |
| 5 | +20 | The '\0' of ZOE | An empty string |
The scores, the counter and the sum come out right, because their array has not moved. The program gives no warning: i · 4 only works if every name takes exactly 4 bytes. For names of different lengths, either you pad them to the same size or you move to the next one by looking for its '\0', as in the mirror.
Try it yourself
- Add a final line with the place of the highest score, keeping the
iof the maximum in another register (as inrecord.sin the theory). - Change the minimum to 9000 in
.dataand check that the flag and the counter change without touching the code.
Program 2: the marquee
The notice arrives in lowercase, with spaces, a colon, digits and an exclamation mark. The program has to build the sign in another string (lowercase letters as capitals; everything else as it is), count the letters changed and the digits, and then “light up” the sign by printing each character followed by a space. What makes it tricky: three kinds of character with three different treatments, and a character must not go through two of them.
The idea
It is the string loop with two pointers (reading in notice, writing in sign) and an if-else-if inside:
- If the character is between
'a'and'z': subtract 32, add a letter changed and jump straight to writing. - If not, ask whether it is between
'0'and'9': if it is, add a digit (and leave it unchanged). - Write the character (changed or not) with
sband move both pointers on.
At the end, sb $zero closes the sign. And to light it up, another string loop over sign that uses syscall 11 (print_char): character in $a0, li $v0, 11, syscall, and again with a space.
Before programming, think about what happens to a lowercase letter after subtracting 32. If it carried on to the digit question, a 'P' (80) would not be a digit, but imagine another calculation where the result did land in a range it should not: the j write makes each character go through only one branch.
The code
# marquee.s: the arcade machine's marquee in capitals, letter by letter
.data
notice: .asciiz "new record from leo: 15600!" # what arrives
sign: .space 32 # what lights up
tsign: .asciiz "Sign: "
tupp: .asciiz "\nLetters changed: "
tdig: .asciiz "\nDigits: "
tlight: .asciiz "\nIn lights: "
.text
.globl main
main: la $t0, notice # read pointer
la $t1, sign # write pointer
li $s0, 0 # letters changed
li $s1, 0 # digits
loop: lbu $t2, 0($t0) # current character
beqz $t2, end # '\0': end of the notice
blt $t2, 'a', notlow # opposite of (c >= 'a')
bgt $t2, 'z', notlow # opposite of (c <= 'z')
addi $t2, $t2, -32 # lowercase -> uppercase
addi $s0, $s0, 1
j write # a lowercase letter is not a digit: skip that question
notlow: blt $t2, '0', write # opposite of (c >= '0')
bgt $t2, '9', write # opposite of (c <= '9')
addi $s1, $s1, 1 # it is a digit: count it and leave it as it is
write: sb $t2, 0($t1) # write the character (changed or not)
addi $t0, $t0, 1
addi $t1, $t1, 1
j loop
end: sb $zero, 0($t1) # close the sign
li $v0, 4
la $a0, tsign
syscall
li $v0, 4
la $a0, sign
syscall
li $v0, 4
la $a0, tupp
syscall
li $v0, 1
move $a0, $s0
syscall
li $v0, 4
la $a0, tdig
syscall
li $v0, 1
move $a0, $s1
syscall
li $v0, 4
la $a0, tlight
syscall
# --- the sign in lights: each character followed by a space (syscall 11) ---
la $t1, sign
lights: lbu $a0, 0($t1) # the character goes straight into $a0
beqz $a0, quit
li $v0, 11 # print_char: prints the character in $a0
syscall
li $a0, ' '
li $v0, 11
syscall
addi $t1, $t1, 1
j lights
quit: li $v0, 10
syscall
The heart of the program is these lines. The first two comparisons form an “and” (lowercase); if either fails, they jump to notlow, where the second question starts:
blt $t2, 'a', notlow # opposite of (c >= 'a')
bgt $t2, 'z', notlow # opposite of (c <= 'z')
addi $t2, $t2, -32 # lowercase -> uppercase
addi $s0, $s0, 1
j write # a lowercase letter is not a digit: skip that question
notlow: blt $t2, '0', write # opposite of (c >= '0')
bgt $t2, '9', write # opposite of (c <= '9')
addi $s1, $s1, 1 # it is a digit: count it and leave it as it is
write: sb $t2, 0($t1) # write the character (changed or not)
And the lights loop. Notice that the character is read straight into $a0, which is where syscall 11 wants it, and that the space is set with li $a0, ' ':
la $t1, sign
lights: lbu $a0, 0($t1) # the character goes straight into $a0
beqz $a0, quit
li $v0, 11 # print_char: prints the character in $a0
syscall
li $a0, ' '
li $v0, 11
syscall
addi $t1, $t1, 1
j lights
Output
Sign: NEW RECORD FROM LEO: 15600! Letters changed: 16 Digits: 5 In lights: N E W R E C O R D F R O M L E O : 1 5 6 0 0 !
Checking by hand. Letters in the notice: “new” (3) + “record” (6) + “from” (4) + “leo” (3) = 16 lowercase letters, all changed. Digits: 1, 5, 6, 0, 0 = 5. And the rest (the four spaces, the colon and the exclamation mark) was copied as it was: the spaces are still in place on the sign. 16 + 5 + 4 + 1 + 1 = 27, which is the length of the notice.
Some laps, character by character:
| Lap | Character (code) | 'a'…'z'? | '0'…'9'? | Written | Letters | Digits |
|---|---|---|---|---|---|---|
| 1 | n (110) | Yes | — | N (78) | 1 | 0 |
| 2 | e (101) | Yes | — | E (69) | 2 | 0 |
| 4 | space (32) | No (blt) | No (blt) | space (32) | 3 | 0 |
| 20 | : (58) | No (blt) | No (bgt) | : (58) | 16 | 0 |
| 22 | 1 (49) | No (blt) | Yes | 1 (49) | 16 | 1 |
| 27 | ! (33) | No (blt) | No (blt) | ! (33) | 16 | 5 |
The space, which my first version turned into a '\0', now fails the first question (32 < 97) and the digit question (32 < 48), so it is written as it is.
Questions you might ask
Why the j write after counting the letter? So that a lowercase letter does not carry on to the digit question. Here it would not give a different result (a capital is never between '0' and '9'), but it is the right thing in an if-else-if: each character goes into one branch and only one. In the exam, if your sign counts correctly “by chance”, it will not be marked as right.
Why lbu and not lb? Because they are characters. With the characters in this notice it makes no difference, but with lbu you never get a negative character slipping through a blt.
Can I compare with 97 instead of 'a'? Yes, it is the same to SPIM. With 'a' it reads better and you do not have to look up the ASCII table. In the exam they give you the table, but writing 'a' saves you mistakes when copying numbers.
Why do I read straight into $a0 in the lights loop? Because the character has to end up in $a0 for syscall 11, and the same load serves the beqz that stops the loop. One instruction and one register fewer.
What if the sign is too short? .space 32 reserves 32 bytes and the notice takes 28 (27 characters + the '\0'). If the notice were longer than the room, the copy would write over whatever is behind it in memory (here, the string tsign), without any warning. Always reserve with room to spare.
What if the sign were reserved with .space 16?
The program does not crash, but the console looks like this:
LEO: 15600!NEW RECORD FROM LEO: 15600!600!16 Digits: 5 In lights: N E W R E C O R D F R O M L E O : 1 5 6 0 0 !
“Sign: ” and “Letters changed: ” are gone, “LEO: 15600!” appears twice and a stray “600!” shows up. The addresses explain all of it:
| What | Where | Why |
|---|---|---|
sign | 0x1001001c to 0x1001002b | With .space 16 it now has only 16 bytes |
tsign (“Sign: “) | 0x1001002c to 0x10010032 | Right behind it: 6 characters and its '\0' |
tupp (“\nLetters changed: “) | From 0x10010033 | Right behind tsign |
| The copy of the notice | 0x1001001c to 0x10010037 | It writes 28 bytes (27 characters and the '\0'), without checking where the room ends |
| Characters 16 to 26 (“LEO: 15600!”) | 0x1001002c to 0x10010036 | They land on top of tsign and of the first four bytes of tupp |
The final '\0' | 0x10010037 | It overwrites the second t of “\nLetters changed: “ |
Now the first syscall 4, the one for tsign, prints “LEO: 15600!” up to the '\0' at 0x10010037. The one for sign prints the whole sign, because it does not find a 0 before that point either. And the one for tupp starts at 0x10010033, which now holds the 6, 0, 0 and ! of the score: it prints “600!” and stops, with no line break, which is why the 16 is stuck to it.
Subtracting 32 without checking that it is a lowercase letter, which is exactly my hook: the space becomes a 0 and the sign is cut at “NEW”.
Try it yourself
- Make the sign also change spaces into underscores (
'_'), with a third branch, and count them. - Make the lights print only the letters (no digits or symbols), separated by a hyphen.
Program 3: the mirror
The arcade machine has a “mirror” mode: it shows the message INSERT COIN backwards and without spaces, and it looks in the table for the names that read the same forwards and backwards (palindromes). What makes it tricky: in the first part, writing does not always move on (when a space is skipped, only reading moves back); in the second, letters have to be compared from both ends.
The idea
Part 1, backwards and without spaces. It is the reverse template from the theory with one change: if the character read is a space, I count it and do not write it. That is why reading and writing move on in different places of the loop:
- Find the
'\0'and step back one:$t1on the last letter. - While
$t1 >= $t0: read; if it is not a space,sband writing +1; reading −1 always. - Close with
sb $zero.
Part 2, palindromes. For each name, two pointers: $t0 on the first letter and $t1 on the last. Compare; if they differ, it is not a palindrome; if they match, $t0 +1 and $t1 −1. If they cross (or meet) without a different pair, it is a palindrome. And to move to the next name, +4 (each name takes 4 bytes).
Before programming, think about the stop in part 2. With “ANA”, after comparing A with A, both pointers are on the N: $t0 >= $t1. With a four-letter name (“OTTO”) they would cross. The condition bge $t0, $t1 covers both cases.
The code
# mirror.s: the message backwards without spaces and the palindrome names
.data
msg: .asciiz "INSERT COIN"
names: .asciiz "SAM", "LEO", "ANA", "MAX", "ZOE", "KAI" # 4 bytes per name
n: .word 6
reversed: .space 16
trev: .asciiz "Backwards without spaces: "
tspc: .asciiz "\nSpaces removed: "
tpal: .asciiz "\nPalindromes: "
spc: .asciiz " "
.text
.globl main
main:
# --- part 1: backwards and without spaces ---
la $t0, msg # start
move $t1, $t0
find: lbu $t2, 0($t1) # look for the '\0'
beqz $t2, last
addi $t1, $t1, 1
j find
last: addi $t1, $t1, -1 # $t1 = last character
la $t3, reversed # write pointer
li $s0, 0 # spaces removed
copy: blt $t1, $t0, close # I have gone past the start
lbu $t2, 0($t1)
bne $t2, ' ', write # opposite of (c == ' ')
addi $s0, $s0, 1 # a space: count it and do not write it
j advance
write: sb $t2, 0($t3)
addi $t3, $t3, 1 # writing only moves on if I write
advance: addi $t1, $t1, -1 # reading always moves back
j copy
close: sb $zero, 0($t3)
li $v0, 4
la $a0, trev
syscall
li $v0, 4
la $a0, reversed
syscall
li $v0, 4
la $a0, tspc
syscall
li $v0, 1
move $a0, $s0
syscall
li $v0, 4
la $a0, tpal
syscall
# --- part 2: which names read the same forwards and backwards ---
la $s2, names # $s2 = current name
lw $s3, n
li $s4, 0 # i = 0
name: beq $s4, $s3, quit
move $t0, $s2 # left: first letter
move $t1, $s2
end_n: lbu $t2, 0($t1) # right: look for the '\0'...
beqz $t2, right
addi $t1, $t1, 1
j end_n
right: addi $t1, $t1, -1 # ... and stay on the last letter
compare: bge $t0, $t1, palin # they have crossed: every pair matched
lbu $t2, 0($t0)
lbu $t3, 0($t1)
bne $t2, $t3, nopal # one pair differs: not a palindrome
addi $t0, $t0, 1 # left moves forwards
addi $t1, $t1, -1 # right moves backwards
j compare
palin: li $v0, 4
move $a0, $s2
syscall
li $v0, 4
la $a0, spc
syscall
nopal: addi $s2, $s2, 4 # next name (4 bytes)
addi $s4, $s4, 1
j name
quit: li $v0, 10
syscall
Part 1, zoomed in. The space jumps to advance, which only moves reading back; any other character goes through write, which stores it and moves writing on:
copy: blt $t1, $t0, close # I have gone past the start
lbu $t2, 0($t1)
bne $t2, ' ', write # opposite of (c == ' ')
addi $s0, $s0, 1 # a space: count it and do not write it
j advance
write: sb $t2, 0($t3)
addi $t3, $t3, 1 # writing only moves on if I write
advance: addi $t1, $t1, -1 # reading always moves back
j copy
Part 2, the comparison from both ends:
compare: bge $t0, $t1, palin # they have crossed: every pair matched
lbu $t2, 0($t0)
lbu $t3, 0($t1)
bne $t2, $t3, nopal # one pair differs: not a palindrome
addi $t0, $t0, 1 # left moves forwards
addi $t1, $t1, -1 # right moves backwards
j compare
Output
Backwards without spaces: NIOCTRESNI Spaces removed: 1 Palindromes: ANA
Checking by hand: INSERT COIN backwards is NIOC TRESNI, and without the space, NIOCTRESNI (10 of the 11 characters). Spaces removed: 1. Palindromes: out of SAM, LEO, ANA, MAX, ZOE and KAI, only ANA (A = A); in the others, the first and last letters already differ.
The trace of part 1, lap by lap. msg starts at 0x10010000; I write it as msg + offset, and the writing pointer as reversed + offset:
| Lap | $t1 (reading) | Character | Space? | Written to | Spaces | Why |
|---|---|---|---|---|---|---|
| 1 | msg+10 | N | No | reversed+0 | 0 | It starts on the last letter thanks to the addi $t1, $t1, -1 at last |
| 2 | msg+9 | I | No | reversed+1 | 0 | Reading moves back and writing moves forward |
| 3 | msg+8 | O | No | reversed+2 | 0 | Same again |
| 4 | msg+7 | C | No | reversed+3 | 0 | Same again |
| 5 | msg+6 | space | Yes | (nothing) | 1 | bne does not jump: it counts and goes to advance. $t3 stays at reversed+4 |
| 6 | msg+5 | T | No | reversed+4 | 1 | The T takes the slot the space did not use |
| 7 | msg+4 | R | No | reversed+5 | 1 | Same again |
| 8 | msg+3 | E | No | reversed+6 | 1 | Same again |
| 9 | msg+2 | S | No | reversed+7 | 1 | Same again |
| 10 | msg+1 | N | No | reversed+8 | 1 | Same again |
| 11 | msg+0 | I | No | reversed+9 | 1 | $t1 = $t0: blt does not jump, because this letter is still left |
| 12 | msg−1 | — | — | '\0' at reversed+10 | 1 | Now $t1 is less than $t0: blt jumps to close |
Eleven characters read and ten written (plus the '\0' from close): the missing one is the space. If the stop were ble instead of blt, the loop would stop at lap 11 without reading the I at the start, and you would get NIOCTRESN.
Forgetting the addi $t1, $t1, -1 at last. The first read is then the '\0' at msg+11; it is not a space, so it is written to reversed+0, and the rest of the string goes after it. The console says “Backwards without spaces: ” and nothing else: NIOCTRESNI is in memory, but syscall 4 stops at the first byte, which is the 0.
The trace of part 2 with ANA and with KAI (the names start at 0x1001000c, after the 12 bytes of INSERT COIN, so ANA, the third one, is at 0x10010014):
| Name | Lap | $t0 → letter | $t1 → letter | Equal? | Next step |
|---|---|---|---|---|---|
| ANA | 1 | +0 → A | +2 → A | Yes | $t0 +1, $t1 −1 |
| ANA | 2 | +1 → N | +1 → N | — | bge: palindrome, printed |
| KAI | 1 | +0 → K | +2 → I | No | bne to nopal |
Questions you might ask
Why does writing move on inside write and not at the end of the loop? Because if it always moved, each space would leave a gap in reversed: a byte set to 0 that would cut the string at that point (.space reserves zeroed bytes). Writing only moves when it really writes.
Why do I compare addresses with bge $t0, $t1? Because both registers are pointers. When the left address reaches or passes the right one, there are no pairs left to compare. They are numbers, and comparing addresses is as valid as comparing scores.
Why look for the '\0' of each name if I know they all have 3 letters? Because that way the program works for names of any length that end in '\0'. The only thing it assumes is that each name takes 4 bytes (because of the addi $s2, $s2, 4); if the names had different lengths, you would move to the next one by looking for its '\0'.
Could I compare reversed with the original to know whether it is a palindrome? Yes, by reversing each name into an auxiliary string and comparing them letter by letter. It works, but it does twice the work: with the two pointers you do not need to copy anything.
Why do I print a space after each palindrome? So that, if there were several, they come out separated. That is why the output has a space after ANA.
Moving writing +1 for spaces too. The reversed string would have a 0 where the space was and syscall 4 would only print NIOC.
Try it yourself
- Change the message to
"A MAN A PLAN A CANAL PANAMA"and check that, without spaces and backwards, it comes out the same as the original without spaces (it is a sentence palindrome). - Count how many names are palindromes and store it in a word
ncapin.data.
Common mistakes in this practice
- Transforming everything without checking. Subtracting 32 from a space turns it into
'\0'and the string is cut (my “NEW” sign). - The same offset for two arrays of different sizes. Each array converts the index with its own
sll:i · 2for.half,i · 4for 4-byte names. lwwhere the syscall wants an address. Syscall 4 needs the address of the string in$a0(laoradd), not its contents.- Not closing the string you build. Without
sb $zeroat the end, it only works if the place was already zeroed. - Moving writing on when you do not write. When filtering characters (removing spaces), writing only moves after an
sb. - Falling into two branches. In an if-else-if, each branch ends with a
jto the common point; otherwise a character can go through two treatments.
Try it in QtSpim (or right here)
The marquee is the best program for watching a string being built. Open it in QtSpim or in the environment below:
Loading the MIPS environment… If it does not appear, copy marquee.s into your QtSpim.
- Look at the Data tab before running: the notice starts at
0x10010000andsignright after it, at0x1001001c, all zeros. - Put a breakpoint on
sb $t2, 0($t1)and press F5 a few times: at each stop, the byte ofsignabout to be written is the next one in the row, and the zeros fill up with capitals. - On the lap of the first space, watch
$t2: it is 32 before and after, because no branch touches it. - Change
addi $t2, $t2, -32toandi $t2, $t2, 0xDF(the trick from topic 2) and check that the sign comes out the same. - Remove the
sb $zero, 0($t1)and run it: here it still comes out fine, becausesignstarted as zeros. Now change the notice to a shorter one without touching the.space: it still comes out fine. The bug only shows up if the place already had something written in it, and that is why it is so treacherous.
Summary
- With an index, a single
iserves several arrays: each one turns it into bytes with its ownsll(by 2 for halfwords, by 4 for 4-byte names). - Syscall 4 wants an address in
$a0; syscall 1, a number; syscall 11, a character. - Before transforming a character, check its range (
'a'…'z','0'…'9') with the opposite condition; whatever does not match is copied as it is. - In an if-else-if, each branch ends with a
jto the common point, so no character goes through two branches. - When filtering, writing only moves on when you write; reading, always.
- A string you build is closed with
sb $zero, and its.spaceis reserved with room to spare.
Checklist: have you practised it?
- Have you run the three programs and checked the outputs?
- Could you work out by hand the address of
names[3]and ofscores[3]in the high-score table? - Can you explain why my first version of the sign was cut at “NEW”?
- Do you know which syscall prints a character and where that character goes?
- Why does writing not move on every lap in the mirror?
- Have you tried the “Try it yourself” tasks without looking at the theory?
If you answered yes to almost everything, you are ready for the arrays and strings exercises, where I no longer hand you the code. See you there!
Next step: exercisesArrays and strings in MIPS — exercises with extra lives, an encrypted save code and a high score that sneaks into the table (and the code that had a curly brace)Did this article help you? Here is how you can support SergioLearns →



