News:

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

ParseCL.asm

Started by Mark Jones, July 27, 2006, 04:01:59 PM

Previous topic - Next topic

Mark Jones

Hello friends, here is a simple little application to obtain and separate the executable filename and arguments from the command-line. Perhaps the student can be asked to modify the code to handle each argument. (No pain, no gain, right?) :bg  ParseCL should work in both Win32 GUI mode (start-->run-->ParseCl.exe [args] [args...]) or in console (CUI) mode. Link with /subsystem:windows to prevent a CUI window from appearing while it executes.

GeneSysRt.inc should be available in the next build.


; ParseCL by Mark Jones 2006 (596n9xz02 AT sneakemail D0T com)
; GeneSys tutorial, separates command-line filename from arguments

include \GeneSys\include\GeneSysRt.inc  ; standard compile-time libs

.const
    szTitle         db  "ParseCL",0     ; dialog caption
    szFormat        db  "Executed File: %s",13,10,"Arguments: %s",0
.data?
    szFile          db  MAX_PATH dup(?) ; executed filename
    szArgs          db  1759 dup(?)     ; arguments
    szBuff          db  2048 dup(?)     ; result
.data

.code
start:

    push esi                            ; save esi
    push edi                            ; and edi
    cld                                 ; direction flag must be clear

    call GetCommandLine                 ; returns pointer to string
    mov esi,eax                         ; esi = "\file.exe" arg, arg...
    lea edi,szFile                      ; edi = filename buffer

@@: movsb                               ; byte edi<--esi, both++
    cmp byte ptr [esi-1],00h            ; was char == null?
    jz ready                            ; if so, done
    cmp byte ptr [esi-1],20h            ; was last char copied a space?
    jnz @B                              ; loop until space copied
    lea edi,szArgs                      ; edi = arguments

@@: movsb                               ; copy argument bytes
    cmp byte ptr [esi-1],00             ; wait for the copied null
    jnz @B                              ; loop until done
    pop edi                             ; restore edi
    pop esi                             ; and esi
ready:

    invoke wsprintf,addr szBuff,addr szFormat,addr szFile,addr szArgs
    invoke MessageBox,0,addr szBuff,addr szTitle,MB_OK
    invoke ExitProcess,0                ; exit gracefully with errorlevel 0

end start
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Vortex

Mark, thanks for sending the algo. A question, why do you need 1787 bytes for the argumets buffer? Is it the result of a calculation?

Mark Jones

Oh that is a little bug. It should be 2048 - MAX_PATH - szArgs - (bytes added by szFormat) - 1 so that all the text will fit into szBuff with one null left untouched. I doubt anyone will try to pass a string of arguments that long, but it should work if they do. :U
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

lingo

I'm wondering why to preserve esi and edi registers in non CALLBACK procĀ  :wink

In that relation I can't find any answer of the question "Why preserve registers ?"
here: http://x86assembly.codegurus.org/Registers.html