home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / switch.zip / SWITCH.ASM next >
Assembly Source File  |  1988-09-05  |  4KB  |  113 lines

  1. comment    |
  2.  
  3.     This program demonstrates one way to read single-letter switches from
  4.     the command line. The command line switches are received by the
  5.     program from the program segment prefix (PSP) beginning at offset 80h.
  6.  
  7.     The program uses a single memory word (one bit per switch) to store
  8.     information for 16 switches, assumed to be /A through /P. All switches
  9.     are treated as toggles. The code commenting, wording of printing
  10.     messages and some of the code itself have been changed in a number of
  11.     places.
  12.  
  13.     Written for MASM 5.0 by Hardin Brothers for PCResource. The original
  14.     appeared in the April 1988 issue of their magazine. Entire contents of
  15.     the April 1988 issue (C) Copyright 1988 by
  16.                 IDG Communications/Peterborough, Inc.
  17.  
  18.     Hardin Brothers is a freelance programmer and technical writer. Write
  19.     to him at 280 N. Campus Ave., Upland, CA  91786. Enclose a self-
  20.     addressed, stamped envelope for a reply.
  21.  
  22.     |
  23.  
  24. LF    equ    0Ah    ; linefeed char
  25. CR    equ    0Dh    ; carriage return char
  26. STDOUT    equ    1    ; standard output device
  27.  
  28. EXIT    macro    val
  29.     mov    AH, 4Ch        ; INT 21h service 4Ch: exit from program
  30.                 ; also returns a value. replaces old INT 20h.
  31.     mov    AL, val        ; return value byte
  32.     int    21h
  33.     endm
  34.  
  35.     .MODEL    SMALL
  36.     .STACK
  37.  
  38.     .DATA
  39.  
  40. swtch    dw    0        ; can, of course, be initialized so some of
  41.                 ; the switches default ON
  42.  
  43. error$        db    CR, LF, 'Unknown switch detected', CR, LF
  44. lerror        equ    $-error$
  45.  
  46. rpt$        db    CR, LF, 'The current switch char is: '
  47. swtchch        db    ?    ; storage space for the char
  48.         db    CR, LF, CR, LF, 'The following switches are set: '
  49. aswtch        db    'ABCDEFGHIJKLMNOP', CR, LF
  50. lrpt        equ    $-rpt$
  51.  
  52.     .CODE
  53.  
  54. start:    mov    AX, @data
  55.     mov    DS, AX        ; initialize data seg register
  56.     cld            ; reset direction flag
  57.     mov    AX, 3700h    ; INT 21h service 37h: reserved for DOS,
  58.                 ;   undocumented, request current switch char
  59.     int    21h        ; call DOS service
  60.     mov    swtchch, DL    ; save char
  61.     mov    AL, DL        ; copy to AL for searching
  62.     mov    CL, ES:[80h]    ; get line length from PSP
  63.     sub    CH, CH        ; CX is the command line length
  64.     test    CX, CX        ; anything on the cmdline ?
  65.     jz    report        ; skip to switch settings report if nothing
  66.     mov    DI, 81h        ; start of cmdline area
  67.     mov    BX, swtch    ; get word of bit toggles
  68.  
  69. inloop:    repne    scasb        ; search for first cmdline switch
  70.     jcxz    report        ; skip to report if no more switches
  71.     push    CX
  72.     push    AX        ; save byte count and switch char
  73.     mov    AL, ES:[DI]    ; get cmdline switch
  74.     and    AL, 0DFh    ; force uppercase
  75.     sub    AL, '@'        ; 'A' = '@'+ 1: put AL in range 1 to >= 16
  76.     cmp    AL, 10h        ; check out of range ( AL > 16 )
  77.     ja    error        ; skip to error if out
  78.     mov    CL, AL        ; save switch number
  79.     sub    AX, AX        ; just as fast as XOR AX, AX: set AX = 0
  80.     stc            ; set carry flag
  81.     rcl    AX, CL        ; rotate carry to switch bit position in AX
  82.     xor    BX, AX        ; toggle switch bit
  83.     mov    swtch, BX    ; save result
  84.     pop    AX
  85.     pop    CX        ; restore switch char and byte count
  86.     jmp    inloop        ; continue searching
  87.  
  88. error:    lea    DX, error$    ; addr DS:DX points to error message
  89.     mov    CX, lerror    ; get message byte count
  90.     jmp    short    endit    ; skip to endit
  91.  
  92. report:    lea    SI, aswtch    ; addr DS:SI points to ASCII switch names
  93.     clc            ; clear carry flag
  94.     mov    CL, 10h        ; get number of switches
  95.  
  96. outlp:    rcr    BX, 1        ; rotate one bit to carry
  97.     jc    out_2        ; skip to out_2 if set
  98.  
  99.     mov    byte ptr [SI], ' '    ; replace switch name with blank
  100.  
  101. out_2:    inc    SI        ; point to next name
  102.     loop    outlp        ; continue for all switches
  103.     lea    DX, rpt$    ; addr DS:DX points to report message
  104.     mov    CX, lrpt    ; get message byte count
  105.  
  106. endit:    mov    BX, STDOUT    ; write to standard output device
  107.     mov    AH, 40h        ; INT 21h service 40h: write to file/device
  108.     int    21h
  109.     EXIT    0        ; terminate program normally using macro
  110.  
  111.     end    start
  112.  
  113.