Kelo
Kelo

Reputation: 463

Can't get hold of command line arguments

I am very new to assembly but I already have ran into a problem. This is the start of my code. printf prints just constant 2 no matter what the argument is.

section .data
msg: db "n = %d ",10,0

section .text
global _main  
extern _printf   


_main:

push ebp
mov  ebp, esp
sub  esp, 16

push DWORD [ebp +8]
push msg    
call _printf

I was told commandline arguemnts were supposed to be +8,+12 and so on from the pointer, but this doesn't work. Right now it just prints n=2.

Upvotes: 0

Views: 143

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50775

Try this, it shoud print the number of arguments and the first argument

section .data
msg: db ""nbargs = %d, 1st argument = %s",10,0

section .text
global _main  
extern _printf   

_main:

push ebp
mov  ebp, esp
sub  esp, 16

mov  eax,dword ptr [ebp+12]
mov  ecx,dword ptr [eax+4]
push ecx
mov  edx,dword ptr [ebp+8]
push edx
push msg
call printf
...

If your program is called myprog, you shoud for example get this output:

myprog myargument
nbargs = 2, 1st argument = myargument

Upvotes: 1

Related Questions