IC1 practical mock exam — MIPS in QtSpim with other data, subroutines and keyboard input (and why part 1 decides almost everything)
The IC1 practical exam is 90 minutes in the lab with Notepad++ and QtSpim: a MIPS assembly program that has to work for any set of data, not only for the data in the statement. Here you have a mock exam with the same format: two sittings (ordinary and resit), three versions of each, the website’s MIPS environment inside the exam and a marking that changes the values in your .data, types the input and checks memory, console and code, just as a lecturer would with another array.
Everything that comes up is explained in the six MIPS topics, from program structure and data to subroutines and the stack. If you have not installed the simulator yet, start with the QtSpim guide.
Table of Contents
—
Take the mock exam
Choose the sitting and the version (or let one come out at random) and press «Start the mock exam». The clock counts the 90 minutes and the exam opens in a full window: the statement on the left and the MIPS environment on the right, with one tab per part.
The mock exam needs JavaScript. Below you will find what the exam is like and what each exercise covers.
The potion inventory
In a role-playing game, the inventory keeps the power of 12 potions in an array of words Potions. A potion with a positive value heals and one with a negative value poisons; a value outside the range [−50, 100] is a corrupted potion and is not used.
Part 1 (compulsory, 8 marks)
Write a MIPS R2000 assembly program that goes through Potions and:
- a. Computes the total life: the sum of the valid potions (between −50 and 100, both included).
- b. Counts the valid negative potions (poisons), the valid ones greater than or equal to 50 (big heals) and the corrupted ones.
- c. Stores in
Result(4 words), in this order: the total life, the poisons, the big heals and the corrupted ones.
Guidelines
- In memory:
Potions: .word 35, -20, 120, 60, -75, 0, 15, -5, 90, 200, -50, 45,N: .word 12andResult: .space 16(already in the file). Keep those label names: the tests look for them. - The program must work for any 12-element array: the data change when it is marked.
- End with system call 10.
Algorithm
Life = 0; Poisons = 0; Big = 0; Corrupted = 0
for i from 0 to N-1 do
if (Potions[i] < -50) or (Potions[i] > 100) then
Corrupted = Corrupted + 1
else
Life = Life + Potions[i]
if (Potions[i] < 0) then Poisons = Poisons + 1 endif
if (Potions[i] >= 50) then Big = Big + 1 endif
endif
endfor
Result = Life, Poisons, Big, CorruptedWith the statement data, Result ends up as 170, 3, 2, 3.
Solution
a-o1.s
# Ordinary A · Part 1: the potion inventory .data Potions: .word 35, -20, 120, 60, -75, 0, 15, -5, 90, 200, -50, 45 N: .word 12 Result: .space 16 # Life, Poisons, Big, Corrupted (4 words) .text .globl main main: la $t0, Potions # pointer to the current element lw $t1, N # how many elements there are li $t2, 0 # i = 0 li $s0, 0 # Life = 0 li $s1, 0 # Poisons = 0 li $s2, 0 # Big = 0 li $s3, 0 # Corrupted = 0 loop: beq $t2, $t1, end # i == N: the array is over lw $t3, 0($t0) # x = Potions[i] slti $t4, $t3, -50 # x < -50? bne $t4, $zero, corrupt # yes: out of the valid range li $t5, 100 slt $t4, $t5, $t3 # 100 < x? bne $t4, $zero, corrupt add $s0, $s0, $t3 # valid: it goes into the total life bgez $t3, not_poison # x >= 0: not a poison addi $s1, $s1, 1 # Poisons++ not_poison: slti $t4, $t3, 50 # x < 50? bne $t4, $zero, next # yes: not a big heal addi $s2, $s2, 1 # Big++ j next corrupt: addi $s3, $s3, 1 # Corrupted++ next: addi $t0, $t0, 4 # next word addi $t2, $t2, 1 # i++ j loop end: la $t6, Result sw $s0, 0($t6) # Result[0] = Life sw $s1, 4($t6) # Result[1] = Poisons sw $s2, 8($t6) # Result[2] = Big sw $s3, 12($t6) # Result[3] = Corrupted li $v0, 10 syscall
With the statement data, Result ends up in memory as 170, 3, 2, 3 (Data tab of the simulator).
A range with two ends is checked with two branches: slti for «< −50» and slt with the operands swapped for «> 100» (MIPS has no «greater than»). A valid potion always adds up and then the two conditions are checked separately: a 0 is neither a poison nor a big heal, and −50 and 100 are valid (the edge-case tests check it).
Keyboard and screen
Part 2 (optional, 2 marks)
Change the program so that the user types the 12 values of Potions (with a message for each one) and the program prints the four results, each on its own line with a message in front, in the order of Result (which is also stored in memory).
It only counts if you have at least 4 marks in part 1. You can start with the «Bring my code from part 1» button.
Example (the last four console lines)
Total life: 170 Poisons: 3 Big heals: 2 Corrupted: 3
Solution
a-o2.s
# Ordinary A · Part 2: the potions from the keyboard and the result on screen .data Potions: .word 35, -20, 120, 60, -75, 0, 15, -5, 90, 200, -50, 45 N: .word 12 Result: .space 16 # Life, Poisons, Big, Corrupted msg_ask: .asciiz "Potion " msg_colon: .asciiz ": " msg_life: .asciiz "Total life: " msg_poi: .asciiz "Poisons: " msg_big: .asciiz "Big heals: " msg_bad: .asciiz "Corrupted: " newline: .asciiz "\n" .text .globl main main: # 1) Read the N values and store them in Potions la $s4, Potions lw $s5, N li $s6, 0 # i = 0 read: beq $s6, $s5, compute la $a0, msg_ask # "Potion " li $v0, 4 syscall addi $a0, $s6, 1 # the number the user sees starts at 1 li $v0, 1 syscall la $a0, msg_colon # ": " li $v0, 4 syscall li $v0, 5 # read_int syscall sw $v0, 0($s4) # stored at once: $v0 is reused addi $s4, $s4, 4 addi $s6, $s6, 1 j read compute: # 2) The same loop as in part 1 la $t0, Potions lw $t1, N li $t2, 0 li $s0, 0 li $s1, 0 li $s2, 0 li $s3, 0 loop: beq $t2, $t1, end lw $t3, 0($t0) slti $t4, $t3, -50 bne $t4, $zero, corrupt li $t5, 100 slt $t4, $t5, $t3 bne $t4, $zero, corrupt add $s0, $s0, $t3 bgez $t3, not_poison addi $s1, $s1, 1 not_poison: slti $t4, $t3, 50 bne $t4, $zero, next addi $s2, $s2, 1 j next corrupt: addi $s3, $s3, 1 next: addi $t0, $t0, 4 addi $t2, $t2, 1 j loop end: la $t6, Result sw $s0, 0($t6) sw $s1, 4($t6) sw $s2, 8($t6) sw $s3, 12($t6) # 3) Print the four results, each on its own line la $a0, msg_life li $v0, 4 syscall move $a0, $s0 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall la $a0, msg_poi li $v0, 4 syscall move $a0, $s1 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall la $a0, msg_big li $v0, 4 syscall move $a0, $s2 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall la $a0, msg_bad li $v0, 4 syscall move $a0, $s3 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall li $v0, 10 syscall
Potion 1: 35 Potion 2: -20 Potion 3: 120 Potion 4: 60 Potion 5: -75 Potion 6: 0 Potion 7: 15 Potion 8: -5 Potion 9: 90 Potion 10: 200 Potion 11: -50 Potion 12: 45 Total life: 170 Poisons: 3 Big heals: 2 Corrupted: 3
Each read_int leaves the number in $v0: it is stored with sw in Potions before the next syscall. The counters live in $s registers and are printed with print_int (service 1) after their message (service 4).
Healing the allies
The group’s healer walks along the row of allies. The array of words V keeps each one’s life and N how many there are; a negative value means that from there on the allies are out of reach.
Part 1 (compulsory, 6 marks)
Write a MIPS assembly program that goes through V while there are elements left and the value is not negative. In even positions (the first one is 0), if the life is between 10 and 50 (both included), it adds 25 (replaced in the array) and counts it in Healed. Every element visited is counted in Visited. The process ends after the N elements or at the first negative, whichever comes first.
Guidelines
- In memory:
V: .word 40, 12, 33, 55, 18, 7, 26, 49, -1, 30,N: .word 10,Healed: .word 0andVisited: .word 0. Keep those names. - Condition to pass: the program must work correctly for any set of data (any N and any array).
Algorithm
Healed = 0; Visited = 0; i = 0
while (i <= N-1) and (V[i] >= 0) do
if (i is even) then
if (V[i] >= 10) and (V[i] <= 50) then
V[i] = V[i] + 25
Healed = Healed + 1
endif
endif
Visited = Visited + 1
i = i + 1
endwhileWith the statement data: V = 65, 12, 58, 55, 43, 7, 51, 49, −1, 30; Healed = 4; Visited = 8.
Solution
a-e1.s
# Resit A · Part 1: healing the allies .data V: .word 40, 12, 33, 55, 18, 7, 26, 49, -1, 30 N: .word 10 Healed: .word 0 Visited: .word 0 .text .globl main main: la $t0, V # pointer to V[i] lw $t1, N li $t2, 0 # i = 0 li $s0, 0 # Healed li $s1, 0 # Visited while: beq $t2, $t1, end # (i <= N-1) fails: end lw $t3, 0($t0) bltz $t3, end # (V[i] >= 0) fails: end andi $t4, $t2, 1 # bit 0 of i: 1 if i is odd bne $t4, $zero, next # odd: no healing slti $t4, $t3, 10 # V[i] < 10? bne $t4, $zero, next slti $t4, $t3, 51 # V[i] <= 50? (V[i] < 51) beq $t4, $zero, next addi $t3, $t3, 25 # heal: +25 sw $t3, 0($t0) # stored in the same place addi $s0, $s0, 1 # Healed++ next: addi $s1, $s1, 1 # Visited++ addi $t0, $t0, 4 addi $t2, $t2, 1 # i++ j while end: sw $s0, Healed sw $s1, Visited li $v0, 10 syscall
With the statement data: V = 65, 12, 58, 55, 43, 7, 51, 49, -1, 30; Healed = 4; Visited = 8.
An «and» loop condition becomes two exits: beq $t2, $t1, end (N is over) and bltz $t3, end (negative). Even/odd with andi $t4, $t2, 1. The 10-50 range with two slti (< 10 leaves and < 51 must hold). The healed life is written with sw to the same address it was read from.
Subroutine heal
Part 2 (optional, 2 marks)
Change the program so that the life check and the healing are done in a subroutine called heal, which receives the address of V in $a0 and the position i in $a1, heals V[i] if it must and returns in $v0 a 1 if it healed and a 0 if not. main calls it with jal in the even positions and adds $v0 to Healed.
while (i <= N-1) and (V[i] >= 0) do if (i is even) then Healed = Healed + heal(V, i) endif Visited = Visited + 1; i = i + 1 endwhile heal(V, i): if (V[i] >= 10) and (V[i] <= 50) then V[i] = V[i] + 25; return 1 endif return 0
It only counts if you have at least 3 marks in part 1.
Solution
a-e2.s
# Resit A · Part 2: the healing as a subroutine heal(V, i) .data V: .word 40, 12, 33, 55, 18, 7, 26, 49, -1, 30 N: .word 10 Healed: .word 0 Visited: .word 0 .text .globl main main: la $s0, V # base address of V (passed in $a0) lw $s1, N li $s2, 0 # i li $s3, 0 # Healed li $s4, 0 # Visited while: beq $s2, $s1, end sll $t0, $s2, 2 # i * 4 add $t0, $s0, $t0 # &V[i] lw $t1, 0($t0) bltz $t1, end andi $t2, $s2, 1 bne $t2, $zero, next # only even positions call heal move $a0, $s0 # argument 1: V move $a1, $s2 # argument 2: i jal heal add $s3, $s3, $v0 # $v0 = 1 if it healed, 0 if not next: addi $s4, $s4, 1 addi $s2, $s2, 1 j while end: sw $s3, Healed sw $s4, Visited li $v0, 10 syscall # heal(V in $a0, i in $a1): if 10 <= V[i] <= 50, V[i] = V[i] + 25 and it returns 1 in $v0; otherwise 0. # It is a leaf (it calls nobody) and only uses $t registers: it does not need the stack. heal: li $v0, 0 sll $t0, $a1, 2 add $t0, $a0, $t0 # &V[i] lw $t1, 0($t0) slti $t2, $t1, 10 bne $t2, $zero, heal_end slti $t2, $t1, 51 beq $t2, $zero, heal_end addi $t1, $t1, 25 sw $t1, 0($t0) li $v0, 1 heal_end: jr $ra
main keeps what must survive the call (V, N, i and the counters) in $s registers; heal is a leaf that only uses $t, so it does not need the stack and returns with jr $ra. It computes &V[i] with sll + add, as in the arrays topic.
Keyboard and screen
Part 3 (optional, 2 marks)
Change the program (the one from part 1: you do not need part 2) so that the user first types N (1 to 20) and then the N elements of V (declare V: .space 80). At the end, print the whole array (the N elements) on one line, separated by spaces, and then Healed and Visited, each on its own line with a message in front.
It only counts if you have at least 3 marks in part 1.
Example (the last three console lines)
Final array: 65 12 58 55 43 7 51 49 -1 30 Healed: 4 Visited: 8
Solution
a-e3.s
# Resit A · Part 3: the array from the keyboard and its final state on screen .data V: .space 80 # up to 20 words N: .word 0 Healed: .word 0 Visited: .word 0 msg_n: .asciiz "How many allies (1-20)? " msg_ask: .asciiz "Life of ally " msg_colon: .asciiz ": " msg_vec: .asciiz "Final array: " msg_heal: .asciiz "Healed: " msg_vis: .asciiz "Visited: " space: .asciiz " " newline: .asciiz "\n" .text .globl main main: la $a0, msg_n li $v0, 4 syscall li $v0, 5 syscall sw $v0, N # N is stored in memory before the next syscall move $s5, $v0 la $s4, V li $s6, 0 read: beq $s6, $s5, compute la $a0, msg_ask li $v0, 4 syscall move $a0, $s6 # positions start at 0, as in the statement li $v0, 1 syscall la $a0, msg_colon li $v0, 4 syscall li $v0, 5 syscall sw $v0, 0($s4) addi $s4, $s4, 4 addi $s6, $s6, 1 j read compute: la $t0, V lw $t1, N li $t2, 0 li $s0, 0 li $s1, 0 while: beq $t2, $t1, end lw $t3, 0($t0) bltz $t3, end andi $t4, $t2, 1 bne $t4, $zero, next slti $t4, $t3, 10 bne $t4, $zero, next slti $t4, $t3, 51 beq $t4, $zero, next addi $t3, $t3, 25 sw $t3, 0($t0) addi $s0, $s0, 1 next: addi $s1, $s1, 1 addi $t0, $t0, 4 addi $t2, $t2, 1 j while end: sw $s0, Healed sw $s1, Visited # print the whole array (the N elements), separated by spaces la $a0, msg_vec li $v0, 4 syscall la $s4, V li $s6, 0 print: beq $s6, $s5, counters lw $a0, 0($s4) li $v0, 1 syscall la $a0, space li $v0, 4 syscall addi $s4, $s4, 4 addi $s6, $s6, 1 j print counters: la $a0, newline li $v0, 4 syscall la $a0, msg_heal li $v0, 4 syscall move $a0, $s0 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall la $a0, msg_vis li $v0, 4 syscall move $a0, $s1 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall li $v0, 10 syscall
How many allies (1-20)? 10 Life of ally 0: 40 Life of ally 1: 12 Life of ally 2: 33 Life of ally 3: 55 Life of ally 4: 18 Life of ally 5: 7 Life of ally 6: 26 Life of ally 7: 49 Life of ally 8: -1 Life of ally 9: 30 Final array: 65 12 58 55 43 7 51 49 -1 30 Healed: 4 Visited: 8
N is read first and stored in memory (sw $v0, N) before the next syscall. The array is printed with a print_int + space loop over the N elements, including the ones after the negative.
The hits of a wave
In a fight, the array of words Hits keeps the damage of the 12 hits of a wave (positive numbers). Each hit is of one type:
- Critical if it is a multiple of 4: it counts double.
- Blocked if it is not critical but it is a multiple of 3: it is subtracted.
- Normal in any other case: it is added as it is.
Part 1 (compulsory, 8 marks)
Write a MIPS R2000 assembly program that:
- a. Computes the total damage with those rules (a hit that is a multiple of both 4 and 3, like 12, is critical).
- b. Counts the critical, blocked and normal hits.
- c. Stores in
Report(4 words), in this order: the total damage, the critical, the blocked and the normal hits.
Guidelines
- In memory:
Hits: .word 16, 9, 7, 24, 15, 11, 8, 21, 5, 12, 30, 13,N: .word 12andReport: .space 16(already in the file). Keep those label names. - The program must work for any array of 12 positive integers: the data change when it is marked.
- Hint: for «multiple of 4»
andiis enough (the two low bits); for «multiple of 3»,divand the remainder withmfhi.
Algorithm
Damage = 0; Critical = 0; Blocked = 0; Normal = 0
for i from 0 to N-1 do
if (Hits[i] is a multiple of 4) then
Damage = Damage + 2 * Hits[i]; Critical = Critical + 1
else if (Hits[i] is a multiple of 3) then
Damage = Damage - Hits[i]; Blocked = Blocked + 1
else
Damage = Damage + Hits[i]; Normal = Normal + 1
endif
endfor
Report = Damage, Critical, Blocked, NormalWith the statement data, Report ends up as 81, 4, 4, 4.
Solution
b-o1.s
# Ordinary B · Part 1: the hits of a wave .data Hits: .word 16, 9, 7, 24, 15, 11, 8, 21, 5, 12, 30, 13 N: .word 12 Report: .space 16 # Damage, Critical, Blocked, Normal .text .globl main main: la $t0, Hits lw $t1, N li $t2, 0 # i = 0 li $t7, 3 # divisor for «multiple of 3» li $s0, 0 # Damage = 0 li $s1, 0 # Critical = 0 li $s2, 0 # Blocked = 0 li $s3, 0 # Normal = 0 loop: beq $t2, $t1, end lw $t3, 0($t0) # g = Hits[i] andi $t4, $t3, 3 # the two low bits: 0 if g is a multiple of 4 bne $t4, $zero, not_critical sll $t5, $t3, 1 # critical: double (g << 1) add $s0, $s0, $t5 addi $s1, $s1, 1 j next not_critical: div $t3, $t7 # g / 3: the remainder goes to hi mfhi $t4 bne $t4, $zero, normal sub $s0, $s0, $t3 # blocked: subtract addi $s2, $s2, 1 j next normal: add $s0, $s0, $t3 # normal: add as it is addi $s3, $s3, 1 next: addi $t0, $t0, 4 addi $t2, $t2, 1 j loop end: la $t6, Report sw $s0, 0($t6) sw $s1, 4($t6) sw $s2, 8($t6) sw $s3, 12($t6) li $v0, 10 syscall
With the statement data, Report ends up in memory as 81, 4, 4, 4.
andi $t4, $t3, 3 keeps the two low bits: if they are 00, the number is a multiple of 4 without dividing. For 3 you need div and the remainder with mfhi. The order of the questions is the rule: first 4 (so 12 or 24 are critical) and each branch ends with j next so it does not fall into the next one.
Keyboard and screen
Part 2 (optional, 2 marks)
Change the program so that the user types the 12 hits (with a message for each one) and the program prints the report: four lines, each with a message in front, in the order of Report (which is also stored in memory).
It only counts if you have at least 4 marks in part 1.
Example (the last four console lines)
Total damage: 81 Critical: 4 Blocked: 4 Normal: 4
Solution
b-o2.s
# Ordinary B · Part 2: the hits from the keyboard and the report on screen .data Hits: .word 16, 9, 7, 24, 15, 11, 8, 21, 5, 12, 30, 13 N: .word 12 Report: .space 16 msg_ask: .asciiz "Hit " msg_colon:.asciiz ": " msg_dmg: .asciiz "Total damage: " msg_crit: .asciiz "Critical: " msg_blk: .asciiz "Blocked: " msg_nrm: .asciiz "Normal: " newline: .asciiz "\n" .text .globl main main: la $s4, Hits lw $s5, N li $s6, 0 read: beq $s6, $s5, compute la $a0, msg_ask li $v0, 4 syscall addi $a0, $s6, 1 li $v0, 1 syscall la $a0, msg_colon li $v0, 4 syscall li $v0, 5 syscall sw $v0, 0($s4) addi $s4, $s4, 4 addi $s6, $s6, 1 j read compute: la $t0, Hits lw $t1, N li $t2, 0 li $t7, 3 li $s0, 0 li $s1, 0 li $s2, 0 li $s3, 0 loop: beq $t2, $t1, end lw $t3, 0($t0) andi $t4, $t3, 3 bne $t4, $zero, not_critical sll $t5, $t3, 1 add $s0, $s0, $t5 addi $s1, $s1, 1 j next not_critical: div $t3, $t7 mfhi $t4 bne $t4, $zero, normal sub $s0, $s0, $t3 addi $s2, $s2, 1 j next normal: add $s0, $s0, $t3 addi $s3, $s3, 1 next: addi $t0, $t0, 4 addi $t2, $t2, 1 j loop end: la $t6, Report sw $s0, 0($t6) sw $s1, 4($t6) sw $s2, 8($t6) sw $s3, 12($t6) la $a0, msg_dmg li $v0, 4 syscall move $a0, $s0 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall la $a0, msg_crit li $v0, 4 syscall move $a0, $s1 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall la $a0, msg_blk li $v0, 4 syscall move $a0, $s2 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall la $a0, msg_nrm li $v0, 4 syscall move $a0, $s3 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall li $v0, 10 syscall
Hit 1: 16 Hit 2: 9 Hit 3: 7 Hit 4: 24 Hit 5: 15 Hit 6: 11 Hit 7: 8 Hit 8: 21 Hit 9: 5 Hit 10: 12 Hit 11: 30 Hit 12: 13 Total damage: 81 Critical: 4 Blocked: 4 Normal: 4
Reading goes in its own loop before the computation: read_int and sw into Hits. Then, the same loop as in part 1 and four blocks of message + number + newline.
Levelling up
At the end of a mission, each character in the group collects their experience. The array of words XP keeps each one’s experience points (integers ≥ 0) and N how many characters there are. Every 100 points is a level.
Part 1 (compulsory, 6 marks)
Write a MIPS assembly program that goes through the N elements of XP and, if a character has 100 points or more, adds to Levels the levels gained (XP[i] / 100) and leaves in XP[i] what is left (XP[i] mod 100). It also counts in Empty the characters that, after collecting, are left with 0 points.
Guidelines
- In memory:
XP: .word 250, 40, 100, 0, 375, 99, 512, 60,N: .word 8,Levels: .word 0andEmpty: .word 0. Keep those names. - Condition to pass: the program must work correctly for any set of data.
- Hint: a single
divgives the quotient (mflo) and the remainder (mfhi).
Algorithm
Levels = 0; Empty = 0
for i from 0 to N-1 do
if (XP[i] >= 100) then
Levels = Levels + XP[i] / 100
XP[i] = XP[i] mod 100
endif
if (XP[i] = 0) then Empty = Empty + 1 endif
endforWith the statement data: XP = 50, 40, 0, 0, 75, 99, 12, 60; Levels = 11; Empty = 2.
Solution
b-e1.s
# Resit B · Part 1: levelling up .data XP: .word 250, 40, 100, 0, 375, 99, 512, 60 N: .word 8 Levels: .word 0 Empty: .word 0 .text .globl main main: la $t0, XP lw $t1, N li $t2, 0 # i li $t7, 100 # divisor li $s0, 0 # Levels li $s1, 0 # Empty loop: beq $t2, $t1, end lw $t3, 0($t0) slti $t4, $t3, 100 # XP[i] < 100? bne $t4, $zero, check # yes: no level up div $t3, $t7 # quotient (lo) = levels; remainder (hi) = what is left mflo $t5 mfhi $t3 add $s0, $s0, $t5 # Levels += XP[i] / 100 sw $t3, 0($t0) # XP[i] = XP[i] mod 100 check: bne $t3, $zero, next # already transformed: is it 0 now? addi $s1, $s1, 1 # Empty++ next: addi $t0, $t0, 4 addi $t2, $t2, 1 j loop end: sw $s0, Levels sw $s1, Empty li $v0, 10 syscall
With the statement data: XP = 50, 40, 0, 0, 75, 99, 12, 60; Levels = 11; Empty = 2.
A single div $t3, $t7 leaves the quotient in lo and the remainder in hi: mflo is the levels and mfhi what is left, written back with sw in the same place. The empty check goes after the transformation: 100 becomes 0 and counts (checking before would give 1 instead of 2).
Subroutine level_up
Part 2 (optional, 2 marks)
Change the program so that collecting for one character is done in a subroutine called level_up, which receives the address of XP in $a0 and the position i in $a1, leaves in XP[i] what is left and returns in $v0 the levels gained (0 if it had less than 100). main calls it with jal for every element, adds $v0 to Levels and counts the empty ones.
for i from 0 to N-1 do Levels = Levels + level_up(XP, i) if (XP[i] = 0) then Empty = Empty + 1 endif endfor
It only counts if you have at least 3 marks in part 1.
Solution
b-e2.s
# Resit B · Part 2: level_up(XP, i) as a subroutine .data XP: .word 250, 40, 100, 0, 375, 99, 512, 60 N: .word 8 Levels: .word 0 Empty: .word 0 .text .globl main main: la $s0, XP lw $s1, N li $s2, 0 # i li $s3, 0 # Levels li $s4, 0 # Empty loop: beq $s2, $s1, end move $a0, $s0 # argument 1: XP move $a1, $s2 # argument 2: i jal level_up add $s3, $s3, $v0 # levels gained with this element sll $t0, $s2, 2 add $t0, $s0, $t0 lw $t1, 0($t0) # XP[i] already transformed by the subroutine bne $t1, $zero, next addi $s4, $s4, 1 next: addi $s2, $s2, 1 j loop end: sw $s3, Levels sw $s4, Empty li $v0, 10 syscall # level_up(XP in $a0, i in $a1): if XP[i] >= 100, XP[i] = XP[i] mod 100 and it returns XP[i] / 100 in $v0; otherwise 0. level_up: li $v0, 0 sll $t0, $a1, 2 add $t0, $a0, $t0 # &XP[i] lw $t1, 0($t0) slti $t2, $t1, 100 bne $t2, $zero, level_end li $t3, 100 div $t1, $t3 mflo $v0 # levels mfhi $t1 # what is left sw $t1, 0($t0) level_end: jr $ra
The subroutine returns a value in $v0 and also changes the array (it receives its address): that is why main reads XP[i] again after the jal to count the empty ones. Everything main needs after the call lives in $s registers.
Keyboard and screen
Part 3 (optional, 2 marks)
Change the program (the one from part 1: you do not need part 2) so that the user first types N (1 to 20) and then the N values of XP (declare XP: .space 80). At the end, print the collected XP array on one line, separated by spaces, and then Levels and Empty, each on its own line with a message in front.
It only counts if you have at least 3 marks in part 1.
Example (the last three console lines)
Final XP: 50 40 0 0 75 99 12 60 Levels gained: 11 Characters at zero: 2
Solution
b-e3.s
# Resit B · Part 3: the XP from the keyboard and the result on screen .data XP: .space 80 N: .word 0 Levels: .word 0 Empty: .word 0 msg_n: .asciiz "How many characters (1-20)? " msg_ask: .asciiz "XP of character " msg_colon:.asciiz ": " msg_vec: .asciiz "Final XP: " msg_lvl: .asciiz "Levels gained: " msg_emp: .asciiz "Characters at zero: " space: .asciiz " " newline: .asciiz "\n" .text .globl main main: la $a0, msg_n li $v0, 4 syscall li $v0, 5 syscall sw $v0, N move $s5, $v0 la $s4, XP li $s6, 0 read: beq $s6, $s5, compute la $a0, msg_ask li $v0, 4 syscall move $a0, $s6 li $v0, 1 syscall la $a0, msg_colon li $v0, 4 syscall li $v0, 5 syscall sw $v0, 0($s4) addi $s4, $s4, 4 addi $s6, $s6, 1 j read compute: la $t0, XP lw $t1, N li $t2, 0 li $t7, 100 li $s0, 0 li $s1, 0 loop: beq $t2, $t1, end lw $t3, 0($t0) slti $t4, $t3, 100 bne $t4, $zero, check div $t3, $t7 mflo $t5 mfhi $t3 add $s0, $s0, $t5 sw $t3, 0($t0) check: bne $t3, $zero, next addi $s1, $s1, 1 next: addi $t0, $t0, 4 addi $t2, $t2, 1 j loop end: sw $s0, Levels sw $s1, Empty la $a0, msg_vec li $v0, 4 syscall la $s4, XP li $s6, 0 print: beq $s6, $s5, counters lw $a0, 0($s4) li $v0, 1 syscall la $a0, space li $v0, 4 syscall addi $s4, $s4, 4 addi $s6, $s6, 1 j print counters: la $a0, newline li $v0, 4 syscall la $a0, msg_lvl li $v0, 4 syscall move $a0, $s0 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall la $a0, msg_emp li $v0, 4 syscall move $a0, $s1 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall li $v0, 10 syscall
How many characters (1-20)? 8 XP of character 0: 250 XP of character 1: 40 XP of character 2: 100 XP of character 3: 0 XP of character 4: 375 XP of character 5: 99 XP of character 6: 512 XP of character 7: 60 Final XP: 50 40 0 0 75 99 12 60 Levels gained: 11 Characters at zero: 2
First N (stored in memory), then a reading loop and at the end the printing loop with print_int and a space.
The user name
An online game only accepts nicks with letters and digits, and stores them in upper case. The nick the player types is in the string Nick.
Part 1 (compulsory, 8 marks)
Write a MIPS R2000 assembly program that goes through Nick character by character and:
- a. Counts the upper-case letters (A-Z), the lower-case letters (a-z), the digits (0-9) and the other characters (everything else: spaces,
_,!…). - b. Builds the clean nick in
Clean: only letters and digits, with lower case turned into upper case (a lower-case letter minus 32 is its upper case in ASCII). Close it with its'\0'. - c. Stores the four counters in
Count(4 words), in the order of a.
Guidelines
- In memory, in this order:
Count: .word 0, 0, 0, 0,Nick: .asciiz "Kira_99 la Rapida!"andClean: .space 64(already in the file; the words go before the strings so they stay aligned). Keep those label names. - The program must work for any nick of up to 63 characters: the string changes when it is marked.
Algorithm
i = 0; j = 0 while (Nick[i] <> 0) do if (Nick[i] is upper case) then Upper = Upper + 1; Clean[j] = Nick[i]; j = j + 1 else if (Nick[i] is lower case) then Lower = Lower + 1; Clean[j] = Nick[i] - 32; j = j + 1 else if (Nick[i] is a digit) then Digits = Digits + 1; Clean[j] = Nick[i]; j = j + 1 else Others = Others + 1 endif i = i + 1 endwhile Clean[j] = 0 Count = Upper, Lower, Digits, Others
With the statement data: Clean = «KIRA99LARAPIDA» and Count = 2, 10, 2, 4.
Solution
c-o1.s
# Ordinary C · Part 1: the user name .data Count: .word 0, 0, 0, 0 # Upper, Lower, Digits, Others Nick: .asciiz "Kira_99 la Rapida!" Clean: .space 64 # the nick, cleaned and in upper case .text .globl main main: la $t0, Nick # read pointer la $t1, Clean # write pointer (it only moves when copying) li $s0, 0 # upper case li $s1, 0 # lower case li $s2, 0 # digits li $s3, 0 # others loop: lbu $t2, 0($t0) # c = Nick[i] beqz $t2, end # the '\0' closes the string blt $t2, 'A', not_upper bgt $t2, 'Z', not_upper addi $s0, $s0, 1 # upper case: copied as it is sb $t2, 0($t1) addi $t1, $t1, 1 j next not_upper: blt $t2, 'a', not_lower bgt $t2, 'z', not_lower addi $s1, $s1, 1 # lower case: copied in upper case addi $t3, $t2, -32 # 'a' (97) - 32 = 'A' (65) sb $t3, 0($t1) addi $t1, $t1, 1 j next not_lower: blt $t2, '0', other bgt $t2, '9', other addi $s2, $s2, 1 # digit: copied sb $t2, 0($t1) addi $t1, $t1, 1 j next other: addi $s3, $s3, 1 # any other character: not copied next: addi $t0, $t0, 1 # +1: strings go byte by byte j loop end: sb $zero, 0($t1) # close Clean with its '\0' la $t4, Count sw $s0, 0($t4) sw $s1, 4($t4) sw $s2, 8($t4) sw $s3, 12($t4) li $v0, 10 syscall
With the statement nick, Clean ends up as «KIRA99LARAPIDA» and Count as 2, 10, 2, 4.
Two pointers: one reads Nick and always moves; the other writes into Clean and only moves when it copies. Strings go byte by byte (lbu, sb, +1) and the loop stops at the '\0'. Ranges with characters (blt $t2, 'A', …) are pseudo-instructions that SPIM accepts. At the end, sb $zero closes Clean: without it, a shorter string than the previous one would leave leftovers.
Keyboard and screen
Part 2 (optional, 2 marks)
Change the program so that the user types the nick (read_string into Nick, which is now .space 64) and the program prints the clean nick on one line and then the four counters, each on its own line with a message in front (they are also stored in memory). Watch out for the '\n' that read_string leaves: it is not part of the nick.
It only counts if you have at least 4 marks in part 1.
Example (the last five console lines)
Clean nick: KIRA99LARAPIDA Upper case: 2 Lower case: 10 Digits: 2 Others: 4
Solution
c-o2.s
# Ordinary C · Part 2: the nick from the keyboard and the result on screen .data Count: .word 0, 0, 0, 0 Nick: .space 64 # now the user types it Clean: .space 64 msg_ask: .asciiz "Type your nick: " msg_cln: .asciiz "Clean nick: " msg_up: .asciiz "Upper case: " msg_low: .asciiz "Lower case: " msg_dig: .asciiz "Digits: " msg_oth: .asciiz "Others: " newline: .asciiz "\n" .text .globl main main: la $a0, msg_ask li $v0, 4 syscall la $a0, Nick # read_string: buffer in $a0 and size in $a1 li $a1, 64 li $v0, 8 syscall # remove the '\n' that read_string leaves (otherwise it would count as «other») la $t0, Nick strip_nl: lbu $t2, 0($t0) beqz $t2, process li $t3, 10 bne $t2, $t3, next_nl sb $zero, 0($t0) j process next_nl: addi $t0, $t0, 1 j strip_nl process: la $t0, Nick la $t1, Clean li $s0, 0 li $s1, 0 li $s2, 0 li $s3, 0 loop: lbu $t2, 0($t0) beqz $t2, end blt $t2, 'A', not_upper bgt $t2, 'Z', not_upper addi $s0, $s0, 1 sb $t2, 0($t1) addi $t1, $t1, 1 j next not_upper: blt $t2, 'a', not_lower bgt $t2, 'z', not_lower addi $s1, $s1, 1 addi $t3, $t2, -32 sb $t3, 0($t1) addi $t1, $t1, 1 j next not_lower: blt $t2, '0', other bgt $t2, '9', other addi $s2, $s2, 1 sb $t2, 0($t1) addi $t1, $t1, 1 j next other: addi $s3, $s3, 1 next: addi $t0, $t0, 1 j loop end: sb $zero, 0($t1) la $t4, Count sw $s0, 0($t4) sw $s1, 4($t4) sw $s2, 8($t4) sw $s3, 12($t4) la $a0, msg_cln li $v0, 4 syscall la $a0, Clean li $v0, 4 syscall la $a0, newline li $v0, 4 syscall la $a0, msg_up li $v0, 4 syscall move $a0, $s0 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall la $a0, msg_low li $v0, 4 syscall move $a0, $s1 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall la $a0, msg_dig li $v0, 4 syscall move $a0, $s2 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall la $a0, msg_oth li $v0, 4 syscall move $a0, $s3 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall li $v0, 10 syscall
Type your nick: Kira_99 la Rapida! Clean nick: KIRA99LARAPIDA Upper case: 2 Lower case: 10 Digits: 2 Others: 4
read_string also stores the Enter key ('\n', 10): if it is not removed, it counts as «other» and the count is one too high. The strip_nl loop replaces it with a 0 before processing.
The ciphered message
The clan passes messages in a simple cipher. Each character of the string Message is transformed like this:
- The vowels a, e, i, o and the s (upper or lower case) become 4, 3, 1, 0 and 5. Each of these replacements counts in
Changes. - The other lower-case letters become upper case (subtracting 32).
- Everything else (upper case that is not a, e, i, o or s, digits, spaces, signs) is copied as it is.
Part 1 (compulsory, 6 marks)
Write a MIPS assembly program that builds the ciphered message in Cipher (closed with its '\0') and stores in Changes the number of replacements.
Guidelines
- In memory, in this order:
Changes: .word 0,Message: .asciiz "See you at the base at 8"andCipher: .space 64. Keep those names. - Condition to pass: the program must work correctly for any message of up to 63 characters.
Algorithm
Changes = 0; i = 0 while (Message[i] <> 0) do c = Message[i]; m = c in lower case if it is upper case (c + 32) if (m = 'a') then Cipher[i] = '4'; Changes = Changes + 1 else if (m = 'e') then Cipher[i] = '3'; Changes = Changes + 1 … the same with 'i' → '1', 'o' → '0', 's' → '5' else if (c is lower case) then Cipher[i] = c - 32 else Cipher[i] = c endif i = i + 1 endwhile Cipher[i] = 0
With the statement data: Cipher = «533 Y0U 4T TH3 B453 4T 8» and Changes = 10.
Solution
c-e1.s
# Resit C · Part 1: the ciphered message .data Changes: .word 0 Message: .asciiz "See you at the base at 8" Cipher: .space 64 .text .globl main main: la $t0, Message la $t1, Cipher li $s0, 0 # Changes loop: lbu $t2, 0($t0) # c beqz $t2, end move $t3, $t2 # t3 = c in lower case (only to compare) blt $t2, 'A', compare bgt $t2, 'Z', compare addi $t3, $t2, 32 # 'A' + 32 = 'a' compare: li $t4, '4' beq $t3, 'a', change li $t4, '3' beq $t3, 'e', change li $t4, '1' beq $t3, 'i', change li $t4, '0' beq $t3, 'o', change li $t4, '5' beq $t3, 's', change # not a, e, i, o or s: lower case becomes upper case; the rest stays move $t4, $t2 blt $t2, 'a', store bgt $t2, 'z', store addi $t4, $t2, -32 j store change: addi $s0, $s0, 1 # Changes++ (only the replacements by a digit) store: sb $t4, 0($t1) addi $t0, $t0, 1 addi $t1, $t1, 1 j loop end: sb $zero, 0($t1) # close Cipher sw $s0, Changes li $v0, 10 syscall
With the statement message: Cipher = «533 Y0U 4T TH3 B453 4T 8» and Changes = 10.
The trick is to compare with a lower-case copy (t3): that way 'O' and 'o' fall into the same branch. Only the replacements by a digit add to Changes. Both pointers move together (the cipher has the same length) and at the end sb $zero closes Cipher.
Subroutine translate
Part 2 (optional, 2 marks)
Change the program so that the transformation of one character is done in a subroutine called translate: it receives the character in $a0 and returns the ciphered character in $v0 and, in $v1, a 1 if it was a replacement by a digit (0 if not). main calls it with jal for every character, stores $v0 in Cipher and adds $v1 to Changes.
It only counts if you have at least 3 marks in part 1.
Solution
c-e2.s
# Resit C · Part 2: translate(c) as a subroutine .data Changes: .word 0 Message: .asciiz "See you at the base at 8" Cipher: .space 64 .text .globl main main: la $s0, Message la $s1, Cipher li $s2, 0 # Changes loop: lbu $a0, 0($s0) # argument: the character beqz $a0, end jal translate sb $v0, 0($s1) # $v0 = the new character add $s2, $s2, $v1 # $v1 = 1 if it was a replacement addi $s0, $s0, 1 addi $s1, $s1, 1 j loop end: sb $zero, 0($s1) sw $s2, Changes li $v0, 10 syscall # translate(c in $a0) -> $v0 = ciphered character, $v1 = 1 if it was replaced by a digit (0 if not) translate: li $v1, 1 move $t3, $a0 blt $a0, 'A', tr_compare bgt $a0, 'Z', tr_compare addi $t3, $a0, 32 tr_compare: li $v0, '4' beq $t3, 'a', tr_end li $v0, '3' beq $t3, 'e', tr_end li $v0, '1' beq $t3, 'i', tr_end li $v0, '0' beq $t3, 'o', tr_end li $v0, '5' beq $t3, 's', tr_end li $v1, 0 # no replacement move $v0, $a0 blt $a0, 'a', tr_end bgt $a0, 'z', tr_end addi $v0, $a0, -32 tr_end: jr $ra
A subroutine can return two values: $v0 and $v1. translate is a leaf that only uses $t, $v0 and $v1; main keeps the pointers and the counter in $s because a call may change the $t registers.
Keyboard and screen
Part 3 (optional, 2 marks)
Change the program (the one from part 1: you do not need part 2) so that the user types the message (read_string into Message, which is now .space 64; remove the '\n' at the end) and the program prints the ciphered message on one line and the changes on the next, each with a message in front.
It only counts if you have at least 3 marks in part 1.
Example (the last two console lines)
Cipher: 533 Y0U 4T TH3 B453 4T 8 Changes: 10
Solution
c-e3.s
# Resit C · Part 3: the message from the keyboard and the cipher on screen .data Changes: .word 0 Message: .space 64 Cipher: .space 64 msg_ask: .asciiz "Message: " msg_cip: .asciiz "Cipher: " msg_chg: .asciiz "Changes: " newline: .asciiz "\n" .text .globl main main: la $a0, msg_ask li $v0, 4 syscall la $a0, Message li $a1, 64 li $v0, 8 syscall la $t0, Message # remove the '\n' at the end strip_nl: lbu $t2, 0($t0) beqz $t2, process li $t3, 10 bne $t2, $t3, next_nl sb $zero, 0($t0) j process next_nl: addi $t0, $t0, 1 j strip_nl process: la $t0, Message la $t1, Cipher li $s0, 0 loop: lbu $t2, 0($t0) beqz $t2, end move $t3, $t2 blt $t2, 'A', compare bgt $t2, 'Z', compare addi $t3, $t2, 32 compare: li $t4, '4' beq $t3, 'a', change li $t4, '3' beq $t3, 'e', change li $t4, '1' beq $t3, 'i', change li $t4, '0' beq $t3, 'o', change li $t4, '5' beq $t3, 's', change move $t4, $t2 blt $t2, 'a', store bgt $t2, 'z', store addi $t4, $t2, -32 j store change: addi $s0, $s0, 1 store: sb $t4, 0($t1) addi $t0, $t0, 1 addi $t1, $t1, 1 j loop end: sb $zero, 0($t1) sw $s0, Changes la $a0, msg_cip li $v0, 4 syscall la $a0, Cipher li $v0, 4 syscall la $a0, newline li $v0, 4 syscall la $a0, msg_chg li $v0, 4 syscall move $a0, $s0 li $v0, 1 syscall la $a0, newline li $v0, 4 syscall li $v0, 10 syscall
Message: See you at the base at 8 Cipher: 533 Y0U 4T TH3 B453 4T 8 Changes: 10
read_string leaves the Enter key inside the string: without the strip_nl loop, the cipher would end with an extra newline (and the changes line would not be the last one).
The environment starts with the hints off (no colours, no autocomplete), like Notepad++ on exam day. If you get stuck, turn them on in «Hints», but get used to writing without them.
Nothing is stored on any server. Your code stays in this browser while you take the mock exam (if you reload, it is still there) and it is deleted when you hand in. «Download as PDF» gives you the statement, your code and what the tests said.
—
What the IC1 practical exam is like
It takes place in the school’s labs, with the Safe Exam Browser: you only have Notepad++, QtSpim and the subject’s lab book. You write the program in Notepad++, load it into QtSpim, run it, and at the end you upload one .s file per part.
| Ordinary | Resit | |
|---|---|---|
| Time | 90 minutes | 90 minutes |
| Part 1 (compulsory) | 8 marks | 6 marks |
| Optional parts | part 2: 2 marks (data from the keyboard and results on screen) | part 2: 2 marks (moving part of the program into a subroutine) and part 3: 2 marks (keyboard and screen) |
| Condition to pass | the program works for any set of data | the same |
| The optional parts count if… | part 1 reaches 4 | part 1 reaches 3 |
Look at the dashed lines in the drawing: in this mock exam the optional parts only add up if part 1 reaches half of its marks. It is a rule that has appeared in real exams («this part only counts if you got at least 5 in the previous one») and it makes sense: a part 2 that asks for «the same but reading from the keyboard» is worth nothing if «the same» does not work.
The statement always brings three things: the data you have to declare in .data (with their label names), the algorithm in pseudocode and what has to be left in memory at the end. You do not need to invent the algorithm: you need to translate it into MIPS without it breaking with other data.
Before writing a single instruction, copy the pseudocode as comments into your .s. Each pseudocode line becomes two or three instructions under its comment, and you do not get lost.
—
How it is marked
In the real exam the lecturers mark it, and the first thing they do is the same thing the tests of this mock exam do: change the data and run it again. If the result changes as it should, the program works «for any set of data»; if not, it does not.
Each part has two to four tests with a weight. Each test:
| Step | What it does | Example |
|---|---|---|
| 1. Other data | Replaces what follows a .data label | Potions: .word 10, 99, -51, 50, … |
| 2. Keyboard | Types the lines your program asks for (read_int, read_string) | 35⏎ -20⏎ … |
| 3. Run | Assembles and runs with the SPIM core, the same as QtSpim (5 million instructions at most) | if it does not finish: «It does not finish» |
| 4. Check | The memory of the result labels, the last console lines and, for subroutines, the code | Result[0] is 252; expected 352 |
The drawing above is the real run of a program that stores the results «by hand» (li $t0, 170 and sw): it passes the statement-data test (3 marks) and fails the other three. With 3 out of 8 it does not reach the minimum of 4, so part 2 does not count even if it is perfect: the final mark is a 3.
These are the sums for real cases (run in the simulator with the mistakes from «The cases that fail the most»):
| What you hand in | Part 1 | Optional parts | Mark |
|---|---|---|---|
| Ordinary A, all correct | 8 out of 8 | 2 out of 2 | 10 |
| Ordinary A, 100 counts as corrupted | 3 + 1 = 4 out of 8 (fails «another vector» and «edge cases») | 2 out of 2 (they count: 4 is half) | 6 |
| Ordinary A, results by hand | 3 out of 8 | 2 out of 2, but they do not count | 3 (with «if they counted, you would have 5») |
| Resit A, without the stop at the negative | 1.5 + 1.5 = 3 out of 6 | 2 + 2 | 7 |
Following the algorithm but changing the label names (potions, res, Results). The tests look for them by name, as the lecturer looks for Vector2 in the Data tab: if it is not there, the part gets 0 and the message «I cannot find the label Result».
—
How to use the environment in the mock exam
The environment is the same as on the MIPS playground page, with the Notepad++-style editor and the QtSpim-style simulator, but inside the exam:
- Editor: the file of each part (
part1.s,part2.s…) already has the statement data in.dataand the end of the program. Write your code undermain:. - Load assembles and loads, like QtSpim’s «Reinitialize and Load File». If there is a syntax error, the «Messages» tab says on which line.
- Run (F5) runs it all; Step (F10) goes one instruction at a time. In «Simulator» you see the registers and the Text and Data tabs, as in QtSpim: that is where you check that
Resultholds what it should. - If the program reads from the keyboard, the console shows an «Input» row: type and press Enter.
- In the optional parts, the «Bring my code from part 1» button copies your part 1 so you can change it (in the real exam you would use «Save as» in Notepad++).
- Hand in runs the tests of every part with the last version of your code. Anything you have not touched counts as blank.
On a phone, at the bottom you have three tabs: Statement, Code and Result. It can be done, but the real exam is with a keyboard and a big screen: if you can, do it on a computer.
Before handing in, change the .data values yourself (put a negative, a value right on the edge, an array where nothing counts) and run it again. It is exactly what the tests will do.
—
How to split the 90 minutes
| Minutes | Ordinary | Resit |
|---|---|---|
| 0-10 | Read the statement, copy the pseudocode as comments | The same |
| 10-55 / 10-50 | Part 1 | Part 1 |
| 55-65 / 50-58 | Test part 1 with other data | The same |
| 65-82 / 58-84 | Part 2 | Part 2 (subroutine) and part 3 (keyboard) |
| Last minutes | Check the labels, upload the .s files | The same |
In the resit, the two optional parts are independent («you do not need part 2 for part 3»): start with the one you find easier. The keyboard one is usually more mechanical; the subroutine one, shorter if the template is clear in your head.
—
The cases that fail the most
These five mistakes were run in the simulator with the mock exam’s tests; the messages are the ones you get when you hand in.
The edge of the range
«Between −50 and 100, both included» has two edges and both are traps. This version checks «x < 100» and flips it with xori, so 100 itself comes out as corrupted:
slti $t4, $t3, 100 # x < 100? xori $t4, $t4, 1 # wrong: 100 also comes out corrupted bne $t4, $zero, corrupt
With the statement data there is no 100 and the test passes. With another array, there is: «Result[0] is 252; expected 352 (Result = [252, 2, 3, 3])». The right way is slt $t4, $t5, $t3 with $t5 = 100: «100 < x?», which leaves 101 out and 100 in.
Counting before transforming
In resit B, Empty counts the characters left at 0 after collecting. If the zero is checked before the div, 100 (which becomes 0) does not count: «Empty is 1; expected 2.» The order of the pseudocode lines is part of the statement.
The ‘\n’ of read_string
read_string also stores the Enter key. If you do not remove it, it counts as one more character («other» in ordinary C): «Count[3] is 5; expected 4 (Count = [2, 10, 2, 5]).» The loop that replaces it with a 0 is eight lines worth learning by heart (they are in the cheat sheet).
The missing stop
«While (i <= N-1) and (V[i] >= 0)» means two exits from the loop. Without the bltz for the negative, the program keeps healing allies that were out of reach: «Visited is 10; expected 8.»
A word after a string
If you declare a string and then reserve words with .space, the address can end up in the middle of a word and the first sw blows up: «It stops with an exception: Unaligned address in store: 0x10010053.» .word aligns itself; .space does not. Put the words before the strings or use .align 2.
When QtSpim says «Unaligned address», look at the address in the Data tab: if it does not end in 0, 4, 8 or c, the label is not aligned.
—
The optional parts: a short guide
Moving part of the program into a subroutine
It is part 2 of the resit, and the statement gives you the subroutine’s name, its arguments and what it returns. The template is always the same (explained in Subroutines and the stack):
# in main: whatever must survive the call, in $s registers move $a0, $s0 # argument 1: the address of the array move $a1, $s2 # argument 2: the position i jal heal add $s3, $s3, $v0 # what it returns, in $v0 heal: # a leaf: it calls nobody sll $t0, $a1, 2 # i * 4 add $t0, $a0, $t0 # &V[i] … # only $t registers: no stack needed jr $ra
The tests check the code (that the label exists, that main calls it with jal and that it returns with jr $ra) and also that the results are still right. If the subroutine called another one (not a leaf), it would have to save $ra on the stack.
Reading the data from the keyboard and printing the results
It is part 2 of the ordinary exam and part 3 of the resit. Three pieces, all from the system calls topic:
- Read N numbers: a loop with a message (service 4),
read_int(service 5) andsw $v0into the array before the nextsyscall. - Read a string:
read_string(service 8) with the buffer in$a0and its size in$a1, and then remove the'\n'. - Print: message,
print_int(service 1) and a newline, once per result and in the order the statement asks for. The tests look at the last lines of the console, so the message can be whatever you like.
—
Common mistakes in the practical exam
- Changing the label names from the statement: the tests (and the lecturer) look for them.
- Writing the results by hand or using the statement data inside the code (
li $t1, 12instead oflw $t1, N): with other data, the program fails. - Moving the pointer by the wrong size: +4 for
.word, +1 for strings and.byte. - Forgetting the
j nextat the end of an «if» branch: the program falls into the next branch. - Using
$tfor whatever must survive ajalor asyscall:$v0changes with every system call and$tregisters are not preserved by convention. - Not closing the output string with
sb $zero: with a nick shorter than the previous one, leftovers remain.
—
Try it in QtSpim (or right here)
This is the template you will use in almost every part 1: go through an array, ask about a range and store the result. Copy it into QtSpim or run it right here:
# The exam template: go through an array, ask and store the result .data V: .word 12, 55, -3, 40, 8, 50, 10, 99 N: .word 8 Inside: .word 0 # how many are between 10 and 50 .text .globl main main: la $t0, V # pointer to V[i] lw $t1, N li $t2, 0 # i = 0 li $s0, 0 # counter loop: beq $t2, $t1, end # i == N: done lw $t3, 0($t0) slti $t4, $t3, 10 # V[i] < 10? yes: out bne $t4, $zero, next slti $t4, $t3, 51 # V[i] <= 50? no: out beq $t4, $zero, next addi $s0, $s0, 1 # inside the range next: addi $t0, $t0, 4 # +4: words addi $t2, $t2, 1 j loop end: sw $s0, Inside # the result, to memory move $a0, $s0 li $v0, 1 # and to the console syscall li $v0, 10 syscall
When you run it, the console shows:
4
Four of the eight values are between 10 and 50: 12, 40, 50 and 10 (50 and 10 count because they are the edges).
What to look at:
- Press Run and check that the console says 4. Then, in «Simulator» → Data tab, the word of
Inside(the tenth, at0x10010024, after the eight values andN) is 4. - Change the
.dataarray to5, 10, 50, 51andNto 4, and run it again: it must give 2 (10 and 50). That is the edge test. - Set
N: .word 0: the loop does no turns and it gives 0. A program that «works for any set of data» also works with an empty array. - Run it with Step (F10) and watch
$t2(i) and$s0(the counter) in the registers tab: the counter only goes up in turns 0, 3, 5 and 6.
—
Cheat sheet: what you can look up
In the real exam you can look up the lab book. This is what you will look for most.
The essentials in 30 seconds: declare the data with the statement’s names, translate the pseudocode line by line, store the results with sw/sb and test with other data before handing in.
| I want… | Code | Result |
|---|---|---|
| The address of an array | la $t0, V | $t0 = address of V |
| A value from memory | lw $t1, N | $t1 = N |
| Walk through words / bytes | addi $t0, $t0, 4 / addi $t0, $t0, 1 | next element |
&V[i] from i | sll $t5, $t2, 2 + add $t5, $s0, $t5 | address of element i |
| Is it even? | andi $t4, $t2, 1 | 0 if even, 1 if odd |
| Multiple of 4? | andi $t4, $t3, 3 | 0 if it is a multiple |
| Multiple of 3? / quotient and remainder | div $t3, $t7 + mfhi / mflo | remainder / quotient |
| x < 10? / x > 100? | slti $t4, $t3, 10 / slt $t4, $t5, $t3 with $t5 = 100 | 1 if it holds |
| Lower case → upper case | addi $t3, $t2, -32 | 'a' (97) → 'A' (65) |
| Close a string | sb $zero, 0($t1) | '\0' at the end |
| Call a subroutine | jal name … jr $ra | arguments in $a0–$a3, result in $v0/$v1 |
| Service | $v0 | Before | After |
|---|---|---|---|
| print_int | 1 | $a0 = number | — |
| print_string | 4 | $a0 = address | — |
| read_int | 5 | — | number in $v0 |
| read_string | 8 | $a0 = buffer, $a1 = size | text in the buffer (with '\n') |
| print_char | 11 | $a0 = character | — |
| exit | 10 | — | — |
Template: remove the ‘\n’ from read_string
la $t0, Buffer strip_nl: lbu $t2, 0($t0) beqz $t2, ready # there was no '\n' li $t3, 10 bne $t2, $t3, next_nl sb $zero, 0($t0) # the '\n' becomes the end j ready next_nl: addi $t0, $t0, 1 j strip_nl ready:
Template: print an array of N words separated by spaces
la $s4, V li $s6, 0 print: beq $s6, $s5, printed # $s5 = N lw $a0, 0($s4) li $v0, 1 syscall la $a0, space # space: .asciiz " " li $v0, 4 syscall addi $s4, $s4, 4 addi $s6, $s6, 1 j print printed:
| Symptom | Likely cause |
|---|---|
| «I cannot find the label …» | You changed the name the statement asks for |
| Right with the statement data, wrong with other data | An edge (< instead of <=) or a result written by hand |
| «It does not finish» | A loop without the index addi or a j that goes back to the wrong place |
| «Unaligned address» | Words after a string without .align 2, or lw on an array of bytes |
| One «other» too many when reading a string | The '\n' from read_string |
| The subroutine returns to a strange place | $ra was lost (it calls another one without saving it on the stack) |
Before handing in, ask yourself:
- Are the labels named exactly as in the statement?
- Have I tested with other data, with a value right on each edge and with a case where nothing counts?
- Does every «if» branch end with its
j, and does every loop move its index and its pointer? - Is whatever must survive a
jalor asyscallin an$sregister or in memory?
—
What to review on the website
| If you fail at… | Review |
|---|---|
Declaring data, .word, .space, .asciiz | Program structure and data |
lw, sw, add, andi, div and mfhi | Load, arithmetic and logic |
| Translating an «if», an «and», an «or» and a loop | Branches, conditionals and loops |
Walking through arrays and strings, &V[i], the '\0' | Arrays and strings |
read_int, read_string and the '\n' | System calls and console I/O |
jal, jr $ra, $a0–$a1, $v0 | Subroutines and the stack |
| Loading and running in QtSpim | How to use QtSpim |
—
Summary
| Idea | In one sentence |
|---|---|
| Format | 90 minutes, Notepad++ and QtSpim, one .s per part |
| Marks | Ordinary 8 + 2; resit 6 + 2 + 2 |
| Minimum | The optional parts count if part 1 reaches half |
| Marking | Other data, the typed input and memory, console and code |
| Optional parts | A subroutine with $a0/$a1/$v0, or the data from the keyboard and the results on screen |
Three ideas to take away:
- Part 1 decides almost the whole mark: secure it before looking at the optional parts.
- «For any set of data» means the lecturer will change your
.data: do it yourself first. - The label names, the edges of the ranges and the
'\n'fromread_stringare where most marks are lost.
Want to review the theory before trying again? All the degree’s mock exams are in Exams and mock exams.
Keep goingAll MIPS articlesDid this article help you? Here is how you can support SergioLearns →



