-
AT&T Assembler
I'm learning AT&T syntax assembly, and as a little exercise want to write a program that outputs the number of command line arguments [including program name].
As I understand it, the number of args [argc to C gurus] is the first value to be pushed to the stack when a program executes?
Here's what I have so far:
Code:
.section .data
argnum:
.ascii ""
.section .text
.globl _start
_start:
movl $4, %eax
movl $1, %ebx
movl (%esp), %edx
movl %edx, argnum
movl argnum, %ecx
movl $2, %edx
int $0x80
movl $1, %eax
xorl %ebx, %ebx
int $0x80
It compiles and runs fine, but nothing visible is output. I think it might have something to do with sys_write not outputting numbers, so that's why I used an ascii var [argnum], with no avail.
-
Some points
1. .ascii ""
Does this reserve enough space - it seems like it's just 1 byte containing \0 to me
2. movl %edx, argnum
a) you need to convert to ascii, say by adding '0' to it.
b) you need to move a byte, not a long
Note this only works for argc in the range 0..9, for larger numbers, the process gets more complicated.
3. movl argnum, %ecx
I'm guessing you wanted to move the address here, and not the contents?
-
Thanks for your help I'll have a fiddle and post up any other problems :)
-
Hmm still no joy - I movb'd instead, and it [as] warned me it was using %dl instead. I now move the address of argnum into %ecx, but I don't know what you mean by converting it to ascii (I don't know how) :confused:
As you can imagine, here it is now:
Code:
.section .data
argnum:
.ascii ""
.section .text
.globl _start
_start:
movl $4, %eax
movl $1, %ebx
movl (%esp), %edx
movb %edx, argnum
movl (argnum), %ecx
movl $2, %edx
int $0x80
movl $1, %eax
xorl %ebx, %ebx
int $0x80
-
Random guesses....
.ascii "0" # space for a single char, which has a \0 following it.
addb %edx,$48 # convert 0 into '0' say
movb %edx, argnum