title: Correct Stack Manipulation in X.86 X64 assembler
body: <p>So having read the x64 architecture quick start guide I wrote some assembler.</p>
  
  <p><a href="https://software.intel.com/en-us/articles/introduction-to-x64-assembly" rel="nofollow noreferrer">https://software.intel.com/en-us/articles/introduction-to-x64-assembly</a></p>
  
  <p>The assembler function is called from C. In turn the assembler calls C functions.</p>
  
  <p>I am unsure as to how the stack mechanics work as I seem to be corrupting the stack on several occasions.</p>
  
  <p>The following code demonstrates:</p>
  
  <pre><code>PUBLIC Lbra_R_A ; Op 16 - Long Branch Always
  Lbra_R_A PROC
      sub rsp, 28h
      push rbx ; must preserve rbx
      ; Calc destination branch address by Adding the two Bytes at [PC+1] (high byte) and [PC+2] (low byte) with PC+2 reg
      ; Get first byte high byte
      movzx rcx, word ptr [pc_s]
      mov rbx, rcx ; save pc_s into temp
      inc bx ; inc temp pc_s
      call MemRead8_s ; returns byte in ax (al)
      push ax ; save high byte
      ; Get second byte low byte @ pc_s
      mov rcx, rbx
      inc bx ; inc temp pc_s
      call MemRead8_s ; returns byte in ax (al) - this call destroys saved high byte???
      ; combine low and high bytes to make 16 bit 2 complements offset
      pop dx ; get saved high byte - wrong value
      mov ah, dl ; move the high byte to high position ; ax now contains 16 bit offset
      add bx, ax ; bx now contains pc_s(+2) + offset
      mov word ptr [pc_s], bx
      pop rbx ; must restore rbx - wrong value???
      add rsp, 28h
      ret
  Lbra_R_A ENDP
  </code></pre>
  
  <p>I setup the stack with sub rsp, 28h but I'm not sure why and I have no idea what I am allowed to do in that 28h byte area!!!  Is it for me or is it reserved.  However without this my code doesn't even run!!!</p>
  
  <p>Next I preserve the rbx register because it is considered non volatile.  However at the end when I restore rbx it isn't the same as what I saved???</p>
  
  <p>Mid code I save/push the ax register before calling a C function called MemRead8_s (supplied by me).  However the moment I call that function the value of ax now stored on the stack is over written so when i attempt to restore it a few instruction later it is wrong!!!  The rsp value before and after this call seems to be the same so what did calling this function do to the stack?</p>
  
  <p>Can anyone shed some light on what the correct stack setup protocol is and  possibly explain why my stack saves are getting corrupted?</p>