MIPS branches. How to jump backwards?
I've been looking for the answer quite a while and finally I decided to ask and see if someone here can help me ;)
The format for the conditional jump instruction in MIPS, lets say a beq instruction, contains a 16-bit field that indicates de "size" of the jump. I know it must be added to the current PC in order to obtain the desired direction. My doubt is, how do I set the field for a backwards jump? I guess two's complement? I found this example, but it confused me more than I was... if anyone could explain it briefly I'd be very thankful! By the way, sorry about my grammar/expressions, I'm not very good at english yet.
The example mentioned:
PC+Jump(Forward)
0100 1101 0000 0011 1010 1100 0101 1000
0000 0000 0000 0000 0000 0000 1001 1100
.......................................................................
0100 1101 0000 0011 1010 1100 1111 0100
PC+Jump(Backwards)
0100 1101 0000 0011 1010 1100 0101 1000
1111 1111 1111 1111 1100 0000 1001 1100
开发者_开发百科
.......................................................................
0100 1101 0000 0011 0110 1100 1111 0100
The offset is a signed value, so jumping backward just uses a negative offset.
The examples you've shown demonstrate simple addition. In the context of branching and jumping, the top number represents the current PC, the second number is the offset, and the bottom number is the new PC. Recall that in two's complement, subtraction is the same as addition of a negative number. Notice that in the backward example, the offset is negative. When the offset is encoded in a MIPS instruction, it gets sign-extended to the full 32 bits.
精彩评论