title: Machine code for branching
body: <p>I'm a little confused as to how a branching instruction translates to machine code. I read in my book that the branch if equal instruction is a B-Type instruction, which is formatted like so:</p>
  
  <p>opcode   reg1 reg2    address + offset
     6      5     5           16</p>
  
  <p>In my textbook, there is a problem where I am shown a program, which I'm told is loaded at 0x4321ABC8 and looks like this: </p>
  
  <pre><code>L1:   sw   $t0,3000($t1)
  
    addi $s1,$t1,-6
  
    beq  $t0,$t1,L1
  </code></pre>
  
  <p>As you can see, the 3rd line of code is branch if equal. </p>
  
  <p>I am trying to convert this program into binary (or hex). <strong>On the branch if equal instruction, I don't understand exactly what it is I'm supposed to enter into the address + offset field.</strong></p>
  
  <p>First I thought the answer would be...</p>
  
  <p>base address (0x4321ABC8), 
  plus 4 times the number of instructions from base which is where the program counter would be (4 * 2 = 8),<br>
  Then minus four for the offset, which would be the original base address, 0x4321ABC8. </p>
  
  <p>The problem with this is that 0x4321ABC8 is a number too big to fit into the 16 bit offset space of the instruction. </p>
  
  <p>So then I thought that the answer would be to ONLY include the offset (which would be -8), however if that were the case, I don't know why the problem in the book would bother to tell me that the program is loaded at 0x4321ABC8.</p>
  
  <p>Any help would be greatly appreciated!</p>