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
2,990 30 30 silver badges 35 35 bronze badges
asked Apr 4, 2013 at 15:19
user1097856 user1097856
169 3 3 silver badges 15 15 bronze badges
Why not just run as and then objdump to find out? Seems like the fastest and easiest way to me.
Commented Apr 4, 2013 at 15:22
Hi, i'm not sure what you are referring to. I'm supposed to calculate it manually.
Commented 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.
Commented Apr 4, 2013 at 15:35 oh, sorry, missed out the loopEnd code. have re-edited my qn. Commented Apr 4, 2013 at 15:38j 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
Now we combine the opcode with the interpreted instruction address: 000010_11111111111111111111001010
And that is the machine code for j loopend