home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / cdactual / demobin / share / program / asm / SHOWARGS.ZIP / SHOWARGS.ASM
Encoding:
Assembly Source File  |  1987-12-15  |  3.9 KB  |  133 lines

  1.          name      showargs
  2.          title     SHOWARGS -- ARGC and ARGV demo
  3. ;
  4. ; SHOWARGS.ASM --- demonstrate command line parsing
  5. ;                  with ARGC.ASM and ARGV.ASM 
  6. ;
  7. ; (C) 1987 Ray Duncan
  8. ;
  9.  
  10. stdin   equ     0               ; MS-DOS handles for
  11. stdout  equ     1               ; standard devices
  12. stderr  equ     2
  13.  
  14. cmdtail equ     80h             ; offset of command tail
  15.                                 ; in program segment prefix
  16.  
  17. cr      equ     0dh             ; ASCII carriage return
  18. lf      equ     0ah             ; ASCII line feed
  19. blank   equ     020h            ; ASCII blank
  20. tab     equ     09h             ; ASCII tab 
  21.  
  22.         extrn   argc:near       ; returns argument count
  23.         extrn   argv:near       ; returns pointer to argument
  24.  
  25. DGROUP  group   _DATA,STACK
  26.  
  27. _TEXT   segment word public 'CODE'
  28.  
  29.         assume  cs:_TEXT,ds:DGROUP,ss:STACK
  30.  
  31. main    proc    far             ; entry point from MS-DOS
  32.  
  33.         mov     ax,DGROUP       ; make our data segment
  34.         mov     ds,ax           ; addressable
  35.  
  36.         mov     psp,es          ; save segment of PSP
  37.  
  38.         mov     bx,cmdtail      ; ES:BX = command tail
  39.         call    argc            ; get number of command 
  40.                                 ;  line arguments       
  41.         mov     pars,ax         ; and save it
  42.  
  43.         mov     bx,offset msg1a ; convert count to ASCII
  44.         call    b2dec           ;  for output
  45.  
  46.         mov     dx,offset msg1  ; display the number of
  47.         mov     cx,msg1_len     ;  command line arguments
  48.         mov     bx,stdout
  49.         mov     ah,40h
  50.         int     21h
  51.  
  52. main1:  mov     ax,count        ; display next argument
  53.         cmp     ax,pars         ; are we all done?      
  54.         je      main2           ; yes, exit
  55.  
  56.         mov     bx,offset msg2a ; no, convert argument number
  57.         call    b2dec
  58.  
  59.         mov     dx,offset msg2  ; and display the number
  60.         mov     cx,msg2_len
  61.         mov     bx,stdout
  62.         mov     ah,40h
  63.         int     21h
  64.  
  65.         mov     ax,count        ; now get the actual argument
  66.         mov     es,psp          ; ES:BX = command tail
  67.         mov     bx,cmdtail
  68.         call    argv
  69.  
  70.         push    ds              ; save our data segment and
  71.         push    es              ;  display argument string
  72.         pop     ds
  73.         mov     dx,bx           ; now DS:DX = argument addr
  74.         mov     cx,ax           ; and CX = argument length
  75.         mov     bx,stdout
  76.         mov     ah,40h
  77.         int     21h
  78.         pop     ds              ; restore our data segment
  79.  
  80.         inc     word ptr count  ; go to next argument
  81.         jmp     main1
  82.  
  83. main2:  mov     ax,4c00h        ; exit to MS-DOS
  84.         int     21h
  85.  
  86. main    endp
  87.  
  88.  
  89. b2dec   proc    near            ; convert binary value 0-99
  90.                                 ;  to decimal ASCII chars.
  91.                                 ; call with 
  92.                                 ; AL = binary data
  93.                                 ; BX = addr. for 2 chars.
  94.         
  95.         aam                     ; divide AL by 10, leaving
  96.                                 ; AH=quotient, AL=remainder
  97.         add     ax,'00'         ; convert to ASCII
  98.         mov     [bx],ah         ; store ten's digit
  99.         mov     [bx+1],al       ; store one's digit
  100.         ret                     ; return to caller
  101.  
  102. b2dec   endp
  103.  
  104. _TEXT   ends
  105.  
  106.  
  107. _DATA   segment word public 'DATA'
  108.  
  109. count   dw      0               ; current command line argument
  110. pars    dw      0               ; total command line arguments
  111. psp     dw      0               ; save segment of PSP
  112.  
  113. msg1    db      cr,lf
  114.         db      'The command line contains '
  115. msg1a   db      'xx arguments'
  116. msg1_len equ $-msg1
  117.  
  118. msg2    db      cr,lf
  119.         db      'Argument '
  120. msg2a   db      'xx is:  '
  121. msg2_len equ $-msg2
  122.  
  123. _DATA   ends
  124.  
  125.  
  126. STACK   segment para stack 'STACK'
  127.  
  128.         dw      64 dup (?)
  129.  
  130. STACK   ends
  131.  
  132.         end     main
  133.