I have three recommendations:
- Check out http://www.nasm.us/doc/nasmdoc9.html if you haven't already,
- Minimize the code that attempts to solve your immediate problem, and
- Examine the assembly generated by a C compiler if possible when you're stuck.
For getting argv, I can simply return the ASCII code for the first character in argv[1] from my program to avoid the system call. The system call is a different problem than getting argv, so avoiding it focuses attention on the problem at hand.
Then I can compile a minimal C program and examine the generated assembly. Reading AT&T syntax assembly isn't that bad if you remember that when you're going to AT&T, which is in New Jersey, the destination is on the right side of the U.S. ;)
tmp$ cat main.c
int main(int argc, char *argv[])
{
if (argc > 1)
return argv[1][0];
return 0;
}
tmp$ gcc -Wall -save-temps main.c
The program just returns the ASCII code for the first character in argv[1]. The 't' is 116.
tmp$ ./a.out test
tmp$ echo $?
116
tmp$
Examining the generated assembly, I see that it doesn't use pops but rather just loads registers based on the position of stack parameters relative to the base pointer, ebp. I find I like this style of using mov with the base pointer.
I don't use pop the way you're trying to do, so someone else might comment on how to do it using pop.
I've annotated the assembly a bit with comments about what I think is going on. Corrections are welcome.
tmp$ cat main.s
.file "main.c"
.text
.globl main
.type main,@function
main:
pushl %ebp ; push the callers base pointer onto the stack
movl %esp, %ebp ; save esp into the base pointer
subl $8, %esp ; make some room on the stack for main ...
andl $-16, %esp ; but pad to an aligned stack pointer
movl $0, %eax
subl %eax, %esp ; dunno why gcc subtracts 0 from stack pointer
cmpl $1, 8(%ebp) ; compare 1 and argc, which is 8 past the base pointer
jle .L2 ; jump to .L2 if argc <= 1
movl 12(%ebp), %eax ; fetch argv into eax
addl $4, %eax ; skip the first 32 bits at that address
movl (%eax), %eax ; fetch address from the resulting address
movsbl (%eax),%eax ; load byte from that address into eax
movl %eax, -4(%ebp) ; put that byte onto the stack (see note 1.)
jmp .L1
.L2:
movl $0, -4(%ebp)
.L1:
movl -4(%ebp), %eax ; load return value from stack (see note 1.)
leave
ret
.Lfe1:
.size main,.Lfe1-main
.ident "GCC: (GNU) 3.2.2"
tmp$
I don't have nasm handy on a 32-bit machine, and x86_64 calling conventions are not the same as the ones you're dealing with, so I didn't translate this assembly into nasm syntax.
- The compiler will do some stuff that makes you scratch your head and wonder, "Is that smart or dumb?" In this case, I think probably I would just use eax itself instead of the stack for holding the return value, but sometimes googling is educational. I learned why gcc sometimes likes to zero a register with "xor reg, reg", for example, by googling.