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 (decoding by hand); Practical 11 (encoding); Lecture 9 (branches and jal/jr). |
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:
- Decode a 32-bit MIPS word into its format, fields and assembly instruction.
- Find branch targets and jump targets from their offset and address fields.
- Rebuild a complete program from its machine code, with labels, and prove it is right by matching the MARS Code column.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Decode | Turn machine code back into an instruction | Reading a barcode to find the product name |
Disassembler | A program that decodes machine code into assembly | A barcode scanner at a Utility Store till |
Hex digit | 4 bits written as 0-9, A-F | One group of four on a CNIC number |
Target | The address a branch or jump goes to | The stop where the bus turns |
Offset | Distance to the target in WORDS, counted from PC + 4 | 'Three stops back from the next stop' |
Label | A name for an address; lost in machine code | A shop's name - the GPS only keeps the coordinates |
Stored program | Instructions and data are both just bits in memory | The same page can be a letter or a list |
3. Background
3.1 Decoding recipe
- 1. Write the 8 hex digits as 32 bits (each hex digit = 4 bits).
- 2. The first 6 bits are the opcode: 0 → R format (read funct); 2 → j, 3 → jal (J format); anything else → I format.
- 3. Cut the remaining bits into the fields of that format and convert each to decimal.
- 4. Turn register numbers into names; in I format a 16-bit value with top bit 1 is NEGATIVE (subtract 65536).
- 5. Branch target = (address of branch + 4) + offset x 4. Jump target = address field x 4 (same top 4 bits as PC).
op (decimal) | Instruction | op | Instruction | funct (op 0) | Instruction |
|---|---|---|---|---|---|
0 | R format | 8 / 9 | addi / addiu | 0x20 / 0x22 | add / sub |
2 / 3 | j / jal | 12 / 13 | andi / ori | 0x24 / 0x25 | and / or |
4 / 5 | beq / bne | 15 | lui | 0x2A | slt |
35 / 43 | lw / sw | 10 | slti | 0x00 / 0x02 / 0x08 / 0x0C | sll / srl / jr / syscall |
Hex to binary: 0 0000, 1 0001, 2 0010, 3 0011, 4 0100, 5 0101, 6 0110, 7 0111, 8 1000, 9 1001, A 1010, B 1011, C 1100, D 1101, E 1110, F 1111.
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, $sp 29, $fp 30, $ra 31. The Text Segment starts at 0x00400000 and the Data Segment at 0x10010000.
4. Time Plan (50 minutes)
Time | Activity | Marks |
|---|---|---|
0-5 | Background 3.1 (keep the tables open) | - |
5-13 | Part A: decode eight words | - |
13-17 | Part B: predict the output from machine code | - |
17-23 | Part C: check three decodings | - |
23-46 | Part D: graded tasks 1-3 | 10 |
46-50 | Part E: post-lab questions; save; submit | - |
Part A - Warm-up: Decode Eight Words
A program starts at 0x00400000. Only its machine code is given below. Decode every word by hand (format, fields, instruction). For beq and j, also write the target address.
Machine code (first eight words) |
Fill one row per word.
Address and code | Format | Fields (decimal) | Instruction |
|---|---|---|---|
0x00400000 0x20080028 | |||
0x00400004 0xAFA8FFF8 | |||
0x00400008 0x8FA9FFF8 | |||
0x0040000c 0x00095080 | |||
0x00400010 0x01482022 | |||
0x00400014 0x10800001 | |||
0x00400018 0x08100008 | |||
0x0040001c 0x2004FFFF |
A1. The program continues with 0x2004FFFF, 0x20020001, 0x0000000C, 0x2002000A, 0x0000000C. Decode them. What does the whole program print?
A2. Why is the beq target 0x0040001C and not 0x00400018?
A3. 0x20020001 decodes to 'addi $v0, $zero, 1'. Could the original source line have been 'li $v0, 1'? How do you know?
Part B - Predict the Output
Each program is given ONLY as machine code. Decode it, then predict what it prints. Only then type the decoded program into MARS and run it.
B1. Straight-line code
Address Code |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
B2. A loop hidden in the hex
Address Code |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
Part C - Bug Hunt
Three students decoded words and made ONE mistake each. Find the mistake (C1 and C2: check by assembling the correct instruction in MARS; C3: run Sana's program). Say what kind of error it is and give the correct answer.
C1. Rizwan's load
Decoding to check |
What MARS shows |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C2. Ayesha's branch target
Decoding to check |
What MARS shows |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C3. Sana's rebuilt program
P12_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)
For each task: decode every word, write the program with labels in MARS (P12_T1.asm, P12_T2.asm, P12_T3.asm), assemble it and check that the Code column matches the given machine code WORD FOR WORD. Then run it. A program that prints the right answer but has different machine code is not a correct decoding.
Task 1 [Easy · 3 marks]: A year in hex
This short program does one subtraction and prints the result.
Machine code |
Decode the seven words, rebuild the program and run it. What does the answer mean?
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]: Find the loop
This program contains a loop. Find where it starts from the branch offset, and give that address a label.
Machine code |
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]: Rebuild a program with a procedure
This program reads two words from the Data Segment and calls a procedure. The Data Segment holds:
Data Segment (two words) |
Machine code (Text Segment) |
Decode all 16 words, rebuild the program with a .data section and labels for the procedure and for the beq target, check the Code column, run it, and explain in one sentence what the procedure computes.
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. Your rebuilt programs have labels such as 'loop' and 'bigger'. Could you recover the programmer's real label names and comments from machine code? What else is lost when a program is assembled?
E2. The word 0x01095020 stored with .word and printed as an integer gives a number; decoded as an instruction it is an add. Find both. What does this tell you about memory?
E3. The Instruction Counter shows 43 for Task 2. Explain this number from the program.
Marking Rubric
Task | Marks | Full marks | Partial marks | Zero |
|---|---|---|---|---|
T1 | 3 | All words decoded, Code column matches, 79 explained | Runs but one word differs (2) | No attempt |
T2 | 3 | Correct loop label from offset -8; output and match | Output right, label wrong place (1-2) | No attempt |
T3 | 4 | la, jal and beq targets, match, explanation | Program runs, targets partly wrong (2) | No attempt |
Submission Rules
- Save files as P12_T1.asm, P12_T2.asm and P12_T3.asm in a folder named RollNo_P12.
- Copy the Part A table and your decoding work for every task (binary split into fields) 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 decodes his/her own words. Copied work gets zero for both students.