Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's a very similar exercise outlined here: <a href="http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax" rel="noreferrer">http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax</a></p> <p>You've figured out most of it -- I'll just make additional notes for emphasis and additions.</p> <p><CODE>__main</CODE> is a subroutine in the GNU standard library that takes care of various start-up initialization. It is not strictly necessary for C programs but is required just in case the C code is linking with C++.</p> <p><CODE>_main</CODE> is your main subroutine. As both <CODE>_main</CODE> and <CODE>__main</CODE> are code locations they have the same storage class and type. I've not yet dug up the definitions for <CODE>.scl</CODE> and <CODE>.type</CODE> yet. You may get some illumination by defining a few global variables.</p> <p>The first three instructions are setting up a stack frame which is a technical term for the working storage of a subroutine -- local and temporary variables for the most part. Pushing <CODE>ebp</CODE> saves the base of the caller's stack frame. Putting <CODE>esp</CODE> into <CODE>ebp</CODE> sets the base of our stack frame. The <CODE>andl</CODE> aligns the stack frame to a 16 byte boundary just in case any local variables on the stack require 16 byte alignment (for the x86 SIMD instructions require that alignment, but alignment does speed up ordinary types such as <CODE>int</CODE>s and <CODE>float</CODE>s.</p> <p>At this point you'd normally expect <CODE>esp</CODE> to get moved down in memory to allocate stack space for local variables. Your <CODE>main</CODE> has none so gcc doesn't bother.</p> <p>The call to <CODE>__main</CODE> is special to the main entry point and won't typically appear in subroutines.</p> <p>The rest goes as you surmised. Register <CODE>eax</CODE> is the place to put integer return codes in the binary spec. <CODE>leave</CODE> undoes the stack frame and <CODE>ret</CODE> goes back to the caller. In this case, the caller is the low-level C runtime which will do additional magic (like calling <CODE>atexit()</CODE> functions, set the exit code for the process and ask the operating system to terminate the process.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload