How to represent mips instruction as it's hex representation

5.5k views Asked by At

I'm given a MIPS instruction:

top:
 lw $t1, ($t0)
 beq $s0, $0, end
 jal func
 add $s0, $s0, $t0
 addi $s0, $s0, -1
 j top
 bne $s0, $0, top
end:
func:

 sll $v0, $t1, 4
 jr $ra

and am told to convert each line to the "instruction in hex." What I am having a problem with is the jal instruction. I understand it is a Pseudodirect address, what I don't understand it how to write it out as asked.

Given the OPCode for a jal instruction is 3hex, the first 6 bits in the J-Type instruction format would be 000011, how do I figure out the remaining?

I understand how to complete this task for R-Type and I-Type instruction formats but cant figure this one out.

Any help is appreciate.

2

There are 2 answers

0
Avi Dubey On BEST ANSWER

Opcode: 0000 11

Remaining 26 bits: Bits 2-27 of the address of label

Explanation:
The machine language equivalent that you know so far is:

0000 11xx xxxx xxxx xxxx xxxx xxxx xxxx

x represents not-known-at-this-point.

To find 32-bit Machine Language representation of jal func, first thing you'd need is the memory address of the label func. If you know the address of any of the instructions above, adding 4 to it for every instruction can give you the address of func label.
Lets say the address of func is 0x12345678 (binary: 0001 0010 0011 0100 0101 0110 0111 1000)
This address is 32-bits while you only have 26 bits to fit in.

To deal with this, you would do two things:

1.Since this address will always be a multiple of four, the last two bits are always 00. Therefore, we do not need to include them. Hence, our new "address" of func becomes: 0001 0010 0011 0100 0101 0110 0111 10--

2.Now we need to fit 30-bits of func address in 26-bits since 6-bits are occupied by the opcode. To do this, we ignore the 4 most significant bits. Machine takes these 4-bits from the PC. Hence, our new "address" of func becomes: ---- 0010 0011 0100 0101 0110 0111 10--

These 26-bits of the address of func make 32-bit Machine Language of jal become:

0000 1100 1000 1101 0001 0101 1001 1110
0
user35443 On

First you take the opcode, for the top 6 bits.

0b000011 = 0x03

Then you take the address of the next instruction, let's say

0x00155874

Shift it to the right by two (or perform an integer division by 4), which makes it

0x0005561D

Padd both values to 4 bytes

0x3 << 26  = 0x0C000000
0x0005561D = 0x0005561D

Perform a bitwise-or

0x0C05561D

That's it. (I could've made a mistake somewhere, though).