Exercises in MIPS
Exercises in MIPS
Q. 1. [15 points] Convert the following MIPS assembly language code into machine
code. Write the instructions in hexadecimal.
add $t0, $s0, $s1
lw
$t0, 0x20($t7)
addi $s0, $0, -10
add
$t0
$s0
$s1
shamt
funct
000000
01000
10000
10001
00000
100000
000000
10000
10001
01000
00000
100000
00000010000100010100000000100000
02114020
Final answer:
0x02114020
Page 1 of 7
Second instruction: lw
$t0, 0x20($t7)
In this case, register $t0 is to be loaded with the word that is located at the memory
location whose base address is in register $t7, the offset being 0020 (hexadecimal).
Basic format of the assembly instruction:
lw
$t0
Offset: 0x0020
$t7
100011
01000
0000000000100000
01111
100011
01111
01000
0000000000100000
10001101111010000000000000100000
8de80020
Final answer:
0x8de80020
addi
$s0
$0
-10
001000
10000
00000
001000
00000
10000
00100000000100001111111111110110
2010fff6
Final answer:
0x2010fff6
Page 2 of 7
Q. 2. [20 points] Convert the following program from machine language into MIPS
assembly language. The numbers on the left are addresses in memory, and those on the
right are instructions at the respective addresses.
Address
0x00400000
0x00400004
0x00400008
0x0040000c
0x00400010
0x00400014
0x00400018
0x0040001c
Instruction
0x20080000
0x20090001
0x0089502a
0x15400003
0x01094020
0x21290002
0x08100002
0x01001020
001000
00000
01000
0000000000000000
001000
01000
00000
0000000000000000
addi
$t0
$0
Assembly instruction
Final answer:
001000
00000
01001
0000000000000001
001000
01001
00000
0000000000000001
addi
$t1
$0
Assembly instruction
Final answer:
Page 3 of 7
000000
00100
01001
01010
00000
101010
000000
01010
00100
01001
00000
101010
slt
$t2
$a0
$t1
--
--
Assembly instruction
Final answer:
bne
Rs
Rt
Imm
000101
01010
00000
0000000000000011
At this point, recall the assembly language format of this instruction and its
correspondence with the machine language format:
bne Rs, Rt, Label #If [Rs]!=[Rt] then PC = PC + se Imm << 2
In this particular case, Imm << 2 in binary is given by 0000000000001100 that is equal to
0x0000000c. Further, PC currently holds 0x00400010 (address of the next instruction).
Accordingly, Label itself will correspond to the memory address 0x00400010 plus
0x0000000c that is 0x0040001c. Let L2 denote the address 0x0040001c.
Binary representation in the assembly format:
Assembly instruction
000101
01010
00000
0000000000011100
bne
$t2
$0
L2
Final answer:
Page 4 of 7
000000
01000
01001
01000
00000
100000
000000
01000
01000
01001
00000
100000
add
$t0
$t0
$t1
--
--
Assembly instruction
Final answer:
001000
01001
01001
0000000000000010
001000
01001
01001
0000000000000010
addi
$t1
$t1
Assembly instruction
Final answer:
Imm
000010
00000100000000000000000010
Load the PC with the address formed by concatenating the first four bits of the current PC
with the value in the immediate field padded at the rightmost end by two 0s. Now, PC
currently holds 0x0040001c (address of the next instruction). Accordingly, jump will be
made to the memory address 00000000010000000000000000001000 that is 0x00400008.
Let L1 denote this address.
Final answer:
j L1
Page 5 of 7
000000
01000
00000
00010
00000
100000
000000
00010
01000
00000
00000
100000
add
$v0
$t0
$0
--
--
Assembly instruction
Final answer:
Here is the complete assembly code corresponding to the given machine code:
L1:
L2:
Page 6 of 7
Q. 3. [20 points] Implement the following high-level code segments using the slt
instruction. Assume that the integer variables g and h are in registers $s0 and $s1,
respectively.
(a)
if (g > h)
g = g + h;
else
g = g h;
(b)
if (g >= h)
g = g + 1;
else
h = h 1;
(a)
else:
done:
# $s0 = g, $s1 = h
slt $t0, $s1, $s0
beq $t0, $0, else
add $s0, $s0, $s1
j done
sub $s0, $s0, $s1
#
#
#
#
#
if h < g, $t0 = 1
if $t0 == 0, do else
g = g + h
jump past else block
g = g - h
# $s0 = g, $s1 = h
slt $t0, $s0, $s1
bne $t0, $0, else
addi $s0, $s0, 1
j done
addi $s1, $s1, -1
#
#
#
#
#
if g < h, $t0 = 1
if $t0 != 0, do else
g = g + 1
jump past else block
h = h - 1
(b)
else:
done:
Page 7 of 7