This homework covers R-Type, I-Type, and J-Type MIPS instructions. For each instruction, (1) identify the format, (2) show the binary encoding, and (3) give the hex machine code.


Reference: Instruction formats

Format Layout Field sizes
R-type op | rs | rt | rd | shamt | funct 6 | 5 | 5 | 5 | 5 | 6
I-type op | rs | rt | immediate 6 | 5 | 5 | 16
J-type op | target 6 | 26

Register numbers (5-bit)

Register Number Binary
$zero 0 00000
$t0 8 01000
$t1 9 01001
$t2 10 01010
$s0 16 10000
$s1 17 10001
$ra 31 11111

Opcode / funct (selected)

Instruction op (6) funct (6)
add 000000 100000
sub 000000 100010
and 000000 100100
or 000000 100101
slt 000000 101010
jr 000000 001000
sll 000000 000000
srl 000000 000010
sra 000000 000011
addi 001000
lw 100011
sw 101011
beq 000100
bne 000101
j 000010
jal 000011

Part 1: R-Type instructions

1.1 Encode: add $t2, $t0, $t1
(R-type: rd = $t2, rs = $t0, rt = $t1)

1.2 Encode: sub $s0, $s1, $t0
(R-type: rd = $s0, rs = $s1, rt = $t0)

1.3 Encode: and $t1, $t0, $t2
(R-type: rd = $t1, rs = $t0, rt = $t2)

1.4 Encode: jr $ra
(R-type: jr → rs = $ra, rt = 0, rd = 0, shamt = 0)

1.5 Encode: sll $t1, $t0, 4
(R-type: shift left logical; rd = $t1, rt = $t0, shamt = 4; rs = 0 unused)

1.6 Encode: srl $t2, $t1, 2
(R-type: shift right logical; rd = $t2, rt = $t1, shamt = 2)

1.7 Encode: sra $s0, $t0, 3
(R-type: shift right arithmetic; rd = $s0, rt = $t0, shamt = 3)


Part 2: I-Type instructions

2.1 Encode: addi $t1, $t0, 100
(I-type: rt = $t1, rs = $t0, immediate = 100)

2.2 Encode: addi $t0, $t0, -16
(I-type: immediate = -16 in 16-bit two’s complement)

2.3 Encode: lw $t1, 8($t0)
(I-type: rt = $t1, rs = $t0, offset = 8)

2.4 Encode: sw $t2, 12($t0)
(I-type: rt = $t2, rs = $t0, offset = 12)

2.5 Encode: beq $t0, $t1, 4
(I-type: branch offset = 4 words; rs = $t0, rt = $t1, immediate = 4)


Part 3: J-Type instructions

3.1 Encode: j 0x00400020
(J-type: op = 2 for j. The 26-bit field = (target address) >> 2.
Given address 0x00400020, compute the 26-bit target.)

3.2 Encode: jal 0x00401000
(J-type: op = 3 for jal. 26-bit field = (0x00401000) >> 2.)


Part 4: Mixed (identify format and encode)

4.1 What format is slt $t0, $s0, $s1? Encode it.
(slt: op = 000000, funct = 101010; rd = $t0, rs = $s0, rt = $s1)

4.2 What format is bne $t0, $zero, -8? Encode it.
(Immediate -8 in 16-bit two’s complement; $zero = 0.)


Solutions


Part 1: R-Type — Solutions

1.1 add $t2, $t0, $t1

  • Format: R-type
  • Fields: op=000000, rs=$t0=01000, rt=$t1=01001, rd=$t2=01010, shamt=00000, funct=100000

Binary:

000000  01000  01001  01010  00000  100000
  op     rs     rt     rd   shamt   funct

Hex: 0x01095020

(Grouped: 0000 0001 0000 1001 0101 0000 0010 0000 → 0x01095020)


1.2 sub $s0, $s1, $t0

  • Format: R-type
  • Fields: op=000000, rs=$s1=10001, rt=$t0=01000, rd=$s0=10000, shamt=00000, funct=100010

Binary:

000000  10001  01000  10000  00000  100010
  op     rs     rt     rd   shamt   funct

Hex: 0x02288022


1.3 and $t1, $t0, $t2

  • Format: R-type
  • Fields: op=000000, rs=$t0=01000, rt=$t2=01010, rd=$t1=01001, shamt=00000, funct=100100

Binary:

000000  01000  01010  01001  00000  100100
  op     rs     rt     rd   shamt   funct

Hex: 0x010A4824


1.4 jr $ra

  • Format: R-type
  • Fields: op=000000, rs=$ra=11111, rt=00000, rd=00000, shamt=00000, funct=001000

Binary:

000000  11111  00000  00000  00000  001000
  op     rs     rt     rd   shamt   funct

Hex: 0x03E00008


1.5 sll $t1, $t0, 4

  • Format: R-type
  • Meaning: Shift left logical — $t1 = $t0 << 4 (multiply by 16).
  • Fields: op=000000, rs=00000 (unused for sll), rt=$t0=01000 (source), rd=$t1=01001 (destination), shamt=00100 (4), funct=000000

Binary:

000000  00000  01000  01001  00100  000000
  op     rs     rt     rd   shamt   funct

Hex: 0x00084800


1.6 srl $t2, $t1, 2

  • Format: R-type
  • Meaning: Shift right logical — $t2 = $t1 >> 2 (unsigned divide by 4).
  • Fields: op=000000, rs=00000 (unused), rt=$t1=01001 (source), rd=$t2=01010 (destination), shamt=00010 (2), funct=000010

Binary:

000000  00000  01001  01010  00010  000010
  op     rs     rt     rd   shamt   funct

