Nexus Learn

Practical 9: Loops: Counting and Summing

Lab Practical CS-704 Advanced Computer Architecture 9 min read Updated 8 Nov 2026
On this page
  1. 1. Objectives
  2. 2. Word Bank
  3. 3. Background
  4. 4. Time Plan (50 minutes)
  5. Part A - Warm-up: Trace a Counting Loop
  6. Part B - Predict the Output
  7. Part C - Bug Hunt
  8. Part D - Graded Tasks (10 marks)
  9. Part E - Post-Lab Questions
  10. Marking Rubric
  11. Submission Rules

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
C: sum = 0;
for (i = 1; i <= n; i++) sum = sum + i;

MIPS: li $t0, 1 # i = 1 (initialise)
li $t1, 0 # sum = 0
loop: bgt $t0, $t2, done # leave when i > n (inverted test)
add $t1, $t1, $t0 # body
addi $t0, $t0, 1 # i++ (update)
j loop # go back
done:

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
loop: add $t1, $t1, $t0 # body runs first
addi $t0, $t0, 1
ble $t0, $t2, loop # repeat while i <= n

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
# Practical 09 - Part A: trace a counting loop (sum of 1..5)
.text
main:
li $t0, 1 # i = 1
li $t1, 0 # sum = 0
li $t2, 5 # n = 5
loop:
bgt $t0, $t2, done # leave the loop when i > n
add $t1, $t1, $t0 # sum = sum + i
addi $t0, $t0, 1 # i = i + 1
j loop # go back and test again
done:
li $v0, 1 # print sum
move $a0, $t1
syscall
li $v0, 10
syscall

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
sp: .asciiz " "
go: .asciiz "Go!"
.text
main:
li $t0, 3
loop:
li $v0, 1
move $a0, $t0
syscall
li $v0, 4
la $a0, sp
syscall
addi $t0, $t0, -1
bnez $t0, loop # test at the BOTTOM
li $v0, 4
la $a0, go
syscall
li $v0, 10
syscall

My prediction (write BEFORE running):

Actual output in MARS / was I right? Why?

B2. Step of 3

.text
main:
li $t0, 0
loop:
addi $t0, $t0, 3
blt $t0, 10, loop
li $v0, 1
move $a0, $t0
syscall
li $v0, 10
syscall

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
# Should print 1 2 3 4 5
.data
sp: .asciiz " "
.text
main:
li $t0, 1 # i = 1
li $t2, 5 # n = 5
loop:
bgt $t0, $t2, done
li $v0, 1
move $a0, $t0
syscall
li $v0, 4
la $a0, sp
syscall
j loop
done:
li $v0, 10
syscall

What MARS shows
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
(keeps printing 1 and never finishes - press Stop)

Bug type (assemble error / runtime error / logic error), line number, and your fix:

C2. Sum of 1 to 5

P09_C2_bug.asm
# Sum of 1..n for n = 5. Should print 15
.text
main:
li $t0, 1 # i
li $t1, 0 # sum
li $t2, 5 # n
loop:
add $t1, $t1, $t0
addi $t0, $t0, 1
blt $t0, $t2, loop # repeat while i < n ?
li $v0, 1
move $a0, $t1
syscall
li $v0, 10
syscall

What MARS shows
10
-- program is finished running --

Bug type (assemble error / runtime error / logic error), line number, and your fix:

C3. Count to 3

P09_C3_bug.asm
# Count from 1 to 3 and print the last value (3)
.text
main:
li $t0, 0
loop:
adi $t0, $t0, 1
blt $t0, 3, loop
li $v0, 1
move $a0, $t0
syscall
li $v0, 10
syscall

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
Enter n: 5
Sum = 15
-- program is finished running --

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
Enter a number: 7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

-- program is finished running --

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
Enter n: 5
Sum = 15
-- program is finished running --

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.

← All CS-704 material