title: x86 ASM: how return to different code segment is implemented?
body: <p>I'm getting acquainted with <a href="http://pacman128.github.io/static/pcasm-book.pdf" rel="nofollow noreferrer">PCASM</a> book, and i don't understand one thing (which may be just my misunderstanding, i'm still new to low-level programming). It is told that c calling convention is something like</p>
  
  <ul>
  <li>push arguments onto stack</li>
  <li>use <code>call</code> instruction to call the subroutine, which, among other things, will push return address on stack</li>
  <li>refer to arguments adding constant offset to <code>ESP</code></li>
  <li>call <code>ret</code> when you done which will pop return address (then optionally pop arguments if you wish) and use it to jump back to the calling code</li>
  </ul>
  
  <p>So far so good, but examples tell that return code is simply four bytes on the stack. That allows to jump anywhere on current code segment, but what if subroutine has to return to another segment? If code segment has to be pushed on the stack the same way as return address, how should one refer to arguments in subroutine (because they will have different offset on near and far return)?</p>