home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / cdactual / demobin / share / program / Asm / ARGC.ZIP / ARGC.ASM
Encoding:
Assembly Source File  |  1987-12-15  |  1.9 KB  |  59 lines

  1.          name      argc
  2.          title     ARGC --- return argument count
  3. ;
  4. ; ARGC.ASM: return count of command line arguments.
  5. ; Treats blanks and tabs as whitespace, carriage 
  6. ; return as terminator.
  7. ;
  8. ; (C) 1987 Ray Duncan
  9. ; Call with:  ES:BX = command line
  10. ;
  11. ; Returns:    AX    = argument count (always >=1)
  12. ;             Other registers preserved
  13. ;
  14.  
  15. cr      equ     0dh             ; ASCII carriage return
  16. lf      equ     0ah             ; ASCII line feed
  17. tab     equ     09h             ; ASCII tab
  18. blank   equ     20h             ; ASCII space character
  19.  
  20. _TEXT   segment word public 'CODE'
  21.  
  22.         assume  cs:_TEXT
  23.  
  24.         public  argc            ; make ARGC available to Linker
  25.  
  26. argc    proc    near            ; count command line arguments
  27.  
  28.         push    bx              ; save original BX and CX 
  29.         push    cx              ;  for later
  30.         mov     ax,1            ; force count >= 1
  31.  
  32. argc1:  mov     cx,-1           ; set flag = outside argument
  33.  
  34. argc2:  inc     bx              ; point to next character 
  35.         cmp     byte ptr es:[bx],cr
  36.         je      argc3           ; exit if carriage return
  37.         cmp     byte ptr es:[bx],blank
  38.         je      argc1           ; outside argument if ASCII blank
  39.         cmp     byte ptr es:[bx],tab    
  40.         je      argc1           ; outside argument if ASCII tab
  41.  
  42.                                 ; otherwise not blank or tab,
  43.         jcxz    argc2           ; jump if already inside argument
  44.  
  45.         inc     ax              ; else found argument, count it
  46.         not     cx              ; set flag = inside argument
  47.         jmp     argc2           ; and look at next character
  48.  
  49. argc3:  pop     cx              ; restore original BX and CX
  50.         pop     bx
  51.         ret                     ; return AX = argument count
  52.  
  53. argc    endp
  54.  
  55. _TEXT   ends
  56.  
  57.         end     
  58.