Nexus Learn

Practical 11: Instruction Encoding: R, I and J Formats

Lab Practical CS-704 Advanced Computer Architecture 11 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: Encode and Check
  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 (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
# Practical 11 - Part A: encode each instruction, then check the Code column
.data
total: .word 0
.text
main:
addi $t0, $zero, 750 # toll for one car (Rs)
sll $t1, $t0, 2 # four cars = 750 x 4
addi $t2, $zero, 1500 # toll for one bus
add $t3, $t1, $t2 # total for the group
la $s0, total # address of total
sw $t3, 0($s0) # store the total
li $v0, 1
move $a0, $t3
syscall
li $v0, 10
syscall

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
sp: .asciiz " "
.text
main:
addi $t0, $zero, -1 # immediate field = 0xFFFF
ori $t1, $zero, 0xFFFF # immediate field = 0xFFFF
li $v0, 1
move $a0, $t0
syscall
li $v0, 4
la $a0, sp
syscall
li $v0, 1
move $a0, $t1
syscall
li $v0, 10
syscall

My prediction (write BEFORE running):

Actual output in MARS / was I right? Why?

B2. Just one more

.text
main:
addi $t0, $zero, 32767
addi $t1, $zero, 32768
add $a0, $t0, $t1
li $v0, 1
syscall
li $v0, 10
syscall

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
# Units sold this week = shop A ($s0) + shop B ($s1)
.text
main:
add $s2, $s0, $s1 # Hamza's hand encoding: 0x02508820

What MARS shows
Code column: 0x02119020 Basic: add $18,$16,$17

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

C2. Multiply by 64

P11_C2_bug.asm
# Multiply the price by 64 with a shift
.text
main:
li $t0, 125 # price
sll $t1, $t0, 64 # price x 64
li $v0, 1
move $a0, $t1
syscall
li $v0, 10
syscall

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

C3. Print my own machine code

P11_C3_bug.asm
# Print the machine code of the first instruction
.text
main:
addi $t0, $zero, -1
la $t1, main # address of the first instruction
lw $a0, 0($t1) # read it as a word
li $v0, 34 # print in hex
syscall
li $v0, 10
syscall

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
# Practical 11 - Task 1: electricity bill with R-format instructions
.data
u: .asciiz "Units = "
b: .asciiz "\nBill = Rs "
.text
main:
li $s0, 4520 # previous meter reading
li $s1, 4870 # current meter reading
sub $s2, $s1, $s0 # units = current - previous
sll $t0, $s2, 3 # units x 8
add $s3, $t0, $s2 # units x 9 (Rs 9 per unit)
li $v0, 4
la $a0, u
syscall
li $v0, 1
move $a0, $s2 # units
syscall
li $v0, 4
la $a0, b
syscall
li $v0, 1
move $a0, $s3 # bill
syscall
li $v0, 10
syscall

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
Units = 350
Bill = Rs 3150
-- 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]: 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
# Practical 11 - Task 2: committee (BC) collection with I-format instructions
.data
pay: .word 5000, 5000, 4500, 5000, 5000
total: .word 0
msg: .asciiz "Collected = Rs "
.text
main:
la $s0, pay # pointer to the first payment
li $t0, 0 # sum = 0
li $t1, 5 # members left
loop:
lw $t2, 0($s0) # this member's payment
add $t0, $t0, $t2 # sum = sum + payment
addi $s0, $s0, 4 # next member
addi $t1, $t1, -1 # one member fewer
bne $t1, $zero, loop # repeat while members are left
sw $t0, 0($s0) # $s0 now points at total
li $v0, 4
la $a0, msg
syscall
li $v0, 1
move $a0, $t0
syscall
li $v0, 10
syscall

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
Collected = Rs 24500
-- 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]: 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
# Practical 11 - Task 3: constants that do and do not fit, and a procedure call
.text
main:
li $s0, 25000 # fits in 16 bits
li $s1, 70000 # does not fit in 16 bits
addi $s2, $s0, -40000 # constant does not fit either
jal show # call the procedure (J format)
li $v0, 10
syscall
show:
add $a0, $s1, $s2 # 70000 + (25000 - 40000)
li $v0, 1
syscall
jr $ra # return (R format)

(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
55000
-- 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. 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.

← All CS-704 material