title: Does it matter where the ret instruction is called in a procedure in x86 assembly
body: <p>I am currently learning x86 assembly. Something is not clear to me still however when using the stack for function calls. I understand that the call instruction will involve pushing the return address on the stack and then load the program counter with the address of the function to call. The ret instruction will load this address back to the program counter.</p>
  
  <p>My confusion is, does it matter when the ret instruction is called within the procedure/function? Will it always find the correct return address stored on the stack, or must the stack pointer be currently pointing to where the return address was stored? If that's the case, can't we just use push and pop instead of call and ret?</p>
  
  <p>For example, the code below could be the first on entering the function , if we push different registers on the stack, must the ret instruction only be called after the registers are popped in the reverse order so that after the pop %ebp instruction , the stack pointer will point to the correct place on the stack where the return address is, or will it still find it regardless where it is called? Thanks in advance</p>
  
  <pre><code>push %ebp
  mov %ebp, %esp
  //push other registers
  
  ...
  //pop other registers
  mov %esp, %ebp
  (could ret instruction go here for example and still pop the correct return address?)
  pop %ebp
  ret
  </code></pre>