home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PPOS2.ZIP / ARGC.ASM next >
Assembly Source File  |  1988-09-13  |  3KB  |  87 lines

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