Item | Detail |
|---|---|
Course learning outcomes | CLO6 - low-level software in MIPS assembly (P3); CLO2 - measure performance with instruction count (C3). |
Tool | MARS 4.5: Edit, Execute (Run > Step), Registers window, Tools > Instruction Counter |
Linked lectures | Lecture 9 (control flow: conditional branches and jumps); Lecture 4 (CPU time = IC x CPI x clock time); Practicals 7-8 (branches). |
Assessment level | Psychomotor P3 (Guided Response): you perform each step by following the guided procedure, then on your own. |
1. Objectives
By the end of this practical you will be able to:
- Write while, for and do-while loops in MIPS with a counter, a test and a jump back.
- Trace a loop pass by pass and find off-by-one and infinite-loop bugs.
- Measure how the instruction count grows with n, and compare a loop with a formula.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Loop | A group of instructions that repeats | Counting 10 tasbeeh beads one by one |
Counter (i) | A register that counts the passes | The over number on a cricket scoreboard |
Loop test | The branch that decides to repeat or leave | Checking 'are all 6 balls bowled?' |
Iteration (pass) | One run of the loop body | One ball of an over |
Off-by-one | A loop that runs one time too many or too few | Counting 5 balls as an over instead of 6 |
Infinite loop | A loop whose test never becomes true | A rickshaw going round the same chowk forever |
Closed form | A direct formula with no loop | Using n(n+1)/2 instead of adding one by one |
3. Background
3.1 The while / for pattern: test at the top
for loop = initialise, test, body, update, jump back |
The test is INVERTED: C says 'repeat while i <= n', so MIPS branches out when i > n. If n is 0 or negative, the body never runs.
3.2 The do-while pattern: test at the bottom
do-while |
The body always runs at least once, and the j disappears, so each pass costs fewer instructions. Compilers often turn a while loop into this shape (with one extra test before the loop).
3.3 Counting instructions in a loop
Instructions executed = (instructions per pass) x (number of passes) + (instructions outside the loop). Remember that blt, bgt, ble and bge are pseudo-branches: each becomes slt + beq/bne, so it counts as 2 (Practical 7). Use Tools > Instruction Counter: open it, press 'Connect to MIPS', then run.
4. Time Plan (50 minutes)
Time | Activity | Marks |
|---|---|---|
0-6 | Background 3.1-3.3 | - |
6-14 | Part A: trace a counting loop | - |
14-18 | Part B: predict the output | - |
18-23 | Part C: bug hunt | - |
23-46 | Part D: graded tasks 1-3 | 10 |
46-50 | Part E: post-lab questions; save; submit | - |
Part A - Warm-up: Trace a Counting Loop
Type the program, save it as P09_A1_trace.asm, assemble, and Step (F7) through it while you watch $t0 (i), $t1 (sum) and $t2 (n).
P09_A1_trace.asm |
Fill one row per pass of the loop (n = 5).
Pass | i at the test | Branch to done taken? | sum after add | i after addi |
|---|---|---|---|---|
1 | ||||
2 | ||||
3 | ||||
4 | ||||
5 | ||||
exit |
A1. When the program ends, what are $t0 and $t1? Why is $t0 not 5?
A2. Look at the Basic column. How many real instructions does one pass of the loop execute? Connect the Instruction Counter and run with n = 5, 10 and 20. Write a formula for the instructions executed.
A3. Predict the count for n = 0, then check it.
Part B - Predict the Output
Predict first, then run.
B1. A do-while countdown
.data |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
B2. Step of 3
.text |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
Part C - Bug Hunt
Each program has ONE bug. Say whether it is an assemble error, a runtime error or a logic error, give the line number, and fix it.
C1. Printing 1 to 5
P09_C1_bug.asm |
What MARS shows |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C2. Sum of 1 to 5
P09_C2_bug.asm |
What MARS shows |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C3. Count to 3
P09_C3_bug.asm |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
Part D - Graded Tasks (10 marks)
Write each program in its own file (P09_T1.asm, P09_T2.asm, P09_T3.asm). Keep the prompts exactly as in the sample runs.
Task 1 [Easy · 3 marks]: Sum of 1 to n
Read n and print the sum 1 + 2 + ... + n using a loop. Test with n = 5, 10, 1, 0 and -3.
Sample run:
MARS Run I/O |
Record: file name of your program, and paste your output here (code goes in the lab book): |
Task 2 [Medium · 3 marks]: Multiplication table (no mul)
Read a number and print its table from 1 to 10 in the form '7 x 3 = 21', one line each. Do NOT use mul: keep a running product and add the number to it on every pass. Test with 7, 3 and 12.
Sample run:
MARS Run I/O |
Record: file name of your program, and paste your output here (code goes in the lab book): |
Task 3 [Challenge · 4 marks]: Loop versus formula: measure the instruction count
Write P09_T3.asm that prints the same sum as Task 1 using the formula n(n+1)/2 with mul and sra, with no loop. Then connect Tools > Instruction Counter and fill the table for BOTH programs:
n | Task 1 (loop) instructions | Task 3 (formula) instructions |
|---|---|---|
10 | ||
100 | ||
1000 |
Then: write a formula IC = a x n + b for Task 1, and say which program you would ship and why. |
Sample run:
MARS Run I/O |
Record: file name of your program, and paste your output here (code goes in the lab book): |
Part E - Post-Lab Questions
E1. The do-while version of the Part A loop (test at the bottom with ble) runs 28 instructions for n = 5, but the while version runs 35. Explain the difference.
E2. Using your Task 1 formula, how many instructions run for n = 1,000,000? How long does that take at CPI 1 on a 1 GHz processor?
E3. Run both Task 1 and Task 3 with n = -3. What does each print, and which is correct?
Marking Rubric
Task | Marks | Full marks | Partial marks | Zero |
|---|---|---|---|---|
T1 | 3 | Correct for all five tests including 0 and -3 | Correct for positive n only (2) | Does not assemble or never stops |
T2 | 3 | 10 lines in the exact format, no mul | Correct numbers but uses mul or wrong format (2) | No attempt |
T3 | 4 | Formula program + complete table + IC formula + conclusion | Program right, table incomplete (2) | No attempt |
Submission Rules
- Save files as P09_T1.asm, P09_T2.asm and P09_T3.asm in a folder named RollNo_P09.
- Record the Part A trace table, the Part A formula and the Task 3 table in the lab book.
- Submit the zipped folder on the LMS by the end of the session; late submission loses 1 mark per day.
- Each student writes his/her own code. Copied code gets zero for both students.