Professional Documents
Culture Documents
a) Assemble the following MIPS code sequence into the equivalent machine code (in hex
format). Write your answer to the right side of each instruction.
b) Disassemble the following three machine code instructions (in hexadecimal) to their
equivalent MIPS assembly language instructions. Write your answer to the right side of each
instruction. For partial credit, show your work.
Give the machine code for the MIPS assembler code assembled at the starting address 0x0040 0000. (You
may use a combination of decimal, hexadecimal and/or binary encodings.)
1
Questions 1 & 2 (Quiz #1 Sec3 Spring 2016)
1. How is the jump target address calculated for j and jal instructions?
The 26 bit address field in the J-format instruction is extended to 28 bits by multiplying
by 4 (or shifting left 2 places, or concatenating two 0s). Then the most significant 4 bits
from PC+4 (bits 31-28) are concatenated at the left to form the 32-bit pseudo-direct
address, of the jump target.
2. Assemble this line of MIPS code into its equivalent machine code. Don't write 32 ones
and zeros; instead, show the instruction format and in each field, give the correct value
(in base 10 numbers): sw $sp, 8($s5)
opcode rs rt immediate
43 21 29 8
Assemble the following MIPS code into the equivalent machine language code. Next to each
instruction, give the machine code in hexadecimal form. [To be able to gain partial credit, you
should show your work, giving the machine code in binary form also.]
2
bne $ra, $a2, Top
bne $31 $6 -3
000101 11111 00110 1111111111111101
HEX: 0x17E6FFFD (immediate is -3, since Branch Target Address is: PC + 4 + [SignExt
(immed) x 4 ]
What assembly language statement do each of the following hexadecimal patterns represent, if
interpreted as a MIPS machine instruction?
i) 0x0000000C :
The binary bit pattern is: 0000 0000 0000 0000 0000 0000 0000 1100
opcode = 000000 = 0 this code is for all R-type instructions
funct = 001100 = 12, the code for syscall
rs field = rt field = rd field = 0
shamt field = 00000 = 0
the MIPS instruction is : syscall
ii) 0x9681FFE8 :
The binary bit pattern is: 1001 0110 1000 0001 1111 1111 1110 1000
Opcode = 100101 = 37 this code is for lhu (load halfword unsigned), an I-type
instruction
rs field = 10100 = 20, the code for $s4
rt field = 00001 = 1, the code for $at
immed field = 1111111111101000, which is -24
the MIPS instruction is: lhu $at, -24 ($s4)
iii) 0xE7A40008 :
The binary bit pattern is: 1110 0111 1010 0100 0000 0000 0000 1000
Opcode = 111001 = 57 this code is for swc1 (store word coprocssor 1), an I-type
instruction
3
rs field = 11101 = 29, the code for $sp
rt field = 00100 = 4, the code for $ft0 or $4 (floating point register ft0, in coprocessor 1)
immed field = 0000000000001000, which is 8
the MIPS instruction is: swc1 $ft0, 8 ($sp)
Assemble the following MIPS code into the machine language. For each instruction, give the
machine code in binary form, along with the hexadecimal equivalent.
lw $2 $26 28
100011 00010 11010 0000000000011100
HEX: 0x8C5A001C
iv) 0xC3E1000C :
The binary bit pattern is: 1100 0011 1110 0001 0000 0000 0000 1100
Opcode = 110000 = 45 this code is for ll (load linked), an I-type instruction
rs field = 11111 = 31, the code for $ra
rt field = 00001 = 1, the code for $at
immed field =0000000000001100, which is 12
the MIPS instruction is : ll $at, 12 ($ra)
v) 0xAAAAAAAA :
The binary bit pattern is: 1010 1010 1010 1010 1010 1010 1010 1010
Opcode = 101010 = 42 this code is for swl (store word left), an I-type instruction
4
rs field = 10101 = 21, the code for $s5
rt field = 01010 = 10, the code for $t2
immed field = 1010101010101010, which is -21846
the MIPS instruction is: swl $t2, -21846 ($s5)
vi) 0x00000000 :
The binary bit pattern is: 0000 0000 0000 0000 0000 0000 0000 0000
Opcode = 000000 = 0 this code is for an R-type instruction
rs field = 00000 = 0, the code for $0 (or $zero)
rt field = 00000 = 0, the code for $0
rd field = 00000 = 0, the code for $0
shamt field = 00000 = 0
funct field = 000000 = 0 this code is for sll (shift left logical)
the MIPS instruction is: sll $0, $0, 0
a) Assemble the MIPS jump instruction into machine code (give the machine instruction
in hex)
Top: instruction1
instruction2
instruction3
0x0040F124: j Top
Address of Top is 0x0040F118 = 0000 0000 0100 0000 1111 0001 0001 1000
26-bit address field (from above) must be 0000 0100 0000 1111 0001 0001 10
5
So the assembly language instruction is ori $gp, $at, 0xF0F0
a) Assemble this MIPS instruction into machine code (give the machine instruction in
hex)
BEQ_LOAD: beq $s1, $t9, BEQ_LOAD
opcode rs rt immed
000100 10001 11001 1111 1111 1111 1111
= 0x 1239FFFF
opcode rs rt immed
001000 11101 00100 1111 1111 1111 1110
Assemble this line of MIPS code into its equivalent machine code. Don't write 32 ones and
zeros; instead, show the instruction format and in each field, give the correct value (in base
10 numbers): sw $sp, 8($s5)
opcode rs rt immediate
43 21 29 8
6
Question 5 (Retake Spring 2014)
a. Interpret the 32-bits in 0x8024FFF9 as a MIPS machine language instruction. Give the
equivalent MIPS assembly language instruction. Show your work and write your answer in the
box provided below.
lb $a0, -7($at)
d. Assemble the MIPS pseudo-instruction Spin: bgt $t0, $s0, Spin into the equivalent real MIPS
machine language instructions, giving them in 8-digit hex format. Show your work and write the
answer in the box provided below.
0x0208082A
0x1420FFFE
a. Translate the assembly language statement bgt $t9, $fp, Top into MIPS machine language,
giving the resultant machine language statement(s) in the standard 8-digit hex format. [The
statement at Top: is located just before the bgt statement.] Show your work and write your
answer in the box provided below.
0x03D9082A
0x1420FFFD
bgt $t9, $fp, Top is a pseudoinstruction, requiring 2 real MIPS instructions:
slt $at, $fp, $t9 op=0, rs=30, rt=25, rd=1, shamt=0, func=42
bne $at, $zero, Top op=5,rs=1 rt=0, immed= -3 (go back 3 from next instr to Top)
machine code for slt: 000000 11110 11001 00001 00000 101010 = 0x03D9082A
machine code for bne:000101 00001 00000 1111111111111101 = 0x1420FFFD
7
c. Interpret the 32-bits in 0xA3A1FFF8 as MIPS machine language instruction. Give the
equivalent MIPS assembly language instruction. Show your work and write your answer in the
box provided below. sb $at, -8($sp)
a) Assemble the following MIPS code into the machine language. Give the machine code in
hexadecimal next to each instruction.
Give details for your answer below one by one for each instruction.
a) Assemble the beq instruction into the equivalent machine code. Write your answer in the box.
Give your answer in Hexadecimal.
Skip:
lb $t9, -9 ($sp)
lw $t1, sum
beq $s3, $t5, Skip
12 6D FF FC
Note that lw instruction generated two instructions so we have to branch back 4 words. FF FC is
2s complement notation for -4.
12 6D FF FD
8
b) Assemble j instruction into the equivalent machine code. Write your answer in the box. Give
your answer in Hexadecimal. Assume that the memory address of the label main is 00 40 00 0016.
.text
.globl main
main:
j Next
Comeback:
add $t1, $zero, $zero
add $t2, $zero, $zero
Next: <== address: 00 40 00 0C
addi $t3, $t1, 1
08 10 00 03
In the address part get the leftmost 4 bits from PC (don't store), drop last 2 bits. In the following
the rightmost two 11 comes from the address of Next which is at 00 40 00 0C (C: 1100, we drop
the last two bits of C).
0000 10 00 0001 0000 0000 0000 0000 0011
i. Generate the machine language equivalent (object code) for the following MIPS instruction
of the above code segment. Give your answer in hexadecimal.
lw $8, 100($7)
8C E8 00 64
ii. What will be the contents of $5 (in hex) after executing the above code segment?
Note that the instruction at memory location "main" is loaded into $5.
9
8C E8 00 64
iii.. Generate the machine language equivalent (object code) for the following MIPS instruction
of the above code segment. Give your answer in hexadecimal.
Since lw $5, main is a pseudo that must be expanded into two real MIPS machine instructions,
the distance from main to the next instruction after the branch is 4. So this is a branch back of 4
words. The instruction beq is of I-type and instruction components are: 4, 8, 5, -4. In binary form
000100 01000 00101 1111 1111 1111 1100.
-4 in 2s complement using 16 bits is FF FC.
11 05 FF FC
b. Consider the following MIPS instruction in hexadecimal. Give its assembler instruction
equivalent as you would see in a source program.
AC B0 00 18
sw $16, 24($5)
or
sw $s0, 24($a1)
b) Assemble the following 2 MIPS instructions into the equivalent machine language
instructions, in hex format. Put your answers in the spaces provided.
Give the MIPS machine code (in 8-digit hex format) for the lines of code marked below as a:, b:
and c: Let the first line of the program be located at the start of code space in MIPS memory, at
0x00400000. Write your answers in the space provided.
Answers: jr $t0 has opcode=0, func=8. rs=8, but rt=rd=shamt=0. So the R-type
instruction is: 000000 01000 00000 00000 00000 001000
bne $t1, $zero, Top has opcode=5, rs=9, rt=0. The address of Top is
0x0040000C, which is -6 instructions back from Next (which is PC+4, at
address 0x00400024). Therefore the relative branch offset in bytes from
PC+4 is -24, so the immediate value must be -6 (since it will be shifted
left by 2 by the hardware, before adding with PC+4 to form BTA). So the
I-type instruction is: 000101 01001 00000 1111111111111010
Machine Instruction
a) 0x01000008
11
b) 0x08100009
c) 0x1520FFFA
For the following code segment consisting of 2 pseudo-instructions, give the MIPS machine code
instructions (in hexadecimal format) that will be generated by the assembler:
Write your answer in the box below. Use 8-digit hexadecimal format to represent the machine
language statements.
Solution:
The pseudo-instructions are translated into 3 real MIPS instructions as follows:
Target: sll $0, $0, 0
Test: slti $at, $t9, 11
beq $at, $zero, Target # the Target address is at PC + 4 – 12
0x2B21000B
a) Disassemble the following MIPS machine code into its closest assembly language equivalent.
Assume that the code is normally located, in the Text (.txt) segment.
0x23E4FFFC
0x0F0F0F0F
Give the MIPS assembly instructions in the box. Show your work in the space after the box.
addi $a0, $ra, -4
jal 0x0C3C3C3C3C
12
0010 00|11 111|0 0100| 1111 1111 1111 1100
op=8 rs=31 rt= 4 immed16 = -4
addi $ra $a0 -4
0000 11|11 0000 1111 0000 1111 0000 1111 this is J-type, opcode is 3 for jal
PC[31-28] = 0000, since the Text segment is located below memory address 0x10000000
PC[27-0] = 11 0000 1111 0000 1111 0000 1111 || 00 where || means concatenated
= C 3 C 3 C 3 C
b) Assemble the following MIPS assembly code into its equivalent machine code instructions
and put them in hexadecimal format.
Top: sllv $a3, $s5, $t7
bne $a3, $k1, Top
Write your answer in the following box. Show your work in the space after the box.
0x01F53804
0x14FBFFFE
c) What real MIPS assembly instructions would the MARS assembler generate, if the following
pseudoinstructions are its input:
ble $a0, $a1, Top
li $a2, -12345
13
Convert the following statements into their machine language equivalent. Give the hexadecimal
form of the instruction, as it would be found in memory (.text segment). Write your answers in
the boxes provided.
a) beq $t8, $s5, Loop_top (the instruction is located at 0xCCCF0064, and Loop_top is at
0xCCCFF044)
Branches use PC-relative addressing, so the branch target address is calculated as:
PC + 4 + SignExtended (immed16) * 4
The distance between the branch target and PC+4 is: F044 - 0068 = EFDC bytes (in base 2, …
00 1110 1111 1101 1100 bytes), which is …0011 1011 1111 0111 instructions. So the 16-bit
immediate value must be 0011 1011 1111 0111
op rs rt immed16
000100 11000 1 0101 0011 1011 1111 0111
bne $t8 $s5 difference in words ( branch target - PC+4)
The equivalent hexadecimal is 0x13153BF7
[Note that multiplying a value by 4, shifting the value left by two places, and concatenating 00 to
the right-hand end of the binary bits all do the same thing. ]
The jump target address has the same upper 4 bits as PC[31:28], so the jump is not out of the
range for J-type. The 26-bit immediate in the J-type instruction is concatenated with PC[31:28]
in the most significant end, and with 00 in the least significant end, to form the 32 bit jump target
address.
jal Call_point
0000 11 11 1111 1100 0001 0011 1100 1011
Write the equivalent MIPS assembly code. The values in the table are in base 10.
1000 10 4 8 1
1004 0 0 8 13 2 0
1008 35 29 4 4
1012 8 18 29 4
1016 0 31 0 8
14
slti $t0, $a0, 1
sll $t5, $t0, 2
lw $a0, 4($sp)
addi $sp, $s2, 4
jr $ra
1000 0 16 5 9 0 32
1004 36 9 10 [… 0 …]
1008 0 16 4 11 0 32
1012 40 11 10 [… 0 …]
1016 4 10 0 [… 2 …]
1020 8 16 16 [… 1 …]
1024 2 [… … 250 … …]
a. Practice doing conversion from assembly into machine language (with fields)
Convert the following lines of MIPS assembly code into machine language, filling in the
decimal values of each field. (Hint: if the Green Card doesn’t give enough info, see
Appendix B in the back of the book)
b. Practice doing conversion from machine language (in hex) into assembly language
c. Practice doing conversion from assembly language into 32-bit binary machine language, then
abbreviating it in hex.
What is the 8-digit hexadecimal value of the machine instruction for each of the
following?
addi $v0, $sp, -28 => 001000 11101 00010 1111111111100100
=> 0x23A2FFE4
sw $gp, 36 ($fp) => 101011 11110 11100 0000000000100100
=> 0xAFDC0024
c. Convert the following lines of MIPS assembly code into machine code, using the format
shown on the lecture slide (the I-format of page 97), filling in the values of each field in binary:
lw $t9, 8($a3)
100011 00111 11001 00000 ....... ....1000
sw $v1, -4($s5)
101011 10101 00011 11111….. ….1100
Question 1 (Older)
a) Assemble the following MIPS code sequence into the equivalent machine code. Write your
answer in the box, next to each instruction. Give your answer in Hexadecimal.
b) Disassemble the following two machine code instructions (in hexadecimal) to their equivalent
MIPS assembly language instructions. Write your answer in the box, next to each instruction.
16
0xAA82F134 swl $v0, -3788($s4)
0x03A0C809 jalr $sp, $t9
Question 2 (Older)
1. How is the jump target address calculated for j and jal instructions?
The 26 bit address field in the J-format instruction is extended to 28 bits by multiplying by 4.
Then the most significant 4 bits from PC (bits 31-28) are concatenated at the left to form the 32-bit
pseudo-direct address, of the jump target.
2. Assemble this line of MIPS code into its equivalent machine code. Don't write 32 ones and zeros;
instead, show the instruction format and in each field, give the correct value (in base 10
numbers): sw $sp, 8($s5)
opcode rs rt immediate
43 21 29 8
Question 3 (Older)
Assemble the following MIPS instructions into the equivalent machine code (in hex format).
Write your answer to the right side of each instruction.
Since bgt is a pseudoinstruction, it must be assembled into real MIPS instructions which do the
same thing. In this case, it takes 2 real MIPS instructions.
Question 4 (Older)
Convert the following MIPS machine language instruction to the equivalent MIPS assembly
language instruction: 00100011000111011111111111111110 (or 0x231DFFFE)
Question 5 (Older)
a) Give the binary codes for the following MIPS items (be sure give the correct number of
bits, not more or less):
--opcode sc 111000
--register $a2 00110
--function code div 011010
--ASCII code for “@” 1000000
b) Give the binary codes for the following MIPS items (be sure give the correct number of
bits, not more or less)
--opcode sb 101000
--register $s4 10100
--function code jalr 001001
--ASCII code for “z” 1111010
c) Give the binary codes for the following MIPS items (be sure give the correct number of
bits, not more or less)
--opcode bne 000101
--register $t8 11000
--function code sub 100010
--ASCII code for “T” 1010100
Question 6 (Older)
Assemble the following MIPS code sequence into the equivalent machine code (in hex format).
Write your answer to the right side of each instruction.
b) Disassemble the following two machine code instructions (in hexadecimal) to their equivalent
MIPS assembly language instructions. Write your answer to the right side of each instruction.
18
Question 7 (Older)
Assemble the following MIPS code into the machine language. Give the machine code in binary
form, along with the hexadecimal equivalent.
bne $8 $5 PC + 4 + (1 x 4)
000101 01000 00101 0000000000000001
HEX: 0x15050001 (immediate is 1, since PC PC + 4 + [Signext (imm16)] x 4 )
addi $2 $2 1
001000 00010 00010 0000000000000001
HEX: 0x20420001
lw $4 $4 0
100011 00100 00100 0000000000000000
HEX: 0x8C840000
Question 8 (Older)
What do the following bit patterns represent, if interpreted as a MIPS machine instruction?
b) 0011 1100 0000 0001 0000 0000 0000 0001 : lui $at, 1
19