Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are the offsets on MIPS branch instructions sign-extended?

Tags:

mips

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.

like image 432
zneak Avatar asked Apr 03 '12 03:04

zneak


People also ask

Which instruction does sign extension in MIPS?

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).

Why do we need to sign extend the offset in data transfer instructions?

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.

What is offset in branch instruction?

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.

How do branch instructions work in MIPS?

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..


1 Answers

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.

like image 119
blackcompe Avatar answered Sep 18 '22 06:09

blackcompe