Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, yes and no. In Linux, at the <code>_start:</code> label, <code>argc</code> is at <code>[esp]</code> and <code>argv[0]</code> is at <code>[esp + 4]</code>. If your code works, this must also be true of Mac OSX. By doing <code>-e main</code> on the ld command line, essentially <code>main</code> is lying about its name. It isn't really a "C style main". This label is jumped to, not called. If <code>main</code> (or _main, for 'doze and Mac OSX) is called by "C startup code" (crt2.o), then there's a return address on the stack, so <code>argc</code> is at <code>[esp + 4]</code> and <code>argv[0]</code> is at <code>[esp + 8]</code>. Also, as Tim tells you at news:comp.lang.asm.x86 <code>argv</code> is a <code>**</code> - a "pointer to pointer" - so you also need the <code>mov ebx, [ebx]</code> (a "de-reference"). I'm pretty sure in Windows, our code is called regardless of what we name the entrypoint. Can you get it to work that way?</p> <p>EDIT: Well this has pretty much been beaten to death, and "solved"(?), but I got bored, too. This works in Linux, and "might" be portable.</p> <p><code></p> <pre>; prints its own name (possibly portable?) ; nasm -f elf32 myprog.asm ; nasm -f macho myprog.asm --prefix _ ; nasm -f win32 myprog.asm --prefix _ ; gcc -o myprog myprog.o(bj) (-m32 for 64-bit systems) global main extern printf section .data prog db `Program: %s \n`, 0 section .text main: mov eax, [esp + 8] mov eax, [eax] push eax push prog call printf add esp, 4 * 2 ret ;---------------------- </code></pre>
 

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