Alex
Alex

Reputation: 876

Accessing cmdline arguments on 32-bit assembly

So I'm currently trying to get the command line arguments using masm, here is what I've done so far:

.386
.MODEL FLAT

includelib kernel32.lib
includelib msvcrt.lib

__getmainargs PROTO C :DWORD
printf        PROTO C :DWORD

.data
            str_argv     db "argv[1]: %s",0
.fardata?
            argc    dd ?    
            argv    db 255 DUP(?)
            env     dd  ?

.code
START:
            push 0
            push OFFSET env
            push OFFSET argv
            push OFFSET argc
            call  __getmainargs
            add   esp, 4*4 ;4 args
 
            push  [edi]
            push  OFFSET str_argv
            call  printf
            add   esp, 4 * 2 ;2 args

            ret
END START

However this returns me a garbage string that does not correspond to the cmdline argument, in this case the module name.

Any ideas why this doesn't work? -> Also are there any other alternatives for accessing cmdline args?

Thanks in advance

Upvotes: 0

Views: 334

Answers (1)

Michael
Michael

Reputation: 58447

You're pushing [edi] without having put the correct value into edi.

Change the declaration of argv to argv dd ?.

Then change the push [edi] to:

mov edi,argv
push dword ptr [edi+4]

And make sure you use the /SUBSYSTEM:CONSOLE option when linking.

Upvotes: 3

Related Questions