home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / PCMAG / VOL7N22.ARC / PP722.ARC / ARGC.ASM next >
Encoding:
Assembly Source File  |  1988-10-06  |  2.7 KB  |  82 lines

  1. ;----------------------------------------------------------------------
  2. ; ARGC.ASM:     Return count of command line arguments.
  3. ;               Treats blanks and tabs as whitespace.
  4. ;
  5. ; Copyright (c) 1988 Ziff Communications Co.
  6. ; PC Magazine * Ray Duncan * 12-27-88
  7. ;
  8. ; Call with:    N/A
  9. ;
  10. ; Returns:      AX    = argument count (always >=1)
  11. ;
  12. ; Uses:         nothing (other registers preserved)
  13. ;----------------------------------------------------------------------
  14.         .286
  15.  
  16. tab     equ     09h             ; ASCII tab
  17. blank   equ     20h             ; ASCII space character
  18.  
  19.         extrn   DosGetEnv:far
  20.  
  21. _TEXT   segment word public 'CODE'
  22.  
  23.         assume  cs:_TEXT
  24.  
  25.         public  argc            ; make ARGC available to Linker
  26.                                 ; local variables
  27. envseg  equ     [bp-2]          ; environment segment
  28. cmdoffs equ     [bp-4]          ; command line offset
  29.  
  30. argc    proc    near
  31.  
  32.         enter   4,0             ; make room for local variables
  33.  
  34.         push    es              ; save original ES,BX, and CX
  35.         push    bx
  36.         push    cx
  37.  
  38.         push    ss              ; get selector for environment
  39.         lea     ax,envseg       ; and offset of command line
  40.         push    ax
  41.         push    ss
  42.         lea     ax,cmdoffs
  43.         push    ax
  44.         call    DosGetEnv       ; transfer to OS/2
  45.         or      ax,ax           ; check operation status
  46.         mov     ax,1            ; force argc >= 1
  47.         jnz     argc3           ; inexplicable failure
  48.  
  49.         mov     es,envseg       ; set ES:BX = command line
  50.         mov     bx,cmdoffs
  51.  
  52. argc0:  inc     bx              ; ignore useless first field
  53.         cmp     byte ptr es:[bx],0
  54.         jne     argc0
  55.  
  56. argc1:  mov     cx,-1           ; set flag = outside argument
  57.  
  58. argc2:  inc     bx              ; point to next character
  59.         cmp     byte ptr es:[bx],0
  60.         je      argc3           ; exit if null byte
  61.         cmp     byte ptr es:[bx],blank
  62.         je      argc1           ; outside argument if ASCII blank
  63.         cmp     byte ptr es:[bx],tab
  64.         je      argc1           ; outside argument if ASCII tab
  65.                                 ; otherwise not blank or tab,
  66.         jcxz    argc2           ; jump if already inside argument
  67.  
  68.         inc     ax              ; else found argument, count it
  69.         not     cx              ; set flag = inside argument
  70.         jmp     argc2           ; and look at next character
  71.  
  72. argc3:  pop     cx              ; restore original BX, CX, ES
  73.         pop     bx
  74.         pop     es
  75.         leave                   ; discard local variables
  76.         ret                     ; return AX = argument count
  77.  
  78. argc    endp
  79.  
  80. _TEXT   ends
  81.         end
  82.