Nexus Learn

Practical 10: Array Processing with Loops

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 Pointer 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 - compare designs by instruction count (C3).

Tool

MARS 4.5: Edit, Execute (Run > Step), Registers and Data Segment windows, Tools > Instruction Counter

Linked lectures

Practical 5 (lw, la, base + displacement); Practical 9 (loops); Lecture 8 (addressing modes); Lecture 9 (control flow).

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:

  • Process every element of an array with a loop: sum, average, maximum and search.
  • Write the same loop in two styles - index (i x 4 + base) and pointer (p = p + 4) - and explain the difference.
  • Compare the two styles by measured instruction count and relate the result to addressing modes.

2. Word Bank

Word

Simple meaning

Real-world example

Array

Values of the same type stored one after another

Seats in one row of a Daewoo bus

Index (i)

The position number of an element, starting at 0

Seat number in the row

Offset

Distance in bytes from the start: i x 4 for words

Steps from the first seat

Pointer (p)

A register holding the address of the current element

Your finger on the current seat

End address

The address just after the last element

The door after the last seat

Linear search

Check elements one by one until the key is found

Looking for a roll number in a printed list

Worst case

The input that needs the most work

The roll number is last - or not in the list

3. Background

3.1 Index loop: compute the address every pass

index style ($s0 = base, $s1 = n)
li $t0, 0 # i = 0
loop: beq $t0, $s1, done # i = n: finished
sll $t2, $t0, 2 # offset = i x 4
add $t2, $s0, $t2 # address = base + offset
lw $t3, 0($t2) # a[i]
...use $t3...
addi $t0, $t0, 1 # i++
j loop

3.2 Pointer loop: move the address itself

pointer style
move $t0, $s0 # p = base
addi $t3, $s0, 20 # end = base + n x 4 (n = 5)
loop: beq $t0, $t3, done # p = end: finished
lw $t2, 0($t0) # value at p
...use $t2...
addi $t0, $t0, 4 # p = p + 4
j loop

The pointer loop does not need sll and add every pass, because p already holds the address. This is the autoincrement mode of Lecture 8 built from two MIPS instructions (lw, then addi 4). Compilers make exactly this change (strength reduction, Lecture 11).

3.3 Two rules for arrays of words

  • Step 4: the next word is 4 bytes later. Adding 1 gives a misaligned address and a runtime error (Practical 5).
  • Exact end: stop at i = n (or p = base + 4n). Using i <= n reads one word past the array.

4. Time Plan (50 minutes)

Time

Activity

Marks

0-6

Background 3.1-3.3

-

6-13

Part A: trace a pointer loop

-

13-17

Part B: predict the output

-

17-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 Pointer Loop

Type the program, save it as P10_A1_trace.asm and Step (F7) through it. Watch $t0 (p), $t3 (end) and $t1 (sum). The array starts at 0x10010000 (see the Data Segment).

P10_A1_trace.asm
# Practical 10 - Part A: walk an array with a pointer
.data
marks: .word 72, 85, 64, 90, 58
.text
main:
la $t0, marks # p = address of marks[0]
addi $t3, $t0, 20 # end = p + 5 words x 4 bytes
li $t1, 0 # sum = 0
loop:
beq $t0, $t3, done # stop when p reaches end
lw $t2, 0($t0) # value = word at p
add $t1, $t1, $t2 # sum = sum + value
addi $t0, $t0, 4 # p = p + 4 (next word)
j loop
done:
li $v0, 1
move $a0, $t1
syscall
li $v0, 10
syscall

Fill one row per pass of the loop.

Pass

p ($t0) in hex

beq taken?

value ($t2)

sum ($t1)

1

2

3

4

5

exit

A1. Why is end = p + 20 and not p + 5?

A2. How many real instructions run per pass? Check the total with the Instruction Counter.

A3. At the end, $t0 = $t3. What is the value, and why does the loop stop exactly there?

Part B - Predict the Output

Predict first, then run.

B1. Count with a condition

.data
marks: .word 72, 85, 64, 90, 58
.text
main:
la $t0, marks
li $t1, 0 # i
li $t4, 0 # count
loop:
bge $t1, 5, done
sll $t2, $t1, 2 # i x 4
add $t2, $t0, $t2
lw $t3, 0($t2) # marks[i]
blt $t3, 80, next
addi $t4, $t4, 1
next:
addi $t1, $t1, 1
j loop
done:
li $v0, 1
move $a0, $t4
syscall
li $v0, 10
syscall

My prediction (write BEFORE running):

Actual output in MARS / was I right? Why?

B2. Every second element

.data
marks: .word 72, 85, 64, 90, 58
.text
main:
la $t0, marks
addi $t3, $t0, 20 # end of the array
li $t1, 0
loop:
lw $t2, 0($t0)
add $t1, $t1, $t2
addi $t0, $t0, 8 # jump two words
blt $t0, $t3, loop
li $v0, 1
move $a0, $t1
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. Highest temperature in Murree

P10_C1_bug.asm
# Highest temperature in Murree (all below zero). Should print -1
.data
temp: .word -3, -7, -1, -5
.text
main:
la $t0, temp
li $t1, 0 # i
li $t5, 0 # max = 0
loop:
bge $t1, 4, done
sll $t2, $t1, 2
add $t2, $t0, $t2
lw $t3, 0($t2) # temp[i]
ble $t3, $t5, next # not bigger: skip
move $t5, $t3 # new max
next:
addi $t1, $t1, 1
j loop
done:
li $v0, 1
move $a0, $t5
syscall
li $v0, 10
syscall

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

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

C2. Sum with a pointer

P10_C2_bug.asm
# Sum of 5 marks with a pointer. Should print 369
.data
marks: .word 72, 85, 64, 90, 58
.text
main:
la $t0, marks
addi $t3, $t0, 20
li $t1, 0
loop:
beq $t0, $t3, done
lw $t2, 0($t0)
add $t1, $t1, $t2
addi $t0, $t0, 1 # next element
j loop
done:
li $v0, 1
move $a0, $t1
syscall
li $v0, 10
syscall

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

C3. Sum using the stored count

P10_C3_bug.asm
# Sum of the marks using the count stored in memory. Should print 369
.data
marks: .word 72, 85, 64, 90, 58
count: .word 5
.text
main:
la $t0, marks
lw $t4, count # number of elements
li $t1, 0 # i
li $t5, 0 # sum
loop:
bgt $t1, $t4, done # stop when i > count
sll $t2, $t1, 2
add $t2, $t0, $t2
lw $t3, 0($t2)
add $t5, $t5, $t3
addi $t1, $t1, 1
j loop
done:
li $v0, 1
move $a0, $t5
syscall
li $v0, 10
syscall

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

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 (P10_T1.asm, P10_T2.asm, P10_T3_index.asm, P10_T3_ptr.asm). Keep the data and messages exactly as given.

Task 1 [Easy · 3 marks]: Total and average of six electricity bills

A family's last six bills (Rs) are stored as:

bills: .word 3250, 4100, 5800, 7200, 6900, 4500
n: .word 6

Use a loop to find the total, then print the total and the average in whole rupees (div then mflo). Your loop must use n from memory, so it still works if the data changes.

Sample run:

MARS Run I/O
Total = Rs 31750
Average = Rs 5291
-- 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]: Hottest day of the week

Lahore's maximum temperatures for one week are:

temp: .word 41, 43, 45, 46, 44, 46, 42

Print the highest temperature and the day it happened (day 1 to 7). If the highest value appears twice, report the EARLIER day.

Sample run:

MARS Run I/O
Highest = 46 on day 4
-- 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]: Search a roll number: index versus pointer

The roll numbers of a lab group are stored as:

rolls: .word 1101, 1107, 1112, 1120, 1125, 1133, 1140, 1148

Read a roll number and print 'Found at position k' (k = 1 to 8) or 'Not found'. Write it TWICE: P10_T3_index.asm uses an index (sll + add + lw) and P10_T3_ptr.asm uses a pointer (lw, then addi 4). Then fill the table with the Instruction Counter:

Roll number

Position

Index version

Pointer version

1101

1

1120

4

1148

8

1111

not found

Sample run:

MARS Run I/O
Roll number to find: 1120
Found at position 4
-- 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. Count the instructions in ONE pass of the search loop in each version. Which instructions does the pointer version save, and which addressing mode from Lecture 8 is it imitating?

E2. In Part B2, a student replaces 'blt $t0, $t3, loop' with 'bne $t0, $t3, loop'. What happens and why?

E3. If every roll number is equally likely to be searched for, what is the average instruction count for a successful search in each version? What would the counts be for a list of 800 students?

Marking Rubric

Task

Marks

Full marks

Partial marks

Zero

T1

3

Loop uses n from memory; total and average correct

Total right, average wrong or n hard-coded (2)

Does not assemble

T2

3

Highest and earlier day correct, including ties

Highest right, day wrong on ties (2)

No attempt

T3

4

Both versions correct + table + explanation

One version correct and table (2-3)

No attempt

Submission Rules

  • Save files as P10_T1.asm, P10_T2.asm, P10_T3_index.asm and P10_T3_ptr.asm in a folder named RollNo_P10.
  • Record the Part A trace table and the Task 3 instruction-count 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