user1097856
user1097856

Reputation: 169

MIPS Instruction J-Format

I have a question on the following MIPS code: If the first instruction is placed in memory address at 0xFFFFFF00, what is the hexadecimal representation of the instruction "j loopEnd"? I'm not sure how to do this. Any help would be greatly appreciated. Thanks.

loop:
    slt $t9, $s3, $s2   
    bne $t9, $zero, end
    add $s4, $s2, $s3           
    srl $s4, $s4, 1
    sll $t0, $s4, 2             
    add $t0, $s0, $t0       
    lw $t1, 0($t0)
    slt $t9, $s1, $t1       
    beq $t9, $zero, bigger  
    addi $s3, $s4, -1 
    j loopEnd
loopEnd:
    j loop

Upvotes: 6

Views: 7091

Answers (1)

JeLLyB0x3r
JeLLyB0x3r

Reputation: 328

j is a j-type instruction and has the following format: opcode - address which are 6 bits and 26 bits respectively.

The first 6 bits, the opcode for a j instruction is 000010

The next 26 bits for the address are a bit trickier. First you will need the address of the label you are referring to. In your example, since the instruction address of slt is 0xFFFFFF00, counting up by 4 everytime, the instruction address of loopEnd (which is instruction j loop) will be 0xFFFFFF2C.

So now we have the address of the target instruction 0xFFFFFF2C, but this is 32 bits while we only have 26 bits to use for the target address in a J-type instruction. This is dealt with by dropping the first hex digit and dropping the last 2 binary digits. The first hex digit the address that will actually be jumped to will be the same as the current instruction (F in your case). We can drop the last 2 binary digits because instruction addresses are always multiples of 4, so we don't need the precision of 0, 1, 2, 3 that the last 2 binary digits give us, they will always be 00 for an instruction address.

Combining all this for your example:

0xFFFFFF2C -> 0x FFFFF2C -> convert to binary

1111_1111_1111_1111_1111_0010_1100 -> 1111_1111_1111_1111_1111_0010_10

Now we combine the opcode with the interpreted instruction address: 000010_11111111111111111111001010

And that is the machine code for j loopend

TL;DR

  1. Get opcode for the jump instruction
  2. Get address at label in hex
  3. Drop the first hex digit of address
  4. Convert to binary
  5. Drop the last two bits
  6. Combine opcode and interpreted address

Upvotes: 8

Related Questions