I'm working with binary MIPS instructions and I found this helpful document. However, I would need a clarification: all branch instructions have an immediate value. Is this immediate value sign-extended?
Most other immediates are, and if not that would mean a backwards jump is basically impossible, so I highly suspect that yes. However, I could not find another source that talks about the binary format of MIPS instruction to clarify.
An integer register on the MIPS is 32 bits. When a value is loaded from memory with fewer than 32 bits, the remaining bits must be assigned. Sign extension is used for signed loads of bytes (8 bits using the lb instruction) and halfwords (16 bits using the lh instruction).
To work with your data it must be 32-bits wide, this why we SIGN-extend, however another approach would be to ZERO-extend, but SIGN-extend is used when you are dealing with immediates and offsets to preserve the sign in 2's complement.
The branch offset is computed as the number of words between the branch instruction and the branch target. The offset is computed such that if it is added to the PC the result will be the address of the branch target instruction.
Branches in MIPS assembly work by adding or subtracting the value of the immediate part of the instruction from the $pc register. So if a branch is taken, $pc is updated by the immediate value, and control of the program continues at the labeled instruction..
Yes, all branch instructions use PC-relative addressing, which involves shifting the immediate field 2 bits left (to make efficient use of the otherwise 2 wasted lower-order bits; remember that all instructions are word-aligned) and then applying the sign-extension (you may address from PC - 2^17 up to PC + (2^17)-4). Here's a summary on addressing modes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With