Item | Detail |
|---|---|
Course learning outcomes | CLO6 - low-level software in MIPS assembly (P3); CLO3 - instruction set principles: memory addressing (C2). |
Tool | MARS 4.5: Execute pane - Data Segment window (Hexadecimal Values on/off) and Registers window |
Linked lectures | Lecture 5 and 6 (memory and locality); prepares Lecture 8 (memory addressing). Follows Practical 4. |
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:
- Declare an array of words in the .data section and get its base address with la.
- Use lw and sw with base + displacement addressing to read and change array elements.
- Calculate the byte address of element i of a word array (base + 4 x i) and check it in the Data Segment.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Memory address | Number of a byte in memory | House number on a street |
Word | 4 bytes (32 bits) kept together | A row of 4 seats in a bus |
Array | Many values of the same kind stored one after another | Roll-number list of a class |
Base address | Address of the first element of an array | The first house of a street |
Displacement (offset) | How many bytes after the base | 'Third house from the corner' |
Load / store | Copy memory → register / register → memory | Taking a file out of a cupboard / putting it back |
3. Background
3.1 Memory is an array of bytes
Every byte in memory has an address. A word is 4 bytes, so consecutive words of an array are 4 addresses apart. In MARS the .data section starts at address 0x10010000.
Declaration | Element | Address | Value |
|---|---|---|---|
runs: .word 45, 12, 78, 30, 6 | runs[0] | 0x10010000 | 45 |
runs[1] | 0x10010004 | 12 | |
runs[2] | 0x10010008 | 78 | |
runs[4] | 0x10010010 | 6 | |
total: .word 0 | total | 0x10010014 | 0 |
3.2 Load, store and la
Base + displacement addressing |
Remember: MIPS is a load-store machine (Lecture 7): arithmetic works only on registers, so data must be LOADED from memory, changed in a register, then STORED back. |
To see memory, use the Data Segment window in the Execute pane. Untick 'Hexadecimal Values' to read numbers in decimal. Each row shows 8 words; the column headings (Value (+0), Value (+4), …) give the offset from the row address.
4. Time Plan (50 minutes)
Time | Activity | Marks |
|---|---|---|
0-6 | Background 3.1-3.2; open the Data Segment window | - |
6-14 | Part A: trace loads and stores | - |
14-18 | Part B: predict the output | - |
18-23 | Part C: bug hunt | - |
23-45 | Part D: graded tasks 1-3 | 10 |
45-50 | Part E: post-lab questions; save; submit | - |
Part A - Warm-up: Trace Loads and Stores
Type the program, save it as P05_A1_trace.asm, assemble it and run it one step at a time (F7). After each lw or sw, write the address used and the value that moved. Watch the Registers window and the Data Segment.
P05_A1_trace.asm |
Fill in the table (addresses in hexadecimal, values in decimal).
Instruction | Address used ($t0 + offset) | Value moved | Where it goes |
|---|---|---|---|
lw $t1, 0($t0) | |||
lw $t2, 4($t0) | |||
lw $t3, 8($t0) | |||
sw $t4, 20($t0) | |||
sw $t5, 4($t0) |
A1. After the program finishes, write the six words of the Data Segment from 0x10010000 to 0x10010014 in decimal.
A2. Why is the offset of total 20 and not 5?
A3. 'la $t0, runs' is a pseudo-instruction. What basic instructions did MARS make (see the Basic column)?
Part B - Predict the Output
Predict the output on paper first. Then run the program to check.
B1. Which element?
.data |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
B2. A moving base
.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. Second mark (1)
P05_C1_bug.asm |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C2. Second mark (2)
P05_C2_bug.asm |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C3. Third mark
P05_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 (P05_T1.asm, P05_T2.asm, P05_T3.asm). Use la once to get the base address, then lw/sw with offsets. No loops yet - loops come in Practical 9.
Task 1 [Easy · 3 marks]: Weekly rainfall
Store the rainfall of one week in Faisalabad (mm) as an array: 0, 12, 5, 0, 20, 8, 3. Add all seven values, STORE the total in a word variable called total, then load total back from memory and print it. Check total in the Data Segment.
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]: Change a bus fare
The array fares holds 30, 50, 80, 120 (rupees). Read an increase from the keyboard, add it to the THIRD fare, store it back in the array, then print the old fare and the new fare. The new fare must be loaded again from memory, to prove the store worked.
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]: Swap the first and last scores
runs holds 45, 12, 78, 30, 6. Swap the FIRST and the LAST elements in memory (use two lw and two sw). Then load both elements again from memory and print them. In your lab book, write the address of each element you used.
Hint: the last element of a 5-word array is index 4, so its offset is 4 x 4 = 16. |
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. Why must the offset of the next word be 4 more than the previous one?
E2. Why does MIPS need lw and sw at all - why can't add use memory directly?
E3. Bug C3 printed 3 without any error. Why is this kind of bug dangerous?
Marking Rubric
Task | Marks | Full marks | Partial marks | Zero |
|---|---|---|---|---|
T1 | 3 | Seven loads, total stored and re-loaded, output 48 | Total printed but not stored (1-2) | Does not assemble |
T2 | 3 | Correct element changed in memory and re-loaded | Prints right values without sw (1-2) | No attempt |
T3 | 4 | Swap in memory, re-loaded, addresses recorded | Swap only in registers (2) | No attempt |
Submission Rules
- Save files as P05_T1.asm, P05_T2.asm and P05_T3.asm in a folder named RollNo_P05.
- In the lab book, write the Data Segment values you saw after each task.
- 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.