;; PARSECMD.ASM--A function which returns successively with es:si ;; pointing to null-terminated command-line arguments. When the ;; end of the arguments is reached, returns si = 0 ;; ;; Library source text from "Assembly Language for the IBM PC Family" by ;; William B. Jones, (c) Copyright 1992, 1997, Scott/Jones Inc. ;; .MODEL SMALL PSP SEGMENT AT 0-256 ORG 80h CmdLen DB ? Command DB 127 DUP (?) PSP ENDS .DATA PUBLIC CmdTail ; RedirErr uses this CmdTail DD 0 ; Segment of 0 indicates CmdTail not yet ; initialized (state 1). Seg <> 0 and ; offset = 0 indicates we've reached the ; end of the command line CMDSEG EQU WORD PTR CmdTail + 2 ; Low order first CMDOFF EQU WORD PTR CmdTail .CODE Public ParseCmd ParseCmd PROC push ds push es push bx mov si, @data ; Using si as we needn't save and restore it mov ds, si ASSUME ds : @data, es : NOTHING cmp CMDSEG, 0 jne NotFirst ; Not first call to ParseCmd push ax ; State 1: Use DOS call to get PSP loc mov ah, 62h int 21h mov CMDSEG, bx mov CMDOFF, OFFSET Command mov es, bx ; Check for correct length ASSUME es : PSP mov bl, CmdLen sub bh, bh cmp bx, 126 jle LenOK mov CmdLen, 126 LenOK: mov [Command + bx], 13 ; make sure terminated with CR pop ax NotFirst: les si, CmdTail test si, si jz Done ; Offset 0 indicates no more params ParseLoop: ; State 2: May be more params cmp BYTE PTR es:[si], ' ' je SkipIt cmp BYTE PTR es:[si], 9 ; 9 = TAB jne FindEnd SkipIt: inc si jmp ParseLoop FindEnd: ; found start of token; now find end cmp BYTE PTR es:[si], 13 je NoMore ; Reached end of command line mov bx, si EndLoop: inc bx cmp BYTE PTR es:[bx], ' ' jg EndLoop cmp BYTE PTR es:[bx], 13 ; This is the last token if = mov BYTE PTR es:[bx], 0 ; Null-terminate parameter je NoMore1 ; Note separation of cmp and je!!! inc bx mov CMDOFF, bx ; Prepare for next ParseCmd jmp Done NoMore: mov si, 0 NoMore1: ; Go to State 3 mov CMDOFF, 0 Done: pop bx pop es pop ds ret ParseCmd ENDP END