Item | Detail |
|---|---|
Course learning outcomes | CLO6 - low-level software in MIPS assembly (P3); CLO3 - instruction set principles: formats and encoding (C3). |
Tool | MARS 4.5: Edit, Execute (Text Segment: Code and Basic columns), Registers and Data Segment windows, Tools > Instruction Counter |
Linked lectures | Lecture 10 (instruction formats and encoding); Lecture 8 (addressing modes); Practical 2 (pseudo-instructions and the Basic column). |
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:
- Encode MIPS R, I and J format instructions into binary and hex by hand.
- Verify every hand encoding against the Code column of the MARS Text Segment.
- Explain the limits set by field sizes (immediates, shift amounts) and what the assembler does when a constant does not fit.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Machine code | The 32-bit number the CPU really reads | The barcode on a product, not the name on the box |
Field | A group of bits with one job (op, rs, rt, rd, shamt, funct, imm) | One box on a bank deposit slip |
Opcode (op) | First 6 bits: which instruction (or 0 = look at funct) | The form number at the top of the slip |
funct | Last 6 bits of an R-format word: which ALU operation | Tick box: deposit or withdrawal |
Immediate | A 16-bit constant inside the instruction | An amount written directly on the slip |
Sign extension | Copy the top bit to fill 32 bits (keeps negatives negative) | -5 written as -0000005 |
Zero extension | Fill the top 16 bits with zeros | Writing 5 as 0000005 |
Code column | MARS column showing each instruction's machine code in hex | The answer key |
3. Background
3.1 The three formats
Format | Fields (bits) | Used by |
|---|---|---|
R | op 6 | rs 5 | rt 5 | rd 5 | shamt 5 | funct 6 | add, sub, and, or, slt, sll, srl, jr |
I | op 6 | rs 5 | rt 5 | immediate 16 | addi, andi, ori, lw, sw, lui, beq, bne |
J | op 6 | address 26 | j, jal |
Instruction | op / funct | Instruction | op |
|---|---|---|---|
add / addu | 0 / 0x20, 0x21 | addi / addiu | 8 / 9 |
sub | 0 / 0x22 | andi / ori | 12 / 13 |
and / or | 0 / 0x24, 0x25 | lui | 15 |
slt | 0 / 0x2A | lw / sw | 35 / 43 |
sll / srl | 0 / 0x00, 0x02 | beq / bne | 4 / 5 |
jr | 0 / 0x08 | j / jal | 2 / 3 |
Registers: $zero 0, $at 1, $v0-$v1 2-3, $a0-$a3 4-7, $t0-$t7 8-15, $s0-$s7 16-23, $t8-$t9 24-25, $gp 28, $sp 29, $fp 30, $ra 31.
3.2 Four steps to encode
- 1. Find the format and the op (and funct for R).
- 2. Put the registers in BIT order: R = rs, rt, rd (assembly writes rd first!); I = rs, rt (lw/addi destination = rt).
- 3. Write every field in binary with exactly its number of bits (negatives in two's complement).
- 4. Join the 32 bits, group in fours, convert to hex. Branch offset = (target - (PC + 4)) / 4; jump field = target / 4.
Check: Run > Assemble, then look at the Execute tab. The Code column is the machine code; the Basic column shows what a pseudo-instruction became.
4. Time Plan (50 minutes)
Time | Activity | Marks |
|---|---|---|
0-5 | Background 3.1-3.2 (keep the tables open) | - |
5-13 | Part A: encode five instructions and check | - |
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: Encode and Check
A group of four cars and one bus passes the motorway toll plaza (Rs 750 per car, Rs 1500 per bus). Type the program, save it as P11_A1_trace.asm and assemble it. BEFORE you look at the Code column, encode the five instructions in the table by hand. Then compare with MARS and correct any mistakes.
P11_A1_trace.asm |
Fill the table by hand, then check with the Code column.
Instruction | Format | Fields (decimal) | Machine code (hex) |
|---|---|---|---|
addi $t0, $zero, 750 | |||
sll $t1, $t0, 2 | |||
addi $t2, $zero, 1500 | |||
add $t3, $t1, $t2 | |||
sw $t3, 0($s0) |
A1. The source line 'la $s0, total' produced two lines in the Basic column. Write them and explain why.
A2. add and sll both have op = 0. How does the CPU know which one to do?
A3. Run the program. What word does the Data Segment show at 0x10010000? Is it 4500?
Part B - Predict the Output
Predict first, then run. For B2 also predict how many Basic lines each source line becomes.
B1. The same 16 bits, two meanings
.data |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
B2. Just one more
.text |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
Part C - Bug Hunt
Each item has ONE bug. C1 is a mistake in a hand encoding (check it with MARS); C2 and C3 are programs. Say whether it is an encoding (logic) error, an assemble error or a runtime error, give the line number, and fix it.
C1. Hamza's hand encoding
P11_C1_bug.asm |
What MARS shows |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C2. Multiply by 64
P11_C2_bug.asm |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C3. Print my own machine code
P11_C3_bug.asm |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
Part D - Graded Tasks (10 marks)
For each task: type and run the program, save it (P11_T1.asm, P11_T2.asm, P11_T3.asm), then fill the encoding table BY HAND and check every word against the MARS Code column. Write the binary with spaces between the fields.
Task 1 [Easy · 3 marks]: Electricity bill: encode the R-format instructions
A meter read 4520 units last month and 4870 now. The bill is Rs 9 per unit; the program multiplies by 9 using a shift and an add (units x 8 + units).
P11_T1.asm |
Encode these four instructions (the last is a pseudo-instruction - first find what it becomes in the Basic column):
Instruction | Format | Fields (binary) | Hex |
|---|---|---|---|
sub $s2, $s1, $s0 | |||
sll $t0, $s2, 3 | |||
add $s3, $t0, $s2 | |||
move $a0, $s2 |
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]: Committee (BC) collection: encode the I-format instructions
Five members of a committee (BC) pay their monthly share. The loop adds the payments and stores the total after the last one.
P11_T2.asm |
Encode the four instructions below. For bne, first find the addresses of bne and of loop in the Text Segment and compute offset = (target - (PC + 4)) / 4.
Instruction | Fields (decimal) | Hex |
|---|---|---|
lw $t2, 0($s0) | ||
addi $t1, $t1, -1 | ||
bne $t1, $zero, loop | ||
sw $t0, 0($s0) |
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]: Constants that do not fit, and a procedure call
This program uses two constants that do not fit in 16 bits and calls a procedure with jal.
P11_T3.asm |
(a) For each of the first four source lines, PREDICT how many Basic instructions it becomes, then check. (b) Encode jal show (J format) by hand: find the address of show, divide by 4 and build the word. (c) Encode jr $ra. (d) After the run, what is in $ra, and why?
Source line | Basic lines (predict) | Basic lines (MARS) |
|---|---|---|
li $s0, 25000 | ||
li $s1, 70000 | ||
addi $s2, $s0, -40000 | ||
jal show |
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. Part B1 showed that addi and ori treat the same 16 bits differently. Why does MIPS need both sign extension and zero extension?
E2. What does MARS say for 'sll $t1, $t0, 32'? Why is 31 the largest shift amount?
E3. Without running, say which of these need more than ONE Basic instruction: addi $t1, $zero, -32768; addi $t2, $zero, -32769; ori $t3, $zero, 65535; ori $t4, $zero, 65536. Then check in MARS.
Marking Rubric
Task | Marks | Full marks | Partial marks | Zero |
|---|---|---|---|---|
T1 | 3 | Runs; 4 words correct incl. move → addu | Runs; 2-3 words correct (2) | Does not assemble |
T2 | 3 | Runs; 4 words correct incl. bne offset -5 | Runs; offset wrong (2) | No attempt |
T3 | 4 | Counts, jal, jr and $ra all correct | Program + counts only (2) | No attempt |
Submission Rules
- Save files as P11_T1.asm, P11_T2.asm and P11_T3.asm in a folder named RollNo_P11.
- Copy the Part A table and the three task tables (with your binary working) into 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 encodings. Copied work gets zero for both students.