Item | Detail |
|---|---|
Course learning outcomes | CLO6 - low-level software in MIPS assembly (P3); CLO3 - instruction set principles: memory addressing and data types (C2). |
Tool | MARS 4.5: Data Segment window (Hexadecimal Values and ASCII views), Registers window |
Linked lectures | Practical 5 (lw, sw, arrays); prepares Lecture 8 (byte addressing, alignment, big- vs little-endian). |
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:
- Use lb, lbu and sb to read and write single bytes, and explain sign and zero extension.
- Observe little-endian byte order of a word in the MARS Data Segment.
- Calculate the address of element i of an array (base + i x 4 for words, base + i for bytes) using sll.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Byte | 8 bits; the smallest unit with its own address | One seat in a bus (a word is a row of 4) |
Endianness | The order in which the bytes of a word are stored | Writing a date as day-month-year or year-month-day |
Little-endian | Least significant byte at the lowest address | Counting rupees starting from the smallest notes |
Sign extension | Copy the sign bit into the upper bits | A negative balance stays negative when written in a bigger box |
Zero extension | Fill the upper bits with 0 | Writing 7 as 007 on a form |
ASCII | Number code for each character ('A' = 65, 'a' = 97) | Roll number given to each letter |
Index | Position of an element, starting at 0 | Seat number in a row |
3. Background
3.1 Byte instructions
Instruction | What it does | Upper 24 bits of the register |
|---|---|---|
lb $t1, 0($t0) | Load one byte | Copies of the byte's sign bit (sign extension) |
lbu $t1, 0($t0) | Load one byte, unsigned | Zeros (zero extension) |
sb $t1, 0($t0) | Store the lowest byte of $t1 | - (only 1 byte of memory changes) |
A byte 0xF0 is 240 as an unsigned number but -16 as a signed number. lb gives -16; lbu gives 240. Use lbu for characters and for values 0-255 such as pixel brightness.
3.2 Endianness
A word has 4 bytes but 4 addresses. Which byte goes at the lowest address? MIPS can work either way; MARS (like x86 and most ARM phones) is little-endian: the least significant byte is at the lowest address.
Address | w + 0 | w + 1 | w + 2 | w + 3 |
|---|---|---|---|---|
Little-endian (MARS) | 0x44 | 0x33 | 0x22 | 0x11 |
Big-endian | 0x11 | 0x22 | 0x33 | 0x44 |
Careful: the Data Segment window prints each WORD as a number (0x11223344), so it does not show the byte order directly. Load single bytes with lbu to see which byte sits at which address. |
3.3 Address of element i
Indexed access |
sll $t2, $t1, 2 shifts left by 2 bits, which multiplies by 4 (Practical 3 used masks; shifts work on the same bits).
4. Time Plan (50 minutes)
Time | Activity | Marks |
|---|---|---|
0-6 | Background 3.1-3.3 | - |
6-14 | Part A: bytes inside a word | - |
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: The Bytes Inside a Word
Type the program, save it as P06_A1_trace.asm, assemble and run it. Then fill in the table from the Registers window (hexadecimal view).
P06_A1_trace.asm |
Fill in the address read by each lbu and the value it loaded (hexadecimal, last two digits are enough).
Register | Address read | Value (hex) | Meaning |
|---|---|---|---|
$t1 | |||
$t2 | |||
$t3 | |||
$t4 | |||
$t6 | |||
$t7 |
A1. Which byte of 0x11223344 is stored at the lowest address? Is MARS little-endian or big-endian?
A2. The Data Segment shows the word at 0x10010004 as 0x00464155 although the string is "UAF". Explain.
A3. What would $t1 hold on a big-endian machine?
Part B - Predict the Output
Predict first, then run. Also look at the Mars Messages tab after assembling B1.
B1. lb or lbu?
.data |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
B2. One byte into a word
.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. Indexed access
P06_C1_bug.asm |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C2. Pixel brightness
P06_C2_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 (P06_T1.asm, P06_T2.asm, P06_T3.asm). Run each one and check the Data Segment.
Task 1 [Easy · 3 marks]: Fare by stop number
fares holds 30, 50, 80, 120, 150 rupees for stops 0-4. Read a stop number i and print fares[i]. Calculate the address with sll and add (base + i x 4) - do not use a list of branches.
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]: Capitalise a city name
Read a city name in small letters with syscall 8 (read string; $a0 = buffer, $a1 = size). Change the first letter to a capital by subtracting 32 from its ASCII code, store it back with sb, then print the code and the whole name.
Hint: 'f' = 102, 'F' = 70; 'a' - 'A' = 97 - 65 = 32. The buffer also holds the Enter key (newline), so the name prints with a line break after it. |
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]: Endianness detective
Read an integer, store it as a word in memory, then load its four bytes with lbu from offsets 0, 1, 2 and 3 and print them in that order. Test with 168496141 (= 0x0A0B0C0D). In your lab book, explain what the output proves about MARS.
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. When should you use lbu instead of lb? Give two examples.
E2. Why is the address of element i of a word array base + 4i, but of a string base + i?
E3. A file written on a big-endian machine holds the word 0x00000102. A little-endian machine reads the same 4 bytes as a word. What value does it get?
Marking Rubric
Task | Marks | Full marks | Partial marks | Zero |
|---|---|---|---|---|
T1 | 3 | Index scaled with sll, correct fare | Correct for index 0 only (1) | Does not assemble |
T2 | 3 | lbu, -32, sb back, both outputs | Prints capital letter but string unchanged (2) | No attempt |
T3 | 4 | Four bytes in order and correct explanation | Bytes printed without explanation (3) | No attempt |
Submission Rules
- Save files as P06_T1.asm, P06_T2.asm and P06_T3.asm in a folder named RollNo_P06.
- Record the Task 3 output and your explanation 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.