How to represent mips instruction as it's hex representation
NickName:corporateWhore Ask DateTime:2015-06-11T10:24:13

How to represent mips instruction as it's hex representation

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.

Copyright Notice:Content Author:「corporateWhore」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30770508/how-to-represent-mips-instruction-as-its-hex-representation

Answers
Avi Dubey 2015-06-13T23:47:31

Opcode: 0000 11\n\nRemaining 26 bits: Bits 2-27 of the address of label\n\nExplanation:\nThe machine language equivalent that you know so far is:\n\n0000 11xx xxxx xxxx xxxx xxxx xxxx xxxx\n\n\nx represents not-known-at-this-point.\n\nTo 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.\nLets say the address of func is 0x12345678 (binary: 0001 0010 0011 0100 0101 0110 0111 1000)\nThis address is 32-bits while you only have 26 bits to fit in.\n\nTo deal with this, you would do two things:\n\n1.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--\n\n2.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--\n\nThese 26-bits of the address of func make 32-bit Machine Language of jal become:\n\n0000 1100 1000 1101 0001 0101 1001 1110\n",


More about “How to represent mips instruction as it's hex representation” related questions

MIPS Shift Instruction

In an sll instruction in MIPS, it can only take 5-bits. How would the shift work if the shift amount was more than 31? How do we represent that with 5-bits? Thanks

Show Detail

How represent each mips instruction line in c++ struct?

I am trying to simulate virtual MIPS architecture using c++. During this process I have to store each line of mips architecture as struct and store it into vector so I can simulate 5 stages of pipe...

Show Detail

MIPS Instruction Decoding

I'm trying to understand how to decode MIPS binary instructions. I compiled a hello world program in C on a Debian MIPS system with gcc and objdump shows me that the first instruction in the .text

Show Detail

MIPS Instruction Register

If the bit pattern 0x0C000000 is placed into the Instruction Register, what MIPS instruction will be executed? I am not sure what this question means by the Instruction Register and how it relates...

Show Detail

Pseudo Instruction for Division in MIPS

What is the pseudo instruction for division in MIPS? Like there is a pseudo instruction of multiplication is MIPS "mul" which makes the life a little bit easier. I am using QT Spim. Regards

Show Detail

qemu mips undefined instruction

I am experimenting with the MIPS architecture using the qemu simulator. However, I get an undefined instruction exception (code 10 in the cause register) when I try to execute an swm instruction

Show Detail

How to represent mips instruction as it's hex representation

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 a

Show Detail

MIPS Instruction code conversion to binary TIPS?

I am studying for the MIPS instruction code conversion to binary. I noticed while doing some conversions that instructions are formatted differently depending on the mnemonics(since DIFFERENT OPERA...

Show Detail

addiu instruction encoding (MIPS,GCC)

Here is addiu instruction opcode (16-bit instructions, GCC option -mmicromips): full instruction: addiu sp,sp,-280 opcode, hexa: 4F75 opcode, binary: 1001(instruction) 11101(sp i...

Show Detail

MIPS instruction questions

Can the subu instruction in MIPS give me a negative result, or will the result always be positive since we are doing the unsigned version of the sub? Also, if I want to do arithmetic shift right, a...

Show Detail