3

I am working on datapaths and have been trying to understand branch instructions.

So this is what I understand. In MIPS, every instruction is 32 bits. This is 4 bytes. So the next instruction would be four bytes away.

In terms of example, I say PC address is 128. My first issue is understanding what this 128 means. My current belief is that it is an index in the memory, so 128 refers to 128 bytes across in the memory. Therefore, in the datapath it always says to add 4 to the PC. Add 4 bits to the 128 bits makes 132, but this is actually 132 bytes across now (next instruction). This is the way I make sense of this.

In branch equals, say the offset is a binary number of 001. I know I must sign extend, so I would add the zeroes (I will omit for ease of reading). Then you shift left two, resulting in 100. What is the purpose of the shift? Does the offset actually represent bytes, and shifting left will represent bits? If so, adding this to the PC makes no sense to me. Because if PC refers to byte indeces, then adding the offset shifted left two would be adding the offset in number of bytes to PC that is in number of bytes. If PC 128 actually refers to 128 bits, so 32 bytes, then why do we only add 4 to it to get to the next instruction? When it says PC+4, does this actually mean adding 4 bytes?

My basic issue is with how PC relative addressing works, what the PC+4 means, and why offset is shifted by 2.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
user3794422
  • 311
  • 1
  • 5
  • 17
  • *"Add 4 bits to the 128 bits makes 132"* this is quite irrelevant here (although mathematically true). PC is 32bit, and you are adding value 4 to it, not bit size. And here *"the offset actually represent bytes, and shifting left will represent bits"* you get to "bits" again in confused way. bits/bytes are just a way to think about some numeric value, ie. value 12 can mean 12 instructions away, or 12 bytes away (3 instr), or you can think about it as 1100 bit mask, but it's still the same number 12. Shift is easy to imagine at bit level (binary format), but affects value. 12>>1 = 0110b = 6. – Ped7g Oct 18 '16 at 07:36

1 Answers1

3

Shifting left by n bits is the same thing as multiplying the number by 2n. Shifting left 2 bits multplies by 4.

If your branch offset is shifted left by 2, that means your branch offset operand is in whole instruction units, not bytes. So a branch instruction with an 8 operand means, jump 8 instructions, which is 32 bytes.

MIPS multiplies by 4 because instructions are always 32 bits. 32 bits is 4 bytes.

MIPS Instructions are guaranteed to begin at an address which is evenly divisible by 4. This means that the low two bits of the PC is guaranteed to be always zeros, so all branch offsets are guaranteed to have 00 in the low two bits. For this reason, there's no point storing the low two bits in a branch instruction. The MIPS designers were trying to maximize the range that branch instructions can reach.

PC means "Program Counter". The program counter is the address of the current instruction. PC+4 refers to the address 4 bytes past the current instruction.

Branch Offset

MIPS branch offsets, like most processors, are relative to the address of the instruction after the branch. A branch with an immediate operand of zero is a no-op, it branches to the instruction after the branch. A branch with a sign extended immediate operand of -1 branches back to the branch.

The branch target is at ((branch instruction address) + 4 + ((sign extended branch immediate operand) << 2)).

Community
  • 1
  • 1
doug65536
  • 6,562
  • 3
  • 43
  • 53
  • So if it gave the PC as 128, adding 4 bytes results in 132 correct? And the offset is 1, shifted left two gives 4 (4*1). If the offset is 4 and the PC+4 is 132, the new PC is PC+4+offset, which is 136. For the sake of this example am I correct? – user3794422 Oct 18 '16 at 03:34
  • @user3794422 Yes. The branch offset is relative to the address of the instruction that immediately follows the branch, so a branch offset of 0 is a no-op (it branches to the instruction after the branch). A branch with an immediate operand of -1 branches back to the branch (-1 << 2 is -4). Is that what you were asking? – doug65536 Oct 18 '16 at 04:38
  • So I am attempting this practice problem in my textbook that says to convert the instruction 00010000011001011111111111110100 to a MIPS code and tell what the new PC address is, given the PC address is currently 128. So I assume 128 refers to bytes. I take the last 16 bits, which are 1111111111110100. Naturally, these would be sign extended, adding 16 1 bits in front. Regardless, the number is -12 in decimal. it's basically multiplied by 4, so it's -48. And this added to the 128 results in an address of 80 right? Am I correct in this reasoning? The textbook fails to provide an answer. Thanks. – user3794422 Oct 18 '16 at 17:22
  • If the branch instruction is at address 128, and the sign extended immediate operand of the branch is `-12`, then the branch target is at `(128 + 4 + (-12 << 2))` or `128 + 4 - 48` so the target of the branch is address `84`. Remember, the branch offset is relative to the address of the instruction *after* the branch. – doug65536 Oct 18 '16 at 21:49
  • Ah you are correct. 84 is the answer I get as well. Forgot the +4 bit. Thank you for the help. Appreciate it. – user3794422 Oct 18 '16 at 23:06