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

  1.         title     SHOWARGS -- ARGC and ARGV demo
  2.         page      55,132
  3.         .286
  4. ;
  5. ; SHOWARGS.ASM --- demonstrate command line parsing
  6. ;                  with ARGC.ASM and ARGV.ASM 
  7. ;
  8. ; OS/2 version 9/2/87  (C) 1987 Ray Duncan
  9. ;
  10.  
  11. stdin   equ     0               ; standard input handle
  12. stdout  equ     1               ; standard output handle
  13. stderr  equ     2               ; standard error handle
  14.  
  15. cr      equ     0dh             ; ASCII carriage return
  16. lf      equ     0ah             ; ASCII line feed
  17. blank   equ     020h            ; ASCII blank
  18. tab     equ     09h             ; ASCII tab 
  19.  
  20.         extrn   argc:near       ; returns argument count
  21.         extrn   argv:near       ; returns argument pointer
  22.  
  23.                                 ; OS/2 API functions
  24.         extrn   DosWrite:far    ; write file or device
  25.         extrn   DosExit:far     ; terminate process
  26.  
  27.  
  28. DGROUP  group   _DATA
  29.  
  30. _DATA   segment word public 'DATA'
  31.  
  32. curarg  dw      0               ; current command line argument
  33. totargs dw      0               ; total command line arguments
  34.  
  35. wlen    dw      ?               ; bytes actually written
  36.  
  37. msg1    db      cr,lf
  38.         db      'The command line contains '
  39. msg1a   db      'xx arguments'
  40. msg1_len equ $-msg1
  41.  
  42. msg2    db      cr,lf
  43.         db      'Argument '
  44. msg2a   db      'xx is:  '
  45. msg2_len equ $-msg2
  46.  
  47. _DATA   ends
  48.  
  49.  
  50. _TEXT   segment word public 'CODE'
  51.  
  52.         assume  cs:_TEXT,ds:DGROUP
  53.  
  54. main    proc    far             ; entry point from OS/2
  55.  
  56.         call    argc            ; get and save number of 
  57.         mov     totargs,ax      ; command line arguments
  58.  
  59.         mov     bx,offset msg1a ; convert argument count 
  60.         call    b2dec           ; to ASCII for output
  61.  
  62.                                 ; display argument count
  63.         push    stdout          ; standard output handle
  64.         push    ds              ; address of message
  65.         push    offset DGROUP:msg1
  66.         push    msg1_len        ; length of message
  67.         push    ds              ; receives bytes written
  68.         push    offset DGROUP:wlen
  69.         call    DosWrite        ; transfer to OS/2
  70.  
  71. main1:  mov     ax,curarg       ; display next argument
  72.         cmp     ax,totargs      ; are we all done?      
  73.         je      main2           ; yes, exit
  74.  
  75.         mov     bx,offset msg2a ; no, convert argument 
  76.         call    b2dec           ; number to ASCII
  77.  
  78.                                 ; display argument number
  79.         push    stdout          ; standard output handle
  80.         push    ds              ; address of message
  81.         push    offset DGROUP:msg2
  82.         push    msg2_len        ; length of message
  83.         push    ds              ; receives bytes written
  84.         push    offset DGROUP:wlen
  85.         call    DosWrite        ; transfer to OS/2
  86.  
  87.         mov     ax,curarg       ; now get actual argument
  88.         call    argv            ; ES:BX=addr,AX=length
  89.  
  90.         push    stdout          ; standard output handle
  91.         push    es              ; command argument address
  92.         push    bx
  93.         push    ax              ; command argument length
  94.         push    ds              ; receives bytes written
  95.         push    offset DGROUP:wlen
  96.         call    DosWrite        ; transfer to OS/2
  97.  
  98.         inc     word ptr curarg ; go to next argument
  99.         jmp     main1
  100.  
  101. main2:  push    1               ; terminate process
  102.         push    0               ; return code = zero
  103.         call    DosExit         ; final exit to OS/2
  104.  
  105. main    endp
  106.  
  107.  
  108. b2dec   proc    near            ; convert binary 0-99
  109.                                 ; to decimal ASCII
  110.                                 ; call with 
  111.                                 ; AL = binary data
  112.                                 ; BX = addr. for 2 chars.
  113.         
  114.         aam                     ; divide AL by 10, leaving
  115.                                 ; AH=quotient, AL=remainder
  116.         add     ax,'00'         ; convert to ASCII
  117.         mov     [bx],ah         ; store ten's digit
  118.         mov     [bx+1],al       ; store one's digit
  119.         ret                     ; return to caller
  120.  
  121. b2dec   endp
  122.  
  123. _TEXT   ends
  124.  
  125.         end     main            ; defines entry point
  126.