6

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
4
  • Why not just run as and then objdump to find out? Seems like the fastest and easiest way to me.
    – Michael
    Apr 4, 2013 at 15:22
  • Hi, i'm not sure what you are referring to. I'm supposed to calculate it manually. Apr 4, 2013 at 15:24
  • I'm referring to using an assembler and disassembler. Your question is unanswerable in its current state though, because there's no loopEnd in the code you pasted, which makes it impossible to calculate the branch target address.
    – Michael
    Apr 4, 2013 at 15:35
  • oh, sorry, missed out the loopEnd code. have re-edited my qn. Apr 4, 2013 at 15:38

1 Answer 1

8

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.