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.

—

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 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.

OrdinaryResit
Time90 minutes90 minutes
Part 1 (compulsory)8 marks6 marks
Optional partspart 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 passthe program works for any set of datathe same
The optional parts count if…part 1 reaches 4part 1 reaches 3
Marks in the IC1 practical examTwo 10-mark bars. Ordinary: part 1 worth 8 and part 2 worth 2, with the minimum at 4. Resit: part 1 worth 6 and two optional parts worth 2 (subroutine and keyboard), with the minimum at 3.Two exams, 10 marks eachOrdinaryPart 1 · 8P. 2 · 24 minimum for the optional parts to countResitPart 1 · 6P. 2 · 2P. 3 · 23 minimum for the optional parts to countcompulsorykeyboard and screensubroutinekeyboard
Part 1 decides almost everything: below the dashed line, the optional parts do not add up.

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.

How a part is markedFive steps: your file, other data in .data, the typed input, the run with the SPIM core and the check of memory, console and code. Below, the four tests of part 1 of ordinary A with a program that stores the results by hand: only the statement-data test passes (3 out of 8).How a part is markedYour .sResult.space 161 · Other dataPotions:.word 10, 99…2 · Keyboardwhat it asksread_int3 · RunSPIMcore4 · Checkmemory,console, codeExample: storing 170, 3, 2, 3 «by hand» with li and sw (ordinary A)With the statement dataweight 3With another 12-element vectorweight 2Result[0] = 170Edge casesweight 2Result[0] = 170All corrupted (life 0)weight 1Result[0] = 170Mark for the part: 3 out of 8 → below 4: the optional parts do not count.
A program that only works with the statement data gets 3 out of 8: that is why «it must work for any set of data».

Each part has two to four tests with a weight. Each test:

StepWhat it doesExample
1. Other dataReplaces what follows a .data labelPotions: .word 10, 99, -51, 50, …
2. KeyboardTypes the lines your program asks for (read_int, read_string)35⏎ -20⏎ …
3. RunAssembles 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. CheckThe memory of the result labels, the last console lines and, for subroutines, the codeResult[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 inPart 1Optional partsMark
Ordinary A, all correct8 out of 82 out of 210
Ordinary A, 100 counts as corrupted3 + 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 hand3 out of 82 out of 2, but they do not count3 (with «if they counted, you would have 5»)
Resit A, without the stop at the negative1.5 + 1.5 = 3 out of 62 + 27

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:

  1. Editor: the file of each part (part1.s, part2.s…) already has the statement data in .data and the end of the program. Write your code under main:.
  2. Load assembles and loads, like QtSpim’s «Reinitialize and Load File». If there is a syntax error, the «Messages» tab says on which line.
  3. 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 Result holds what it should.
  4. If the program reads from the keyboard, the console shows an «Input» row: type and press Enter.
  5. 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++).
  6. 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

Splitting the 90 minutesTwo 90-minute bars. Ordinary: 10 to read, 45 for part 1, 10 to test with other data, 17 for part 2 and 8 to review. Resit: 10, 40, 8, 14 for part 2, 12 for part 3 and 6 to review.90 minutes, one file per partOrdinaryreadpart 1 (8)testp. 2 (2)review01030507090Resitreadpart 1 (6)testp. 2p. 301030507090Suggested split. «Test»: change the .data values and check that the result changes as it should.
The amber slot is the one that saves the most marks: testing part 1 with data that are not the statement data.
MinutesOrdinaryResit
0-10Read the statement, copy the pseudocode as commentsThe same
10-55 / 10-50Part 1Part 1
55-65 / 50-58Test part 1 with other dataThe same
65-82 / 58-84Part 2Part 2 (subroutine) and part 3 (keyboard)
Last minutesCheck the labels, upload the .s filesThe 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) and sw $v0 into the array before the next syscall.
  • Read a string: read_string (service 8) with the buffer in $a0 and 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, 12 instead of lw $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 next at the end of an «if» branch: the program falls into the next branch.
  • Using $t for whatever must survive a jal or a syscall: $v0 changes with every system call and $t registers 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:

  1. Press Run and check that the console says 4. Then, in «Simulator» → Data tab, the word of Inside (the tenth, at 0x10010024, after the eight values and N) is 4.
  2. Change the .data array to 5, 10, 50, 51 and N to 4, and run it again: it must give 2 (10 and 50). That is the edge test.
  3. 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.
  4. 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…CodeResult
The address of an arrayla $t0, V$t0 = address of V
A value from memorylw $t1, N$t1 = N
Walk through words / bytesaddi $t0, $t0, 4 / addi $t0, $t0, 1next element
&V[i] from isll $t5, $t2, 2 + add $t5, $s0, $t5address of element i
Is it even?andi $t4, $t2, 10 if even, 1 if odd
Multiple of 4?andi $t4, $t3, 30 if it is a multiple
Multiple of 3? / quotient and remainderdiv $t3, $t7 + mfhi / mfloremainder / quotient
x < 10? / x > 100?slti $t4, $t3, 10 / slt $t4, $t5, $t3 with $t5 = 1001 if it holds
Lower case → upper caseaddi $t3, $t2, -32'a' (97) → 'A' (65)
Close a stringsb $zero, 0($t1)'\0' at the end
Call a subroutinejal name … jr $raarguments in $a0–$a3, result in $v0/$v1
Service$v0BeforeAfter
print_int1$a0 = number—
print_string4$a0 = address—
read_int5—number in $v0
read_string8$a0 = buffer, $a1 = sizetext in the buffer (with '\n')
print_char11$a0 = character—
exit10——

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:
SymptomLikely cause
«I cannot find the label …»You changed the name the statement asks for
Right with the statement data, wrong with other dataAn 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 stringThe '\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:

  1. Are the labels named exactly as in the statement?
  2. Have I tested with other data, with a value right on each edge and with a case where nothing counts?
  3. Does every «if» branch end with its j, and does every loop move its index and its pointer?
  4. Is whatever must survive a jal or a syscall in an $s register or in memory?

—

What to review on the website

If you fail at…Review
Declaring data, .word, .space, .asciizProgram structure and data
lw, sw, add, andi, div and mfhiLoad, arithmetic and logic
Translating an «if», an «and», an «or» and a loopBranches, 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, $v0Subroutines and the stack
Loading and running in QtSpimHow to use QtSpim

—

Summary

IdeaIn one sentence
Format90 minutes, Notepad++ and QtSpim, one .s per part
MarksOrdinary 8 + 2; resit 6 + 2 + 2
MinimumThe optional parts count if part 1 reaches half
MarkingOther data, the typed input and memory, console and code
Optional partsA subroutine with $a0/$a1/$v0, or the data from the keyboard and the results on screen

Three ideas to take away:

  1. Part 1 decides almost the whole mark: secure it before looking at the optional parts.
  2. «For any set of data» means the lecturer will change your .data: do it yourself first.
  3. The label names, the edges of the ranges and the '\n' from read_string are 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 articles

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