0
votes

I have implemented beq instruction for mips assembly language. As my understanding for beq instruction (beq $s, $t, i), i can take on integer values or hex values. I have establish a bound for 16 bits integer value. I was wondering what is the bound for 16 bits hex values, so when i is too large (or too small?) it would output error before executing it. Following is beq instruction in binary.

Branch On Equal
beq $s, $t, i
0001 00ss ssst tttt iiii iiii iiii iiii

I tried (i > 0xffff) but it seems not cover all the cases. What should i do here? Thanks.

2
what exactly do you mean by "it seems not cover all the cases"?TomT
It does not pass the tests given by my professor.I'm pretty sure it's related to the range of those 16 bits hex.Jing
It's unclear to me exactly what you're trying to do. Are you writing a MIPS emulator? Or a MIPS assembler? Either way, the offset is signed, so the range is -32768..+32767, which then will be multiplied by 4 before being added to PC.Michael
What Michael's comment means, it's +/- 32k words. zero = no offset (= nop), 0xffff = -1 = 1 word backwards (branch to itself), ...turboscrew

2 Answers

0
votes

It should be in between 0 and 2^16 - 1 for hex(0 <= i <= 65535);
It should be in between -2^15 and 2^15 - 1 for int(-32768<=i<=32767)

0
votes

When you need to conditionally branch farther than +- 2^15 instructions (byte offset = two's complement 16 bits << 2), you can conditionally jump over a non-conditional jump that can reach the target.

beq $s, $t,  target      # short jump, approx +- 2^17 bytes

vs. j uses pseudo-direct addressing, keeping the top 4 bits of PC and replacing the rest with a 26-bit << 2 immediate. This lets you reach any target address in the same aligned 1/16th of address space as your code.

bne $s, $t,  .nojump
j   target               # anywhere with the same top 4 bits
.nojump:

vs. putting the address in a register and using jr to reach any 32-bit address:

bne $s, $t,  .nojump
lui  $t0,      %hi(target)
ori  $t0, $t0, %lo(target)      # this is what  la $t0, target does.  It might use addui, but whatever
jr   $t0
.nojump: