Nexus Learn

Practical 12: Decoding Machine Code

Lab Practical CS-704 Advanced Computer Architecture 8 min read Updated 8 Nov 2026
On this page
  1. 1. Objectives
  2. 2. Word Bank
  3. 3. Background
  4. 4. Time Plan (50 minutes)
  5. Part A - Warm-up: Decode Eight Words
  6. Part B - Predict the Output
  7. Part C - Bug Hunt
  8. Part D - Graded Tasks (10 marks)
  9. Part E - Post-Lab Questions
  10. Marking Rubric
  11. Submission Rules

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)
Address Code
0x00400000 0x20080028
0x00400004 0xAFA8FFF8
0x00400008 0x8FA9FFF8
0x0040000c 0x00095080
0x00400010 0x01482022
0x00400014 0x10800001
0x00400018 0x08100008
0x0040001c 0x2004FFFF

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
0x00400000 0x20080007
0x00400004 0x20090003
0x00400008 0x0128502A
0x0040000c 0x000858C0
0x00400010 0x016A2020
0x00400014 0x20020001
0x00400018 0x0000000C
0x0040001c 0x2002000A
0x00400020 0x0000000C

My prediction (write BEFORE running):

Actual output in MARS / was I right? Why?

B2. A loop hidden in the hex

Address Code
0x00400000 0x20080004
0x00400004 0x20040000
0x00400008 0x00882020
0x0040000c 0x2108FFFF
0x00400010 0x1500FFFD
0x00400014 0x20020001
0x00400018 0x0000000C
0x0040001c 0x2002000A
0x00400020 0x0000000C

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
Word at 0x00400004: 0x8FA80004
Rizwan's decoding: lw $sp, 4($t0)

What MARS shows
MARS, after assembling 'lw $t0, 4($sp)' at 0x00400004:
Code 0x8FA80004 Basic lw $t0, 4($sp)

Bug type (assemble error / runtime error / logic error), line number, and your fix:

C2. Ayesha's branch target

Decoding to check
Word at 0x00400010: 0x1509FFFD
Ayesha's decoding: bne $t0, $t1, back to 0x00400004

What MARS shows
MARS, after assembling a bne at 0x00400010 to a label at 0x00400008:
Code 0x1509FFFD Basic bne $t0, $t1, -3

Bug type (assemble error / runtime error / logic error), line number, and your fix:

C3. Sana's rebuilt program

P12_C3_bug.asm
# Rebuilt by Sana from 0x20090032 and 0x2128FFF6, then print $t0
.text
main:
addi $t1, $zero, 50
addi $t0, $t1, 65526 # decoded from 0x2128FFF6
li $v0, 1
move $a0, $t0
syscall
li $v0, 10
syscall

What MARS shows
65576
-- program is finished running --

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
Address Code
0x00400000 0x200807EA
0x00400004 0x2009079B
0x00400008 0x01092022
0x0040000c 0x20020001
0x00400010 0x0000000C
0x00400014 0x2002000A
0x00400018 0x0000000C

Decode the seven words, rebuild the program and run it. What does the answer mean?

Sample run:

MARS Run I/O
79
-- program is finished running --

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
Address Code
0x00400000 0x20080005
0x00400004 0x20020001
0x00400008 0x01002020
0x0040000c 0x0000000C
0x00400010 0x2002000B
0x00400014 0x20040020
0x00400018 0x0000000C
0x0040001c 0x2108FFFF
0x00400020 0x1500FFF8
0x00400024 0x2002000A
0x00400028 0x0000000C

Sample run:

MARS Run I/O
5 4 3 2 1
-- program is finished running --

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)
0x10010000 0x00000029 0x0000002E

Machine code (Text Segment)
Address Code
0x00400000 0x3C011001
0x00400004 0x34300000
0x00400008 0x8E040000
0x0040000c 0x8E050004
0x00400010 0x0C10000A
0x00400014 0x00402020
0x00400018 0x20020001
0x0040001c 0x0000000C
0x00400020 0x2002000A
0x00400024 0x0000000C
0x00400028 0x0085402A
0x0040002c 0x11000002
0x00400030 0x00A01020
0x00400034 0x03E00008
0x00400038 0x00801020
0x0040003c 0x03E00008

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
46
-- program is finished running --

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.

← All CS-704 material