title: MIPS - JAL confusion: $ra = PC+4 or PC+8?
body: <p>I'm having trouble understanding how the instruction <strong>jal</strong> works in the MIPS processor.
  My two questions are:<br>
  <strong>a)</strong> What is the value stored in <strong>R31</strong> after "jal": <strong>PC+4</strong> or <strong>PC+8</strong>?<br>
  <strong>b)</strong> If it's really <strong>PC+8</strong>, what happens to the instruction at <strong>PC+4</strong>? Is it executed before the jump or is it never executed?</p>
  
  <p>In Patterson and Hennessy (fourth edition), pg 113:</p>
  
  <p>"jump-and-link instruction: An instruction that jumps to and address and simultaneously saves the address of the following instruction in a register (<strong>$ra</strong> in MIPS)"</p>
  
  <p>"program counter (<strong>PC</strong>): The register containing the address of the instruction in the program being executed"</p>
  
  <p>After reading those two statements, it follows that the value saved in <strong>$ra</strong> should be (<strong>PC+4</strong>).</p>
  
  <p>However, in the MIPS reference data (green card) that comes with the book, the jal instruction's algorithm is defined like this:<br>
  <strong>"Jump and Link : jal : J : R[31]=PC+8;PC=JumpAddr"</strong></p>
  
  <p>This <a href="http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/jump.html">website</a> also states that "it's really <strong>PC+8</strong>", but strangely, after that it says that since pipelining is an advanced topic "we'll assume the return address is <strong>PC+4</strong>".<br>
  I come from 8086 assembly, so I'm aware that there's a big difference between returning to an address and to the one following it, because programs won't work if I just assume something that's not true. Thanks.</p>