home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch19 / showargs.asm < prev    next >
Encoding:
Assembly Source File  |  1988-12-12  |  4.8 KB  |  152 lines

  1.     title      SHOWARGS -- ASMHELP.DLL Demo
  2.         page      55,132
  3.     .286
  4.  
  5. ;
  6. ; SHOWARGS.ASM
  7. ;
  8. ; Demonstrates parsing of command line by calls to ASMHELP.DLL.
  9. ;
  10. ; Assemble with:  C> masm showargs.asm;
  11. ; Link with:  C> link showargs,,,os2,showargs
  12. ;
  13. ; Usage is:  C> showargs [argument(s)]
  14. ;
  15. ; Copyright (C) 1988 Ray Duncan
  16. ;
  17.  
  18. stdin   equ     0               ; standard input handle
  19. stdout  equ     1               ; standard output handle
  20. stderr  equ     2               ; standard error handle
  21.  
  22. cr      equ     0dh             ; ASCII carriage return
  23. lf    equ    0ah        ; ASCII linefeed
  24. blank   equ     020h            ; ASCII blank
  25. tab     equ     09h             ; ASCII tab 
  26.  
  27.                                 ; references to ASMHELP.DLL
  28.         extrn   ARGC:far        ; returns argument count
  29.         extrn   ARGV:far        ; returns pointer to argument
  30.  
  31.         extrn   DosWrite:far    ; references to OS/2
  32.         extrn   DosExit:far
  33.         extrn   DosGetEnv:far
  34.  
  35. DGROUP  group   _DATA
  36.  
  37. _DATA   segment word public 'DATA'
  38.  
  39. argno   dw      0               ; receives argument count
  40. argptr  dw      0,0             ; receives argument pointer
  41. arglen  dw      0               ; receives argument length
  42.  
  43. curarg    dw    0        ; current command line argument
  44.  
  45. wlen    dw      0               ; bytes actually written
  46.  
  47. msg1    db      cr,lf
  48.         db      'The command line contains '
  49. msg1a   db      'xx arguments'
  50. msg1_len equ $-msg1
  51.  
  52. msg2    db      cr,lf
  53.         db      'Argument '
  54. msg2a   db      'xx is:  '
  55. msg2_len equ $-msg2
  56.  
  57. _DATA   ends
  58.  
  59.  
  60. _TEXT   segment word public 'CODE'
  61.  
  62.         assume  cs:_TEXT,ds:DGROUP
  63.  
  64. main    proc    far             ; entry point from OS/2
  65.  
  66.                                 ; get number of command
  67.                                 ;  line arguments...
  68.  
  69.         push    ds              ; receives argument count
  70.         push    offset DGROUP:argno
  71.         call    argc            ; call ASMHELP.DLL
  72.  
  73.         mov     ax,argno        ; convert count to ASCII
  74.         mov     bx,offset msg1a ; for output
  75.         call    b2dec
  76.  
  77.                                 ; now display number
  78.                                 ; of arguments...
  79.  
  80.         push    stdout          ; standard output handle
  81.         push    ds              ; address of message
  82.         push    offset DGROUP:msg1
  83.         push    msg1_len        ; length of message
  84.         push    ds              ; receives bytes written
  85.         push    offset DGROUP:wlen
  86.         call    DosWrite        ; transfer to OS/2
  87.  
  88. main1:                          ; display next argument...
  89.  
  90.         mov     ax,curarg       ; are we all done?
  91.         cmp     ax,argno
  92.         je      main2           ; yes, exit
  93.  
  94.         mov     bx,offset msg2a ; no, convert argument number
  95.         call    b2dec
  96.  
  97.                                 ; display argument number...
  98.         push    stdout          ; standard output handle
  99.         push    ds              ; address of message
  100.         push    offset DGROUP:msg2
  101.         push    msg2_len        ; length of message
  102.         push    ds              ; receives bytes written
  103.         push    offset DGROUP:wlen
  104.         call    DosWrite        ; transfer to OS/2
  105.  
  106.                                 ; get argument pointer...
  107.         push    curarg          ; argument number
  108.         push    ds              ; receives pointer
  109.         push    offset DGROUP:argptr
  110.         push    ds              ; receives length
  111.         push    offset DGROUP:arglen
  112.         call    argv            ; call ASMHELP.DLL
  113.  
  114.                                 ; display the argument...
  115.         push    stdout          ; standard output handle
  116.         push    argptr+2        ; pointer to argument
  117.         push    argptr
  118.         push    arglen          ; length of argument
  119.         push    ds              ; receives bytes written
  120.         push    offset DGROUP:wlen
  121.         call    DosWrite        ; transfer to OS/2
  122.  
  123.         inc     word ptr curarg ; go to next argument
  124.         jmp     main1
  125.  
  126. main2:                          ; common exit point
  127.         push    1               ; terminate all threads
  128.         push    0               ; return code = zero
  129.         call    DosExit         ; transfer to OS/2
  130.  
  131. main    endp
  132.  
  133.  
  134. b2dec   proc    near            ; convert binary value 0-99
  135.                                 ;  to two decimal ASCII 
  136.                                 ; call with 
  137.                                 ; AL = binary data
  138.                 ; BX = address for 2 chars.
  139.         
  140.         aam                     ; divide AL by 10, leaving
  141.                 ; AH = quotient, AL = remainder
  142.         add     ax,'00'         ; convert to ASCII
  143.     mov    [bx],ah     ; store tens digit
  144.     mov    [bx+1],al    ; store ones digit
  145.         ret                     ; return to caller
  146.  
  147. b2dec   endp
  148.  
  149. _TEXT   ends
  150.  
  151.         end     main            ; defines entry point
  152.