Hex: 0x00095082


1.7 sra $s0, $t0, 3

  • Format: R-type
  • Meaning: Shift right arithmetic — $s0 = $t0 >> 3 (signed divide by 8; sign bit extended).
  • Fields: op=000000, rs=00000 (unused), rt=$t0=01000 (source), rd=$s0=10000 (destination), shamt=00011 (3), funct=000011

Binary:

000000  00000  01000  10000  00011  000011
  op     rs     rt     rd   shamt   funct

Hex: 0x00088003


2.1 addi $t1, $t0, 100

  • Format: I-type
  • Fields: op=001000 (addi), rs=$t0=01000, rt=$t1=01001, immediate=100

100 decimal = 0x0064 → 0000 0000 0110 0100

Binary:

001000  01000  01001  0000000001100100
  op     rs     rt         immediate

Hex: 0x21290064


2.2 addi $t0, $t0, -16

  • Format: I-type
  • Fields: op=001000, rs=$t0=01000, rt=$t0=01000, immediate=-16

-16 in 16-bit two’s complement: 0xFFF0 → 1111 1111 1111 0000

Binary:

001000  01000  01000  1111111111110000
  op     rs     rt     immediate (-16)

Hex: 0x2108FFF0


2.3 lw $t1, 8($t0)

  • Format: I-type
  • Fields: op=100011 (lw), rs=$t0=01000, rt=$t1=01001, offset=8

8 = 0000 0000 0000 1000

Binary:

100011  01000  01001  0000000000001000
  lw     rs     rt         offset

Hex: 0x8D090008


2.4 sw $t2, 12($t0)

  • Format: I-type
  • Fields: op=101011 (sw), rs=$t0=01000, rt=$t2=01010, offset=12

12 = 0000 0000 0000 1100

Binary:

101011  01000  01010  0000000000001100
  sw     rs     rt         offset

Hex: 0xAD0A000C


2.5 beq $t0, $t1, 4

  • Format: I-type
  • Fields: op=000100 (beq), rs=$t0=01000, rt=$t1=01001, immediate=4 (branch 4 words forward)

Binary:

000100  01000  01001  0000000000000100
  beq    rs     rt         offset

Hex: 0x11090004


Part 3: J-Type — Solutions

3.1 j 0x00400020

  • Format: J-type
  • Fields: op=000010 (j), target = address >> 2

0x00400020 >> 2 = 0x00100008.
The 26-bit field is the lower 26 bits of the instruction address (word address):
0x00400020 / 4 = 0x00100008 → 00 0001 0000 0000 0000 0000 1000

Binary:

000010  000001000000000000001000
  op           26-bit target

Full 32-bit: 0000 1000 0001 0000 0000 0000 0000 1000

Hex: 0x08100008


3.2 jal 0x00401000

  • Format: J-type
  • Fields: op=000011 (jal), target = 0x00401000 >> 2 = 0x00100400

26-bit target: 00 0001 0000 0000 0001 0000 0000

Binary:

000011  000001000000000001000000
  jal              26-bit target

Hex: 0x0C100400


Part 4: Mixed — Solutions

4.1 slt $t0, $s0, $s1

  • Format: R-type
  • Fields: op=000000, rs=$s0=10000, rt=$s1=10001, rd=$t0=01000, shamt=00000, funct=101010

Binary:

000000  10000  10001  01000  00000  101010
  op     rs     rt     rd   shamt   funct

Hex: 0x0211402A


4.2 bne $t0, $zero, -8

  • Format: I-type
  • Fields: op=000101 (bne), rs=$t0=01000, rt=$zero=00000, immediate=-8

-8 in 16-bit two’s complement: 0xFFF8 → 1111 1111 1111 1000

Binary:

000101  01000  00000  1111111111111000
  bne    rs     rt     immediate (-8)

Hex: 0x1500FFF8


Summary table

# Instruction Format Hex
1.1 add $t2, $t0, $t1 R 0x01095020
1.2 sub $s0, $s1, $t0 R 0x02288022
1.3 and $t1, $t0, $t2 R 0x010A4824
1.4 jr $ra R 0x03E00008
1.5 sll $t1, $t0, 4 R 0x00084800
1.6 srl $t2, $t1, 2 R 0x00095082
1.7 sra $s0, $t0, 3 R 0x00088003
2.1 addi $t1, $t0, 100 I 0x21290064
2.2 addi $t0, $t0, -16 I 0x2108FFF0
2.3 lw $t1, 8($t0) I 0x8D090008
2.4 sw $t2, 12($t0) I 0xAD0A000C
2.5 beq $t0, $t1, 4 I 0x11090004
3.1 j 0x00400020 J 0x08100008
3.2 jal 0x00401000 J 0x0C100400
4.1 slt $t0, $s0, $s1 R 0x0211402A
4.2 bne $t0, $zero, -8 I 0x1500FFF8

Quick conversion tips

  • Binary → hex: Group 32 bits into 8 groups of 4; each group is one hex digit (0000=0 … 1111=F).
  • Negative immediate (I-type): Write the positive value in 16 bits, take two’s complement (flip bits, add 1), or use value + 2^16 (e.g. -16 → 0x10000 - 16 = 0xFFF0).
  • J-type target: Use (target address) / 4, then take the lower 26 bits. Upper 4 bits of the final address come from PC+4.
  • Shift instructions (R-type): For sll, srl, sra the shamt field holds the shift amount (0–31). rs is unused (0). rt is the source register; rd is the destination. sll multiplies by 2^shamt; srl is unsigned divide by 2^shamt; sra is signed divide by 2^shamt (sign-extended).