News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

how to make a program take arguments on startup?

Started by Ani_Skywalker, July 11, 2011, 09:40:19 AM

Previous topic - Next topic

Ani_Skywalker

Hi all,

I want to be able to start the app through cmd with arguments, like: "app arg1 arg2 arg3" and wonder what the equivalent of following would look like in asm?

int main (int argc, char *argv[]) {

bla bla...

}




Ani_Skywalker

Thank you, that link was very useful. And sorry, I should've searched for the topic before starting a new thread :(

A question about the linked thread, I saw jj2007 wrote this piece there:

include \masm32\include\masm32rt.inc

.data?
buffer db MAX_PATH dup(?)

.code
start: xor ebx, ebx
.Repeat
inc ebx
invoke GetCL, ebx, offset buffer
.Break .if eax!=1
print "arg #"
print str$(ebx), " = ", 9
print offset buffer, 13, 10
.Until 0
print "BYE"
exit

end start


What is the difference between GetCL and GetCommandLine?

Vortex

Hi Ani,

Here is an example for you :

include CmdLineParam.inc

.data

_format     db 'Command line parameter %d = %s',13,10,0

.data?

buffer      db 256 dup(?)

.code

main PROC C USES ebx esi edi argc:DWORD,argv:DWORD

    xor     edi,edi

    mov     ebx,argc    ; number of command line parameters
    mov     esi,argv    ; pointer to the array of command line parameters

@@:

    add     edi,1
    invoke  wsprintf,ADDR buffer,ADDR _format,edi,DWORD PTR [esi]
    invoke  StdOut,ADDR buffer
    add     esi,4
    dec     ebx
    jnz     @b

    ret

main ENDP

END


A quick test :


CmdLineParam.exe hello "this is a test" 123

Command line parameter 1 = CmdLineParam.exe
Command line parameter 2 = hello
Command line parameter 3 = this is a test
Command line parameter 4 = 123



jj2007

Quote from: Ani_Skywalker on July 11, 2011, 09:48:59 AM
What is the difference between GetCL and GetCommandLine?

GetCommandLine is a Win32 API call and returns one complete commandline (which you would have to parse "by hand").
GetCL is a Masm32 routine - see \masm32\help\masmlib.chm:

QuoteGetCl will retreive the argument specified in the procedure call and return the argument in the buffer specified. It differs slightly in its implementation in that it treats the path and name of the application making the call as arg 0.

Arguments are specified from arg 1 up to the number of command line arguments available. The procedure supports the retrieval of arguments that are enclosed in quotation marks and will return the argument with the quotation marks removed.

Parameters

1. ArgNum Argument number.
2. ItemBuffer Address of buffer to receive the argument if it exists.

Return values

1 = successful operation
2 = no argument exists at specified arg number
3 = non matching quotation marks
4 = empty quotation marks

Parsing commandlines is not trivial, and the rules are not always clear. Here is an example of what GetCL and MasmBasic return for the following commandline:
This is a long command line with "files in quotes" and tabs and dots (....) etc... have fun parsing it!

MasmBasic:
Arg 1 is [This]
Arg 2 is [is]
Arg 3 is [a]
Arg 4 is [long]
Arg 5 is [command]
Arg 6 is [line]
Arg 7 is [with]
Arg 8 is [files in quotes]
Arg 9 is [and   tabs]
Arg 10 is [and]
Arg 11 is [dots]
Arg 12 is [(....)]
Arg 13 is [etc...]
Arg 14 is [have]
Arg 15 is [fun]
Arg 16 is [parsing]
Arg 17 is [it!]

Masm32:
Arg 1 is [This]
Arg 2 is [is]
Arg 3 is [a]
Arg 4 is [long]
Arg 5 is [command]
Arg 6 is [line]
Arg 7 is [with]
Arg 8 is [files in quotes]
Arg 9 is [and]
Arg 10 is [tabs]
Arg 11 is [and]
Arg 12 is [dots]
Arg 13 is [(....)]
Arg 14 is [etc...]
Arg 15 is [have]
Arg 16 is [fun]
Arg 17 is [parsing]
Arg 18 is [it!]


Source attached. I guess I will accept tabs as delimiters in the next MB version...

Ani_Skywalker

Thank you all for very comprehensive answers.

@Vortex: It was a solution like yours I was looking for :)

bomz

WHILE and REPEAT change by compiler's to CMP?

jj2007

Quote from: bomz on July 11, 2011, 10:44:40 AM
WHILE and REPEAT change by compiler's to CMP?

More precisely,
xor ebx, ebx
.Repeat
inc eax
.Until eax>ebx
is equivalent to
xor ebx, ebx
@@: inc eax
cmp eax, ebx
jbe @B


Do not confuse the dot versions (.Repeat, .While) with the non-dot version (Repeat While etc) used in macros. The latter influence the creation of code.


Vortex

Hi Ani,

Originally, my command-line parser was intended to be the main part of my tiny C run-time startup library. It can be used with assembly modules. Feel free to modify \ improve my crt0 code, you can add more features if you wish.