home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch03 / argc.asm next >
Encoding:
Assembly Source File  |  1988-12-12  |  2.6 KB  |  91 lines

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