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.
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)
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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.