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) |
3.2 Pointer loop: move the address itself
pointer style |
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 |
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 |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
B2. Every second element
.data |
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 |
What MARS shows |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C2. Sum with a pointer
P10_C2_bug.asm |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C3. Sum using the stored count
P10_C3_bug.asm |
What MARS shows |
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 |
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 |
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 |
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 |
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.