flat assembler
Message board for the users of flat assembler.

Index > Windows > Trying to get the command line

Author
Thread Post new topic Reply to topic
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY 15 Feb 2006, 21:14
Code:
include '%fasminc%\win32ax.inc'

main:
        invoke GetCommandLine ;command line looks like "c:\prog.exe something;"
        mov esi,eax
        add esi,2 ;Get rid of "
@@:
        cmp byte [esi], '"' ; Find ending "
        je GetPar
        inc esi 
        jmp @b
        add esi,2 ;Jump the space
        mov edi,buf
GetPar:
        movsb
        cmp byte[esi],';' ;Found end of arg?
        jnz GetPar ;No? Find it!
        inc esi
        mov byte[esi],0 ;Put an 0 at end of string
        invoke MessageBox,0,buf,buf,0 ;and display it
        exit:
        ret
        buf rb 200
.end main
    

What's wrong? It compiles OK, but it doesn't run OK.
I tried it on OllyDbg and it shows some error when it finds a space in command line, why?
Post 15 Feb 2006, 21:14
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 15 Feb 2006, 21:19
Well, you're testing for ';' character AFTER you test the '"' character.. I think that might be the problem, or did I misunderstand something?

EDIT: what's with the movsb there? edi is not initialized to anything there until that point.. maybe you wanted lodsb?

ps: i'm not sure, but the command line is not quoted, or is it?
ps2: a decent command line reader ignores spaces.. spaces can be either tabs, spaces or both. and you can have multiple space characters one after another, not just one. I think you gotta redesign this
Post 15 Feb 2006, 21:19
View user's profile Send private message Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY 15 Feb 2006, 21:41
I think it was just bug in my code.
Now it works:
Code:
include '%fasminc%\win32ax.inc'

main:
invoke GetCommandLine ;get cmd line. Looks like: "prog something"
mov esi,eax ;put it in esi
cmp byte[esi],'"' ;if user didn't provide command line. eg: clicked on the program icon, it will be quoted...
jz exit ;...so exit!
@@:
cmp byte[esi],' ' ;is it a space?
jz @f ;Yes? found!
inc esi
jmp @b ;Otherwise try again
@@:
invoke MessageBox,0,esi,esi,0 ;found argument, display it!
exit:
ret ;exit program
.end main
    

Yes, the command line is quoted. Now checked for this. Anyway, it was just a test for a bigger program, and I'll design it better.
Post 15 Feb 2006, 21:41
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 15 Feb 2006, 21:46
it's much more logical right now Very Happy
I did a command line reader long ago only for test purposes, but it is in C.. if I ever program something in asm with cmd line, i'll probably do the conversion.
Post 15 Feb 2006, 21:46
View user's profile Send private message Reply with quote
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend 16 Feb 2006, 14:13
I'm not 100% sure, but in win 98 if commandline had no white characters there was no quoteing characters. I tested it now on win xp and these chars are always present, but in win 9x? I can't say for sure
Post 16 Feb 2006, 14:13
View user's profile Send private message Visit poster's website Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 16 Feb 2006, 17:29
In Win9x I think is like this: if you put commands enclosed with quotes, then they're gonna be there, otherwise they won't!
Post 16 Feb 2006, 17:29
View user's profile Send private message Reply with quote
Sem



Joined: 05 Mar 2005
Posts: 8
Sem 17 Feb 2006, 06:05
Work with UNICODE string and use
Code:
LPWSTR *CommandLineToArgvW(          
    LPCWSTR lpCmdLine,
    int *pNumArgs
);
    
Post 17 Feb 2006, 06:05
View user's profile Send private message Reply with quote
0x4e71



Joined: 25 Feb 2004
Posts: 50
0x4e71 18 Feb 2006, 13:46
Or..

..you can always use __GetMainArgs from old clunky CRTDLL.DLL. Very, very handy.

L
Post 18 Feb 2006, 13:46
View user's profile Send private message Reply with quote
chris



Joined: 05 Jan 2006
Posts: 62
Location: China->US->China->?
chris 22 Feb 2006, 05:00
or __getmainargs and __wgetmainargs (UNICODE) in msvcrt.dll

TO 0x4e71: I think __GetMainArgs calls __getmainargs with envp set to 0, right?
Post 22 Feb 2006, 05:00
View user's profile Send private message Reply with quote
0x4e71



Joined: 25 Feb 2004
Posts: 50
0x4e71 22 Feb 2006, 17:56
Hi,
no actually there is a cmdline parser in crtdll itself and __GetMainArgs does take envp as an argument. Sorry I did not post the prototype for __GetMainArgs before. For clarity, here it is:
Code:
__GetMainArgs( int *argc, char ***argv, char ***envp, int wildcard_flag )
    


To firsttime users: both __getmainargs and __GetMainArgs are __cdecl of course so the stack has to be cleaned up after the call.

+L
Post 22 Feb 2006, 17:56
View user's profile Send private message Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex 22 Feb 2006, 18:52
I coded a procedure to get the command line parameters in console applications :

Code:
proc ParseCmdLine,buffer
        push    esi
        push    edi
        invoke  GetCommandLine
@@:
        lea     edx,[eax-1]
        xor     eax,eax
        mov     esi,[buffer]
        lea     edi,[esi+256]
        mov     ch,32
scan:
        inc     edx
        mov     cl,byte [edx]
        or      cl,cl
        jz      finish
        cmp     cl,32
        je      scan
        inc     eax
        mov     [esi],edi
        add     esi,4
restart:
        mov     cl,byte [edx]
        or      cl,cl
        jz      finish
        cmp     cl,ch
        je      end_of_line
        cmp     cl,34
        jne     @f
        xor     ch,32
        jmp     next_char
@@:     
        mov     byte [edi],cl
        inc     edi
next_char:
        inc     edx
        jmp     restart
end_of_line:
        mov     byte [edi],0
        inc     edi
        jmp     scan    
finish:
        pop     edi
        pop     esi
        ret
endp    


Description:
Download
Filename: Cmdline2.zip
Filesize: 2.78 KB
Downloaded: 243 Time(s)


_________________
Code it... That's all...
Post 22 Feb 2006, 18:52
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.