home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / ansi132.zip / ANSI.ASM next >
Assembly Source File  |  1990-03-23  |  72KB  |  1,422 lines

  1. ;------------------------------------------------------------------------;
  2. ;  ANSI.COM - Replacement for the ANSI.SYS console device driver.        ;
  3. ;  Unlike ANSI.SYS which must be installed at boot time, ANSI.COM        ;
  4. ;  can be installed and uninstalled at anytime.  Enhancements include    ;
  5. ;  a fast screen write and variable sized keyboard reassignment buffer.  ;
  6. ;                                                                        ;
  7. ;  Update 3/2/89 - Fix for DOS function 0Bh, Check Standard Input Status ;
  8. ;                      and STDIN in ANSI_INT_21 handler.                 ;
  9. ;                  Leading zero inserted for Device Status Report for    ;
  10. ;                      single digit cursor positions.                    ;
  11. ;                  INFORMATION typo 40 for 40H.                          ;
  12. ;                  WRITE_FAST modified to handle CR and LF instead of    ;
  13. ;                      WRITE_CHAR.                                       ;
  14. ;  Update 3/7/89 - Fix for CLS in graphics mode.          Version 1.2    ;
  15. ;                                                                        ;
  16. ;  Update 8/8/89 - STI added to INT 21 and 29 handler.    Version 1.3    ;
  17. ;:                                                                       ;
  18. ;: Update 01/01/90 - [KON | KOFF] and [PON | POFF] added  Version 1.31   ;
  19. ;:                   (all changes marked with ;: - Gary Meeker)          ;
  20. ;| Update 03/23/90 - /Q option added for no output when exectued  1.32   ;
  21. ;|                   (all changes marked with ;| - Gary Meeker)          ;
  22. ;                                                                        ;
  23. ;  PC Magazine - Michael J. Mefford                                      ;
  24. ;------------------------------------------------------------------------;
  25.  
  26. _TEXT          SEGMENT PUBLIC 'CODE'
  27.                ASSUME  CS:_TEXT,DS:_TEXT,ES:_TEXT,SS:_TEXT
  28.                ORG     100H
  29. START:         JMP     INITIALIZE
  30.  
  31. ;                 DATA AREA
  32. SIGNATURE      DB      CR,SPACE,SPACE,SPACE,CR,LF
  33. COPYRIGHT      DB      "ANSI 1.32 (C) 1988 Ziff Communications Co.",CR,LF    ;|
  34. PROGRAMMER     DB      "PC Magazine ",BOX," Michael J. Mefford",CR,LF,LF,"$"
  35.                DB      CTRL_Z
  36.  
  37. CR             EQU     13
  38. LF             EQU     10
  39. CTRL_Z         EQU     26
  40. SPACE          EQU     32
  41. BOX            EQU     254
  42. ESC_CHAR       EQU     27
  43. SINGLE_QUOTE   EQU     39
  44. DOUBLE_QUOTE   EQU     34
  45. BELL           EQU     7
  46. BS             EQU     8
  47. TAB            EQU     9
  48.  
  49. OFF            EQU     1
  50. ON             EQU     2
  51. SLOW           EQU     4
  52. FAST           EQU     8
  53. KOFF           EQU     16                         ;:
  54. KON            EQU     32                         ;:
  55. POFF           EQU     64                         ;:
  56. PON            EQU     128                        ;:
  57. STATUS_MASK    EQU     1111110000000001B          ;: Set Mask and Bit
  58.  
  59. ANSI_STATE     DW      ESC_STATE
  60. STATUS         DB      ON OR FAST OR KON OR PON           ;:
  61. PARAMETERS     DB      "OFF",0,"ON",0,0,"SLOWFASTKOFFKON",0,"POFFPON",0 ;:
  62. LAST_PARAMETER EQU     $ - PARAMETERS
  63.  
  64. OLD_INT_29     DW      ?,?
  65. OLD_INT_16     DW      ?,?
  66. OLD_INT_21     DW      ?,?
  67.  
  68. ATTRIBUTE      DB      7
  69. SAVE_POSITION  DW      0
  70. LINE_WRAP      DB      ON
  71. QUOTE_TYPE     DB      ?
  72. ESC_COUNT      DW      0
  73. NUMBER_COUNT   DW      0
  74.  
  75. COMMAND_TABLE  LABEL   BYTE
  76. DB   "H", "A", "B", "C", "D", "f", "n", "s", "u", "K", "m", "h", "l", "p", "J"
  77. COMMAND_LENGTH EQU     $ - COMMAND_TABLE
  78.  
  79. DW   CURS_POSITION, CURSOR_UP,     CURSOR_DOWN, CURS_FORWARD, CURS_BACKWARD
  80. DW   HORZ_VERT_POS, DEVICE_STATUS, SAVE_CURSOR, RESTORE_CURS, ERASE_IN_LINE
  81. DW   SGR,           SET_MODE,      RESET_MODE,  REASSIGNMENT, CLS
  82. COMMAND_END    EQU     $ - 2
  83.  
  84. ATTRIBUTE_TABLE        LABEL    BYTE
  85. DB   00,01,04,05,07,08,30,31,32,33,34,35,36,37,40,41,42,43,44,45,46,47
  86. ATTRIBUTE_LENGTH       EQU      $ - ATTRIBUTE_TABLE
  87.  
  88. ;Format: AND mask,OR mask
  89. DB      000H,07H, 0FFH,08H, 0F8H,01H, 0FFH,80H, 0F8H,70H, 088H,00H
  90. DB      0F8H,00H, 0F8H,04H, 0F8H,02H, 0F8H,06H, 0F8H,01H, 0F8H,05H
  91. DB      0F8H,03H, 0F8H,07H, 08FH,00H, 08FH,40H, 08FH,20H, 08FH,60H
  92. DB      08FH,10H, 08FH,50H, 08FH,30H, 08FH,70H
  93. ATTRIBUTE_END          EQU      $ - 2
  94.  
  95. BIOS_ACTIVE_PAGE       EQU     62H
  96. ACTIVE_PAGE    DB      ?
  97. ADDR_6845      DW      ?
  98. BIOS_CRT_MODE          EQU     49H
  99. CRT_MODE       DB      ?
  100. CRT_COLS       DW      ?
  101. CRT_LEN        DW      ?
  102. CRT_START      DW      ?
  103. CRT_DATA_LENGTH        EQU     $ - CRT_MODE
  104. CURSOR_POSN    LABEL   WORD
  105. CURSOR_COL     DB      ?
  106. CURSOR_ROW     DB      ?
  107. CRT_ROWS       DB      ?
  108.  
  109. DOS_INPUT      DB      OFF
  110. BUSY_FLAG      DB      OFF
  111. REASSIGN_FLAG  DB      OFF
  112. REMOVE_FLAG    DB      ON
  113. REASSIGN_COUNT DW      0
  114. REASSIGN_POS   DW      ?
  115. FUNCTION_16    DB      ?
  116. ZR             EQU     1000000B
  117.  
  118. ESC_BUFFER_SIZE      EQU     126
  119. REASSIGNMENT_DEFAULT EQU     200
  120. REASSIGNMENT_SIZE    DW      REASSIGNMENT_DEFAULT
  121. REASSIGNMENT_MAX     EQU     60 * 1024
  122. REASSIGN_END         DW      REASSIGNMENT_BUFFER
  123. BUFFER_END           DW      REASSIGNMENT_BUFFER + REASSIGNMENT_DEFAULT
  124.  
  125. ;                   CODE AREA
  126. ;************* INTERRUPT HANDLERS *************;
  127. ;------------------------------------------------------------------------------;
  128. ; INT 29 is an undocumented interrupt called by DOS for output to the console. ;
  129. ;------------------------------------------------------------------------------;
  130.  
  131. ANSI_INT_29    PROC    FAR
  132.                STI
  133.                PUSH    AX                      ;Save all registers.
  134.                PUSH    BX
  135.                PUSH    CX
  136.                PUSH    DX
  137.                PUSH    SI
  138.                PUSH    DI
  139.                PUSH    DS
  140.                PUSH    ES
  141.                PUSH    BP
  142.  
  143.                CLD                             ;All string operations forward.
  144.                MOV     BX,CS                   ;Point to our data segment.
  145.                MOV     DS,BX
  146.                MOV     ES,BX
  147.                CALL    ANSI_STATE              ;Call the current state of
  148.                                                ; the ANSI Esc sequence.
  149.                POP     BP
  150.                POP     ES
  151.                POP     DS
  152.                POP     DI
  153.                POP     SI
  154.                POP     DX
  155.                POP     CX
  156.                POP     BX
  157.                POP     AX                      ;Restore all registers and
  158.                IRET                            ; return.
  159. ANSI_INT_29    ENDP
  160.  
  161. ;------------------------------------------------;
  162.  
  163. ANSI_INT_21    PROC    FAR
  164.                PUSHF
  165.                CMP     AH,0BH                  ;If DOS function 0Bh (Check
  166.                JNZ     CK_INPUT                ; Standard Input Status)
  167.                CMP     CS:REASSIGN_FLAG,ON     ; and reassignment in progress,
  168.                JNZ     CK_INPUT                ; including a Device Status
  169.                CALL    DWORD PTR CS:OLD_INT_21 ; Request, then process call in
  170.                MOV     AL,0FFH                 ; case control break pressed;
  171.                IRET                            ; then return 0FFh for char ready
  172.  
  173. CK_INPUT:      CMP     AH,3FH                  ;Read from STDIN?
  174.                JNZ     CK_CONSOLE
  175.                OR      BX,BX
  176.                JZ      INPUT
  177.  
  178. CK_CONSOLE:    CMP     AH,0AH                  ;If DOS Int 21 functions Ah,
  179.                JZ      INPUT                   ; 7h, 1h or console input (6h)
  180.                CMP     AH,7                    ; then tell INT 16, DOS input
  181.                JZ      INPUT                   ; is active.
  182.                CMP     AH,1
  183.                JZ      INPUT
  184.                CMP     AH,8
  185.                JZ      INPUT
  186.                CMP     AH,6
  187.                JNZ     QUICK21_EXIT
  188.                CMP     DL,0FFH
  189.                JZ      INPUT
  190. QUICK21_EXIT:  POPF                              ;If not, let the original
  191.                JMP     DWORD PTR CS:OLD_INT_21   ; interrupt process the call.
  192.  
  193. INPUT:         POPF
  194.                MOV     CS:DOS_INPUT,ON           ;INT 16 handler flag.
  195.                PUSHF
  196.                CALL    DWORD PTR CS:OLD_INT_21   ;Emulate an interrupt.
  197.                MOV     CS:DOS_INPUT,OFF          ;Turn flag back off.
  198.                STI
  199.                RET     2                         ;Return with current flags.
  200. ANSI_INT_21    ENDP
  201.  
  202. ;-----------------------------------------------------------------;
  203. ; If we get here via a DOS console input call, any awaiting key   ;
  204. ; in keyboard buffer is checked to see if it is to be reassigned. :
  205. ;-----------------------------------------------------------------;
  206.  
  207. ANSI_INT_16    PROC    FAR
  208.                STI                             ;Interrupts back on.
  209.                PUSHF                           ;Preserve flags on stack.
  210.                PUSH    BP
  211.                PUSH    DS
  212.                PUSH    AX
  213.                PUSH    CS                      ;Point to our data.
  214.                POP     DS
  215.  
  216.                CMP     DOS_INPUT,OFF           ;If not called via a DOS
  217.                JZ      QUICK16_EXIT            ; console input call, exit.
  218.                CMP     BUSY_FLAG,ON            ;If already processing a call,
  219.                JZ      QUICK16_EXIT            ; exit.
  220.                CLD                             ;All string operations forward.
  221.                MOV     BP,SP                   ;Base reference to flags on stack
  222.                MOV     FUNCTION_16,AH          ;Save function request.
  223.                AND     AH,NOT 10H              ;Strip possible extended request.
  224.                JZ      GET_KEY                 ;If zero, it's wait for a key.
  225.                DEC     AH                      ;Else, decrement.  If zero it's
  226.                JZ      KEY_STATUS              ; key status, else exit.
  227.  
  228. QUICK16_EXIT:  POP     AX                      ;Restore registers and let
  229.                POP     DS                      ; original interrupt handler
  230.                POP     BP                      ; process the call.
  231.                POPF
  232.                JMP     DWORD PTR CS:OLD_INT_16
  233.  
  234. KEY_STATUS:    OR      BYTE PTR [BP+6],ZR      ;Assume none ready; ZR = 1.
  235. GET_KEY:       MOV     BUSY_FLAG,ON            ;Non-reentrant; flag busy.
  236.                POP     AX
  237.                PUSH    BX                      ;Save some more registers.
  238.                PUSH    CX
  239.                PUSH    DX
  240.                PUSH    SI
  241.                PUSH    DI
  242.                CMP     REASSIGN_FLAG,ON        ;Already in process of reassign?
  243.                JZ      CK_BUFFER               ;If yes, check kbd buffer.
  244.  
  245.                PUSHF                           ;Else, emulate an interrupt.
  246.                CALL    DWORD PTR OLD_INT_16
  247.                PUSHF                           ;Status call results in flags.
  248.                TEST    FUNCTION_16,1           ;Was it a status call?
  249.                JZ      CK_DUP                  ;If no, check key returned.
  250.                POPF                            ;Else, retrieve status results.
  251.                JZ      INT16_EXIT              ;If zero, no key waiting.
  252.                AND     BYTE PTR [BP+6],NOT ZR  ;Else, ZR = 0.
  253.                PUSHF                           ;PUSHF just to keep stack right.
  254.  
  255. CK_DUP:        POPF                            ;Fix stack.
  256.                TEST    STATUS,KOFF             ;:If KOFF then we don't do
  257.                JNZ     INT16_EXIT              ;:re-assignments
  258.                MOV     BX,AX                   ;Match procedure wants key in BX.
  259.                CALL    CK_MATCH                ;Check reassignment buffer.
  260.                JC      INT16_EXIT              ;If no match, exit.
  261.                MOV     AX,[DI]                 ;Else, retrieve string length.
  262.                SUB     AX,3                    ;Adjust for length word and match
  263.                ADD     DI,3                    ; byte; same for string pointer.
  264.                OR      BL,BL                   ;Extended key? ie. key = 0.
  265.                JNZ     STORE_COUNT             ;If no, save length and pointer.
  266.                DEC     AX                      ;Else, adjust for extended code.
  267.                INC     DI
  268. STORE_COUNT:   MOV     REASSIGN_COUNT,AX       ;Save string length.
  269.                MOV     REASSIGN_POS,DI         ;Save pointer to string.
  270.                MOV     REASSIGN_FLAG,ON        ;Flag that replacement in effect.
  271. CK_BUFFER:     TEST    FUNCTION_16,1           ;Was it a key wait function call?
  272.                JNZ     RETRIEVE                ;If no, key status; get it.
  273.                CMP     REMOVE_FLAG,OFF         ;Removed it from kbd buffer?
  274.                JZ      RETRIEVE                ;If yes, get reassignment.
  275.                MOV     AH,FUNCTION_16          ;Else, eat the replaced character
  276.                INT     16H                     ; via INT 16.
  277.                MOV     REMOVE_FLAG,OFF         ;Flag character eaten.
  278.  
  279. RETRIEVE:      MOV     DI,REASSIGN_POS         ;Retrieve pointer to string.
  280.                MOV     AX,[DI]                 ;Retrieve replacement character.
  281.                TEST    FUNCTION_16,1           ;Is it a key wait function call?
  282.                JZ      REMOVE                  ;If yes, bump pointer up one.
  283.                AND     BYTE PTR [BP+6],NOT ZR  ;If no, status call; ZR = 0
  284.                JMP     SHORT INT16_EXIT        ;All done.
  285.  
  286. REMOVE:        INC     REASSIGN_POS            ;Move pointer to next character.
  287.                DEC     REASSIGN_COUNT          ;One less character to replace.
  288.                JZ      REASSIGN_DONE           ;If end of string, reset flags.
  289.                OR      AL,AL                   ;Else, extended replacement?
  290.                JNZ     INT16_EXIT              ;If no, done here.
  291.                INC     REASSIGN_POS            ;Else adjust pointers.
  292.                DEC     REASSIGN_COUNT
  293.                JNZ     INT16_EXIT              ;End of string?
  294. REASSIGN_DONE: MOV     REASSIGN_FLAG,OFF       ;If yes, reset flags.
  295.                MOV     REMOVE_FLAG,ON
  296.  
  297. INT16_EXIT:    MOV     BUSY_FLAG,OFF           ;Reset the busy flag.
  298.                POP     DI                      ;Restore registers.
  299.                POP     SI
  300.                POP     DX
  301.                POP     CX
  302.                POP     BX
  303.                POP     DS
  304.                POP     BP
  305.                POPF                            ;Flags on stack have status
  306.                RET     2                       ; results; kill old flags.
  307. ANSI_INT_16    ENDP
  308.  
  309. ;************* ANSI ESCAPE STATE ROUTINES ************* ;
  310.  
  311. ESC_STATE:     MOV     BX,OFFSET BRACKET_STATE ;Assume Esc character.
  312.                CMP     AL,ESC_CHAR             ;Is it Esc?  If yes, store
  313.                JZ      SHORT_JUMP              ; char. and next state.
  314.                JMP     WRITE_CHAR              ;Else, normal char; write it.
  315.  
  316. ;------------------------------------------------;
  317.  
  318. BRACKET_STATE: MOV     BX,OFFSET SPECIAL_STATE ;Assume left bracket.
  319.                CMP     AL,"["                  ;Is it left bracket?  If yes,
  320. SHORT_JUMP:    JZ      STORE_STATE             ;store char. and next state.
  321.                JMP     FLUSH_BUFFER            ;Else, flush Esc out of buffer.
  322.  
  323. ;---------------------------------------------------------------;
  324. ; Must process <ESC>[2J (CLS) regardless if ANSI.COM ON or OFF. ;
  325. ;---------------------------------------------------------------;
  326.  
  327. SPECIAL_STATE: MOV     BX,OFFSET CLS_STATE     ;Assume Erase in Display code.
  328.                CMP     AL,"2"                  ;Is it the "2" ?
  329.                JZ      STORE_STATE             ;If yes, progress to next state.
  330.                TEST    STATUS,OFF              ;Else, is ANSI OFF ?
  331.                JNZ     FLUSH_BUFFER            ;If yes, flush Esc, bracket.
  332.                MOV     BX,OFFSET PARAM_STATE   ;Else, check for first Set,
  333.                CMP     AL,"="                  ; Reset Mode characters of
  334.                JZ      STORE_STATE             ; "=" and "?".
  335.                CMP     AL,"?"
  336.                JZ      STORE_STATE             ;If found, store.
  337.                JMP     SHORT DO_PARAMETER      ;Else, check other codes.
  338.  
  339. ;------------------------------------------------;
  340.  
  341. CLS_STATE:     CMP     AL,"J"                  ;Is it second character of CLS?
  342.                MOV     DI,OFFSET COMMAND_END   ;If yes, execute
  343.                JZ      EXECUTE                 ; Erase in Display procedure.
  344.                TEST    STATUS,OFF              ;ANSI OFF?  If yes, flush buffer.
  345.                JNZ     FLUSH_BUFFER            ; If not fall through to process.
  346.                MOV     BYTE PTR NUMBER_BUFFER,2   ;Store the previous 2.
  347. DO_PARAMETER:  MOV     ANSI_STATE,OFFSET PARAM_STATE  ;Parameter state.
  348.  
  349. ;------------------------------------------------;
  350.  
  351. PARAM_STATE:   CALL    CK_QUOTE                ;Is it single or double quotes?
  352.                JZ      STORE_STATE             ;If yes, store string state.
  353.                CMP     AL,";"                  ;Is it semi-colon delimiter?
  354.                JNZ     CK_NUMBER               ;If no, check if number.
  355.                INC     NUMBER_COUNT            ;Else, increment number count.
  356.                JMP     SHORT BUFFER_CHAR       ;Buffer the semi-colon.
  357.  
  358. CK_NUMBER:     CMP     AL,"0"                  ;Is it below 0 ?
  359.                JB      FLUSH_BUFFER            ;If yes, illegal; flush buffer.
  360.                CMP     AL,"9"                  ;Else, is it above 9 ?
  361.                JA      DO_COMMAND              ;If yes, check for command char.
  362.                CALL    ACCUMULATE              ;Else it's a number; accumulate.
  363.                JMP     SHORT BUFFER_CHAR       ;Buffer the character.
  364.  
  365. DO_COMMAND:    MOV     DI,OFFSET COMMAND_TABLE ;Point to legal ANSI commands.
  366.                MOV     CX,COMMAND_LENGTH       ;Number of commands.
  367.                REPNZ   SCASB                   ;Check for a match.
  368.                JNZ     FLUSH_BUFFER            ;If no match, flush sequence.
  369.                INC     NUMBER_COUNT            ;Else, increment for last number.
  370.                MOV     DI,OFFSET COMMAND_END   ;Point to appropriate command
  371.                SHL     CX,1                    ; processing procedure.
  372.                SUB     DI,CX
  373. EXECUTE:       CALL    GET_BIOS_DATA           ;Get cursor position data.
  374.                CALL    DS:[DI]                 ;Do command subroutine.
  375.                JMP     SHORT FLUSH_END         ;Clear buffer.
  376.  
  377. ;------------------------------------------------;
  378.  
  379. QUOTE_STATE:   CMP     AL,QUOTE_TYPE           ;Is it an ending string quote?
  380.                JNZ     BUFFER_CHAR             ;If no, buffer literal.
  381.                MOV     BX,OFFSET PARAM_STATE   ;Else, back to parameter parsing.
  382.  
  383. ;------------------------------------------------;
  384.  
  385. STORE_STATE:   MOV     ANSI_STATE,BX           ;Store the ANSI state.
  386.  
  387. ;------------------------------------------------;
  388.  
  389. BUFFER_CHAR:   MOV     DI,ESC_COUNT            ;Buffer the character in case
  390.                CMP     DI,ESC_BUFFER_SIZE      ; ending ANSI command is illegal.
  391.                JZ      FLUSH_BUFFER            ;If buffer full, flush.
  392.                ADD     DI,OFFSET ESC_BUFFER    ;Point to next buffer position.
  393.                STOSB                           ;Store the character.
  394.                INC     ESC_COUNT               ;Increment the sequence count.
  395.                RET
  396.  
  397. ;------------------------------------------------;
  398.  
  399. FLUSH_BUFFER:  PUSH    AX                      ;Save the current character.
  400.                MOV     SI,OFFSET ESC_BUFFER    ;Point to the sequence buffer.
  401.                MOV     CX,ESC_COUNT            ;Count of buffered characters.
  402. NEXT_FLUSH:    LODSB                           ;Retrieve one.
  403.                PUSH    CX                      ;Save counter and pointer.
  404.                PUSH    SI
  405.                CALL    WRITE_CHAR              ;Write character to screen.
  406.                POP     SI                      ;Restore counter and pointer.
  407.                POP     CX
  408.                LOOP    NEXT_FLUSH              ;Flush entire buffer.
  409.                POP     AX                      ;Retrieve last character.
  410.                CALL    WRITE_CHAR              ;Write it also.
  411. FLUSH_END:     MOV     ESC_COUNT,0                   ;Reset counter.
  412.                MOV     ANSI_STATE,OFFSET ESC_STATE   ;Back to Esc state.
  413.                MOV     NUMBER_COUNT,0                ;Reset parameter counter.
  414.                PUSH    CS                            ;Point to our data.
  415.                POP     ES
  416.                MOV     DI,OFFSET NUMBER_BUFFER       ;Clear number buffer
  417.                MOV     CX,ESC_BUFFER_SIZE / 2        ; to zeros.
  418.                XOR     AX,AX
  419.                REP     STOSW
  420.                RET
  421.  
  422. ;------------------------------------------------;
  423. ; Slow video writes are via BIOS Write TTY.      ;
  424. ;------------------------------------------------;
  425.  
  426. WRITE_CHAR:    CMP     AL,BELL                 ;Let BIOS process BS and BELL.
  427.                JZ      WRITE_TTY
  428.                CMP     AL,BS
  429.                JZ      WRITE_TTY
  430.  
  431.                CALL    GET_BIOS_DATA           ;Get BIOS video data.
  432.                CMP     AL,TAB                  ;Is character a TAB?
  433.                JNZ     CK_ACTIVE               ;If no, process normally.
  434.                MOV     CX,CURSOR_POSN          ;Else, expand TAB to
  435.                AND     CX,7                    ; appropriate space characters.
  436.                NEG     CX
  437.                ADD     CX,8
  438. NEXT_TAB:      PUSH    CX
  439.                MOV     AL,SPACE
  440.                CALL    CK_ACTIVE
  441.                POP     CX
  442.                LOOP    NEXT_TAB
  443.                RET
  444.  
  445. CK_ACTIVE:     TEST    STATUS,OFF              ;Is ANSI OFF?
  446.                JNZ     WRITE_TTY               ;If yes, write via BIOS TTY.
  447. CK_FAST:       CALL    CK_SLOW_TEXT            ;Is ANSI SLOW or in graphics
  448.                JNC     WRITE_FAST              ; mode? If no, write fast.
  449.  
  450.                CMP     AL,CR                   ;Let BIOS handle CR and LF.
  451.                JZ      WRITE_TTY
  452.                CMP     AL,LF
  453.                JZ      WRITE_TTY
  454.  
  455. WRITE_SLOW:    PUSH    AX                      ;Else, write character/attribute
  456.                MOV     CX,1                    ; at current cursor position
  457.                MOV     BH,ACTIVE_PAGE          ; via BIOS.
  458.                MOV     BL,ATTRIBUTE
  459.                MOV     AH,9
  460.                INT     10H
  461.                POP     AX
  462.  
  463.                CMP     LINE_WRAP,ON            ;Is line wrap on?
  464.                JZ      TTY                     ;If yes, continue.
  465.                MOV     CX,CRT_COLS             ;Else, cursor at rightmost
  466.                DEC     CL                      ; column?
  467.                CMP     CL,CURSOR_COL           ;If yes, continue, else
  468.                JNZ     TTY                     ; return without writing.
  469.                RET
  470.  
  471. ;------------------------------------------------;
  472.  
  473. WRITE_TTY:     MOV     BL,7                    ;Attribute in graphics mode.
  474. TTY:           MOV     AH,0EH
  475.                INT     10H
  476.                RET
  477.  
  478. ;----------------------------------------------------------------------------;
  479. ; Fast screen writes are directly to the video buffer without retrace check. ;
  480. ;----------------------------------------------------------------------------;
  481.  
  482. WRITE_FAST:    PUSH    ES                      ;Preserve extra segment.
  483.                MOV     DX,CURSOR_POSN          ;Retrieve cursor position.
  484.                CMP     AL,CR                   ;Carriage return?
  485.                JNZ     CK_LINEFEED             ;If no, check linefeed.
  486.                XOR     DL,DL                   ;Else, cursor to first column.
  487.                JMP     SHORT UPDATE_CURSOR
  488. CK_LINEFEED:   CALL    VIDEO_SETUP             ;Calculate video address.
  489.                CMP     AL,LF                   ;Linefeed?
  490.                JZ      NEXT_ROW                ;If yes, next row.
  491.  
  492.                MOV     AH,ATTRIBUTE            ;Retrieve attribute.
  493.                STOSW                           ;Put char/attrib in video buffer.
  494.                INC     DL                      ;Increment cursor column.
  495.                CMP     DL,BYTE PTR CRT_COLS    ;End of row?
  496.                JB      UPDATE_CURSOR           ;If no, update cursor.
  497.  
  498.                CMP     LINE_WRAP,OFF           ;Else, line wrap off?
  499.                JZ      FAST_END                ;If yes, don't move cursor.
  500.                XOR     DL,DL                   ;Else, column zero.
  501. NEXT_ROW:      INC     DH                      ;Next row.
  502.                CALL    INFORMATION             ;Get displayable row info.
  503.                CMP     DH,AL                   ;Beyond the bottom of screen?
  504.                JBE     UPDATE_CURSOR           ;If no, update cursor.
  505.  
  506.                DEC     DH                      ;Else, cursor to original row.
  507.                MOV     DI,CRT_START            ;Point destination to top.
  508.                MOV     SI,DI                   ;Point source to second row
  509.                MOV     AX,CRT_COLS             ; by adding width in columns
  510.                PUSH    AX                      ; twice for char/attribute.
  511.                ADD     SI,AX
  512.                ADD     SI,AX
  513.                MUL     DH                      ;Times displayable rows - 1.
  514.                MOV     CX,AX                   ; equals char/attrib to scroll.
  515.                PUSH    DS                      ;Save data segment and
  516.                PUSH    ES                      ; point to video segment.
  517.                POP     DS
  518.                REP     MOVSW                   ;Scroll the screen.
  519.                POP     DS                      ;Restore data segment.
  520.                POP     CX                      ;Retrieve CRT columns.
  521.                MOV     AL,SPACE                ;Write space/attrib to
  522.                MOV     AH,ATTRIBUTE            ; bottom row.
  523.                REP     STOSW
  524.  
  525. UPDATE_CURSOR: CALL    SET_CURSOR              ;Update the cursor position.
  526. FAST_END:      POP     ES                      ;Restore extra segment.
  527.                RET
  528.  
  529. ;************* SUPPORT ROUTINES *************;
  530.  
  531. CK_QUOTE:      MOV     BX,OFFSET QUOTE_STATE   ;Assume quote state.
  532.                MOV     AH,DOUBLE_QUOTE
  533.                CMP     AL,AH                   ;Is it double quote?
  534.                JZ      GOT_QUOTE               ;If yes, string delimiter.
  535.                MOV     AH,SINGLE_QUOTE         ;Is it single quote?
  536.                CMP     AL,AH                   ;Is yes, string delimiter.
  537.                JNZ     QUOTE_END               ;Else, return ZR = 0.
  538. GOT_QUOTE:     MOV     QUOTE_TYPE,AH           ;Store as matching string end.
  539. QUOTE_END:     RET
  540.  
  541. ;------------------------------------------------;
  542.  
  543. ACCUMULATE:    PUSH    AX                      ;Preserve number character.
  544.                SUB     AL,"0"                  ;Convert ASCII to binary.
  545.                MOV     CL,AL                   ;Save the number.
  546.                MOV     AX,10                   ;Multiply previous count by 10.
  547.                MOV     BX,NUMBER_COUNT
  548.                MUL     BYTE PTR NUMBER_BUFFER[BX]
  549.                ADD     AL,CL                            ;Add in new number
  550.                MOV     BYTE PTR NUMBER_BUFFER[BX],AL    ; and store.
  551.                POP     AX
  552.                RET
  553.  
  554. ;---------------------------------------------------------------------------;
  555. ; OUTPUT:  CY = 1 if write SLOW mode or in graphics mode; CY = 0 otherwise. ;
  556. ;---------------------------------------------------------------------------;
  557.  
  558. CK_SLOW_TEXT:  TEST    STATUS,SLOW
  559.                JNZ     SLOW_MODE
  560.                CMP     CRT_MODE,7
  561.                JZ      TEXT_MODE
  562.                CMP     CRT_MODE,3
  563.                JA      SLOW_MODE
  564. TEXT_MODE:     CLC
  565.                RET
  566.  
  567. SLOW_MODE:     STC
  568.                RET
  569.  
  570. ;-------------------------------------;
  571. ; OUTPUT:  AL = Screen rows minus one ;
  572. ;-------------------------------------;
  573.  
  574. INFORMATION:   PUSH    DS                      ;Save data segment.
  575.                MOV     AX,40H                  ;Point to BIOS data.
  576.                MOV     DS,AX
  577.                MOV     AL,DS:[84H]             ;Retrieve rows - 1.
  578.                OR      AL,AL                   ;BIOS supported?
  579.                JNZ     INFO_END                ;If yes, done here.
  580.                MOV     AL,24                   ;Else, assume 25 lines.
  581. INFO_END:      POP     DS
  582.                RET
  583.  
  584. ;------------------------------------------------------------------------------;
  585. ; INPUT:  DX = Cursor position.                                                ;
  586. ; OUTPUT: ES = Video buffer segment; DI = Video buffer offset; AX,DX preserved ;
  587. ;------------------------------------------------------------------------------;
  588.  
  589. VIDEO_SETUP:   PUSH    AX
  590.                MOV     AX,CRT_COLS             ;Retrieve CRT columns.
  591.                MUL     DH                      ;Times cursor row.
  592.                MOV     BL,DL
  593.                XOR     BH,BH
  594.                ADD     AX,BX                   ;Plus cursor column.
  595.                SHL     AX,1                    ;Times two for attribute.
  596.                MOV     DI,CRT_START            ;Plus starting video offset.
  597.                ADD     DI,AX                   ;Equals destination.
  598.  
  599.                MOV     BX,0B000H               ;Assume mono card.
  600.                CMP     ADDR_6845,3B4H          ;Is it mono port?
  601.                JZ      VIDEO_SEGMENT           ;If yes, guessed right.
  602.                ADD     BX,800H                 ;Else, point to color segment.
  603. VIDEO_SEGMENT: MOV     ES,BX
  604.                POP     AX
  605.                RET
  606.  
  607. ;------------------------------------------------;
  608. ; Move BIOS video data into our data segment.    ;
  609. ;------------------------------------------------;
  610.  
  611. GET_BIOS_DATA: PUSH    DS
  612.                PUSH    DI                      ;Point to BIOS data segment.
  613.                MOV     BX,40H
  614.                MOV     DS,BX
  615.                MOV     SI,BIOS_ACTIVE_PAGE     ;Start with active page.
  616.                MOV     DI,OFFSET ACTIVE_PAGE
  617.                MOVSB                           ;Retrieve active page
  618.                MOVSW                           ; and address of 6845 port.
  619.                MOV     SI,BIOS_CRT_MODE        ;Retrieve CRT mode, CRT columns,
  620.                MOV     CX,CRT_DATA_LENGTH      ; CRT length, CRT start.
  621.                REP     MOVSB
  622.                MOV     BL,ES:ACTIVE_PAGE       ;Use active page as index
  623.                XOR     BH,BH                   ; of active cursor position.
  624.                SHL     BX,1
  625.                ADD     SI,BX
  626.                MOVSW
  627.                POP     DI
  628.                POP     DS
  629.                RET
  630.  
  631. ;----------------------------------------------------------------------------;
  632. ; OUTPUT: BL = First parameter; BH = Second parameter; DX = cursor position. ;
  633. ;----------------------------------------------------------------------------;
  634.  
  635. ADJUST_NUMBER: MOV     BX,WORD PTR NUMBER_BUFFER
  636.                OR      BH,BH
  637.                JNZ     ADJUST_AL               ;If either parameter zero,
  638.                INC     BH                      ; increment to convert
  639. ADJUST_AL:     OR      BL,BL                   ; to base one.
  640.                JNZ     ADJUST_END
  641.                INC     BL
  642. ADJUST_END:    MOV     DX,CURSOR_POSN          ;Return cursor position.
  643.                RET
  644.  
  645. ;--------------------------------------------------------------------------;
  646. ; INPUT:  AX = number; BP = 0 if console output; BP = 1 if storage output. ;
  647. ;--------------------------------------------------------------------------;
  648.  
  649. DECIMAL_OUT:   MOV     BX,10                   ;Divisor of ten.
  650.                XOR     CX,CX                   ;Zero in counter.
  651. NEXT_COUNT:    XOR     DX,DX                   ;Zero in high half.
  652.                DIV     BX                      ;Divide by ten.
  653.                ADD     DL,"0"                  ;Convert to ASCII.
  654.                PUSH    DX                      ;Save results.
  655.                INC     CX                      ;Also increment count.
  656.                CMP     AX,0                    ;Are we done?
  657.                JNZ     NEXT_COUNT              ;Continue until zero.
  658.                OR      BP,BP                   ;If BP zero, output to screen.
  659.                JNZ     DEVICE_OUTPUT           ;Else, store in buffer.
  660.  
  661. REPORT_OUTPUT: POP     AX                      ;Retrieve number.
  662.                CALL    PRINT_CHAR              ;Display it.
  663.                LOOP    REPORT_OUTPUT
  664.                RET
  665.  
  666. DEVICE_OUTPUT: CMP     CX,2                    ;Two digits?
  667.                JZ      TWO_DIGITS              ;If yes, process normally.
  668.                MOV     AL,"0"                  ;Else, store leading zero.
  669.                STOSB
  670.  
  671. TWO_DIGITS:    POP     AX                      ;Retrieve number.
  672.                STOSB                           ;Store it
  673.                LOOP    TWO_DIGITS
  674.                RET
  675.  
  676. ;************* ANSI COMMAND SUBSET *************;
  677.  
  678. CLS:           MOV     BH,7                    ;Assume normal attribute.
  679.                MOV     BL,BYTE PTR CRT_MODE    ;Get current video mode.
  680.                CMP     BL,4
  681.                JBE     CK_CLS_STATUS           ;If text mode then check if
  682.                CMP     BL,7                    ; ANSI active.
  683.                JZ      CK_CLS_STATUS
  684.                XOR     BH,BH                   ;Else, use black attribute
  685.                TEST    STATUS,OFF
  686.                JNZ     CLS_SLOW                ;If ANSI inactive, via BIOS.
  687.                JMP     SHORT CK_CLS_SLOW       ;Else, check if FAST or SLOW.
  688.  
  689. CK_CLS_STATUS: TEST    STATUS,OFF              ;Is ANSI OFF?
  690.                JNZ     CLS_SLOW                ;If yes, CLS via BIOS.
  691.                MOV     BH,ATTRIBUTE            ;Else, current attribute.
  692. CK_CLS_SLOW:   CALL    CK_SLOW_TEXT
  693.                JC      CLS_SLOW                ;If ANSI SLOW, via BIOS.
  694.  
  695. CLS_FAST:      MOV     AH,BH                   ;Else, attribute in high half.
  696.                MOV     AL,SPACE                ;Space character in low half.
  697.                XOR     DX,DX                   ;Cursor position home.
  698.                CALL    VIDEO_SETUP             ;Calculate video address.
  699.                MOV     CX,CRT_LEN              ;Retrieve CRT length.
  700.                SHR     CX,1                    ;Times two for attribute.
  701.                REP     STOSW                   ;Write directly to video buffer.
  702.                JMP     SHORT SET_CURSOR        ;Set the cursor top left corner.
  703.  
  704. CLS_SLOW:      CALL    INFORMATION             ;Get displayable rows.
  705.                MOV     DH,AL                   ;Store in DH.
  706.                MOV     DL,BYTE PTR CRT_COLS    ;Retrieve CRT columns.
  707.                DEC     DL                      ;Adjust to zero base.
  708.                XOR     CX,CX                   ;Scroll active page.
  709.                MOV     AX,600H
  710.                INT     10H
  711.                XOR     DX,DX                   ;Home the cursor.
  712.                JMP     SHORT SET_CURSOR
  713.  
  714. ;------------------------------------------------;
  715. CURS_POSITION:                                 ;These two commands are the same.
  716. HORZ_VERT_POS: CALL    INFORMATION             ;Returns AL = screen rows.
  717.                CALL    ADJUST_NUMBER           ;Returns BL,BH = parameters.
  718.                SUB     BX,101H                 ;Zero based.
  719.                CMP     BL,AL                   ;If cursor request within
  720.                JA      CURSOR_END              ; boundaries of screen, set it.
  721.                CMP     BH,BYTE PTR CRT_COLS
  722.                JAE     CURSOR_END
  723.                XCHG    BH,BL
  724.                MOV     DX,BX
  725.  
  726. SET_CURSOR:    MOV     BH,ACTIVE_PAGE          ;Set cursor via BIOS.
  727.                MOV     AH,2
  728.                INT     10H
  729. CURSOR_END:    RET
  730.  
  731. ;------------------------------------------------;
  732.  
  733. CURSOR_UP:     CALL    ADJUST_NUMBER           ;Move cursor up one or more rows
  734.                OR      DH,DH                   ; without changing column.
  735.                JZ      CURSOR_END              ;If already at top, ignore.
  736.                SUB     DH,BL
  737.                JNC     SET_CURSOR              ;Stay in bounds.
  738.                XOR     DH,DH
  739.                JMP     SHORT SET_CURSOR
  740.  
  741. ;------------------------------------------------;
  742.  
  743. CURSOR_DOWN:   CALL    INFORMATION             ;Returns AL = screen rows.
  744.                CALL    ADJUST_NUMBER           ;Returns BL,BH =nos.; DX = cursor
  745.                CMP     DH,AL                   ;Move cursor down requested
  746.                JZ      CURSOR_END              ; rows without changing column.
  747.                ADD     DH,BL
  748.                CMP     DH,AL
  749.                JNA     SET_CURSOR
  750.                MOV     DH,AL                   ;Stay in bounds.
  751.                JMP     SHORT SET_CURSOR
  752.  
  753. ;------------------------------------------------;
  754.  
  755. CURS_FORWARD:  CALL    ADJUST_NUMBER           ;Returns BL,BH =nos.; DX = cursor
  756.                MOV     BH,BYTE PTR CRT_COLS    ;Retrieve displayable columns.
  757.                DEC     BH                      ;Adjust to zero base.
  758.                CMP     DL,BH                   ;Move cursor forward one or more
  759.                JZ      CURSOR_END              ; columns without changing row.
  760.                ADD     DL,BL
  761.                CMP     DL,BH
  762.                JNA     SET_CURSOR
  763.                MOV     DL,BH                   ;Stay in bounds.
  764.                JMP     SHORT SET_CURSOR
  765.  
  766. ;------------------------------------------------;
  767.  
  768. CURS_BACKWARD: CALL    ADJUST_NUMBER           ;Returns BL,BH =nos.; DX = cursor
  769.                OR      DL,DL                   ;Move cursor backward one or
  770.                JZ      CURSOR_END              ; more columns without changing
  771.                SUB     DL,BL                   ; row.  Ignore if already left.
  772.                JNC     SET_CURSOR
  773.                XOR     DL,DL                   ;Stay in bounds.
  774.                JMP     SHORT SET_CURSOR
  775.  
  776. ;--------------------------------------------------------;
  777. ; Report cursor position via console; Format: ESC[#;#R   ;
  778. ;--------------------------------------------------------;
  779.  
  780. DEVICE_STATUS: MOV     DI,OFFSET DEVICE_STATUS_BUFFER
  781.                MOV     AL,ESC_CHAR
  782.                STOSB                           ;Store leading Esc, bracket
  783.                MOV     AL,"["                  ;in special Device Status Buffer.
  784.                STOSB
  785.  
  786.                MOV     AL,CURSOR_ROW           ;Retrieve cursor row.
  787.                XOR     AH,AH                   ;Zero in high half.
  788.                INC     AX                      ;Convert to base one.
  789.                MOV     BP,1                    ;Flag as storage.
  790.                CALL    DECIMAL_OUT             ;Convert to ASCII.
  791.                MOV     AL,";"                  ;Store delimiting semi-colon.
  792.                STOSB
  793.                MOV     AL,CURSOR_COL           ;Do same with cursor column.
  794.                XOR     AH,AH
  795.                INC     AX
  796.                CALL    DECIMAL_OUT
  797.                MOV     AL,"R"                  ;Add command character
  798.                STOSB
  799.                MOV     AL,CR                   ; and carriage return.
  800.                STOSB
  801.                SUB     DI,OFFSET DEVICE_STATUS_BUFFER   ;Setup for console out.
  802.                MOV     REASSIGN_COUNT,DI
  803.                MOV     REASSIGN_POS,OFFSET DEVICE_STATUS_BUFFER
  804.                MOV     REASSIGN_FLAG,ON
  805.                MOV     REMOVE_FLAG,OFF
  806.                RET
  807.  
  808. ;------------------------------------------------;
  809.  
  810. SAVE_CURSOR:   MOV     DX,CURSOR_POSN
  811.                MOV     SAVE_POSITION,DX
  812.                RET
  813.  
  814. ;------------------------------------------------;
  815.  
  816. RESTORE_CURS:  MOV     DX,SAVE_POSITION
  817.                JMP     SET_CURSOR
  818.  
  819. ;------------------------------------------------;
  820.  
  821. ERASE_IN_LINE: MOV     DX,CURSOR_POSN          ;Erase from the cursor to
  822.                MOV     CX,CRT_COLS             ; the end of the line, including
  823.                SUB     CL,DL                   ; the current cursor position.
  824.                CALL    CK_SLOW_TEXT
  825.                JC      ERASE_SLOW              ;If ANSI ON and not graphics,
  826.                CALL    VIDEO_SETUP             ; write directly to video buffer
  827.                MOV     AL,SPACE                ; with space/attribute.
  828.                MOV     AH,ATTRIBUTE
  829.                REP     STOSW
  830.                RET
  831.  
  832. ERASE_SLOW:    MOV     CX,DX                   ;Else, erase SLOW via
  833.                MOV     DL,BYTE PTR CRT_COLS    ; BIOS scroll active page.
  834.                DEC     DL
  835.                MOV     BH,ATTRIBUTE
  836.                MOV     AX,601H
  837.                INT     10H
  838.                RET
  839.  
  840. ;------------------------------------------------;
  841. ;            Set Graphics Rendition              ;
  842. ;------------------------------------------------;
  843.  
  844. SGR:           MOV     SI,OFFSET NUMBER_BUFFER     ;Point to number parameters.
  845. NEXT_SGR:      LODSB                               ;Retrieve parameter.
  846.                MOV     DI,OFFSET ATTRIBUTE_TABLE   ;Look up attribute in
  847.                MOV     CX,ATTRIBUTE_LENGTH         ; translation table.
  848.                REPNZ   SCASB
  849.                JNZ     SGR_LOOP
  850.  
  851.                MOV     DI,OFFSET ATTRIBUTE_END
  852.                SHL     CX,1
  853.                SUB     DI,CX
  854.                MOV     AX,[DI]
  855.                AND     ATTRIBUTE,AL            ;If match, AND with strip mask.
  856.                OR      ATTRIBUTE,AH            ;OR with add mask.
  857. SGR_LOOP:      DEC     NUMBER_COUNT            ;Do all parameters.
  858.                JNZ     NEXT_SGR
  859.                RET
  860.  
  861. ;------------------------------------------------;
  862.  
  863. SET_MODE:      MOV     AH,ON                   ;Assume command 7,
  864.                JMP     SHORT CK_WRAP           ; turn line wrap on.
  865.  
  866. ;------------------------------------------------;
  867.  
  868. RESET_MODE:    MOV     AH,OFF                     ;Assume turn line wrap off.
  869. CK_WRAP:       MOV     AL,BYTE PTR NUMBER_BUFFER  ;Retrieve number parameter.
  870.                CMP     AL,7                       ;Is it 7?
  871.                JZ      SET_WRAP                ;If yes, set wrap mode.
  872.                JB      DO_MODE                 ;1 - 6 valid modes.
  873.                CMP     AL,14                   ;14 - 16 valid modes.
  874.                JB      MODE_END
  875.                CMP     AL,19
  876.                JA      MODE_END                ;If above 16, illegal.
  877. DO_MODE:       XOR     AH,AH                   ;Else, set video mode
  878.                INT     10H                     ; to parameter.
  879.                RET
  880.  
  881. SET_WRAP:      MOV     LINE_WRAP,AH
  882. MODE_END:      RET
  883.  
  884. ;--------------------------------------------------------------------;
  885. ; Keyboard Key Reassignment:  First convert ASCII numbers to binary, ;
  886. ; parse out delimiting semi-colons, and quote string delimiters.     ;
  887. ;--------------------------------------------------------------------;
  888.  
  889. REASSIGNMENT:  TEST    STATUS,POFF             ;:If assignment are OFF,
  890.                JZ      NOT_POFF                ;:   then exit as if
  891.                JMP     ASSIGN_FLUSH            ;:   we are out of room.
  892. NOT_POFF:                                      ;:
  893.                MOV     CX,ESC_COUNT            ;Retrieve length of Esc sequence.
  894.                DEC     CX                          ;Adjust.
  895.                MOV     SI,OFFSET ESC_BUFFER + 2    ;Point past Esc, bracket.
  896.                MOV     DI,OFFSET PARSE_BUFFER      ;Point to parse storage.
  897. NEXT_STRING:   MOV     QUOTE_TYPE,0                ;Reset quote type.
  898. NEXT_NUM:      XOR     DL,DL                   ;Use DL to carry number; init = 0
  899.                XOR     BP,BP                   ;Use BP as number flag.
  900. NEXT_PARAM:    LODSB                           ;Retrieve a byte.
  901.                DEC     CX                      ;Decrement string length counter.
  902.                JZ      PARSE_END               ;If zero, done.
  903.                MOV     AH,QUOTE_TYPE           ;Else, retrieve quote type.
  904.                OR      AH,AH                   ;If zero, not in string.
  905.                JNZ     QUOTE_MODE              ;Else, go to string mode.
  906.                CALL    CK_QUOTE                ;Is character a quote?
  907.                JZ      NEXT_PARAM              ;If yes, ignore.
  908.                CMP     AL,";"                  ;Else, is it semi-colon?
  909.                JZ      STORE_NUMBER            ;If yes, number delimiter.
  910.  
  911.                SUB     AL,"0"                  ;Else, must be number; convert
  912.                MOV     DH,AL                   ; to binary; save in DH.
  913.                MOV     AX,10                   ;Multiply current total by ten.
  914.                MUL     DL
  915.                ADD     AL,DH                   ;Add new number.
  916.                MOV     DL,AL                   ;Save in DL.
  917.                MOV     BP,1                    ;Flag that number mode active.
  918.                JMP     SHORT NEXT_PARAM        ;Next parameter.
  919.  
  920. STORE_NUMBER:  OR      BP,BP                   ;If number mode flag not set then
  921.                JZ      NEXT_PARAM              ;semi-colon not prefaced with no.
  922.                MOV     AL,DL                   ;Else, store number.
  923.                STOSB
  924.                JMP     SHORT NEXT_NUM          ;Reset number carrying registers.
  925.  
  926. QUOTE_MODE:    CMP     AL,AH                   ;Is current char a string ending
  927.                JZ      NEXT_STRING             ; quote?  If yes, exit quote mode
  928.                STOSB                           ;Else, store char as literal.
  929.                JMP     SHORT NEXT_PARAM
  930.  
  931. ;----------------------------------------------------------------------------;
  932. ; Remove duplicate assignments if removal leaves room for new assignment.    ;
  933. ; If no duplicate and room, add new assignment, else flush buffer to screen. ;
  934. ;----------------------------------------------------------------------------;
  935.  
  936. PARSE_END:     OR      BP,BP                   ;Was last parameter a number?
  937.                JZ      CK_LENGTH               ;If no, skip.
  938.                MOV     AL,DL                   ;Else, store the last number.
  939.                STOSB
  940. CK_LENGTH:     SUB     DI,OFFSET PARSE_BUFFER - 2   ;Calculate string length
  941.                MOV     AX,DI                        ; including word for length.
  942.                MOV     BX,WORD PTR PARSE_BUFFER     ;BL=new first ASCII; BH=2nd.
  943.                MOV     CX,5                         ;String length has to be at
  944.                OR      BL,BL                   ;least word + 2 for old + new = 5
  945.                JZ      CK_LEGAL                ; for extended key reassignment.
  946.                DEC     CX                      ;And word + 1 for old + new = 4
  947. CK_LEGAL:      CMP     AX,CX                   ; for regular ASCII reassignment.
  948.                JB      ASSIGN_FLUSH            ;If not, illegal; flush buffer.
  949.                MOV     BP,BUFFER_END           ;BP to carry buffer end pointer.
  950.                CALL    CK_MATCH                ;Is char already reassigned?
  951.                JC      CK_ROOM                 ;If no, check room for new.
  952.  
  953. CK_REMOVE:     MOV     CX,DX                   ;REASSIGN_END - string end
  954.                SUB     CX,SI                   ; = bytes to move.
  955.                JZ      CK_ROOM                 ;If zero, last string.
  956.                SUB     DX,[DI]                 ;Is REASSIGN_END - old string
  957.                ADD     DX,AX                   ; + new string >
  958.                CMP     DX,BP                   ; BUFFER_END?
  959.                JA      ASSIGN_FLUSH            ;If yes, flush buffer to screen.
  960.                REP     MOVSB                   ;Else, move them down in buffer.
  961.                JMP     SHORT STORE_NEW
  962.  
  963. CK_ROOM:       ADD     DX,AX                   ;New string + current strings.
  964.                CMP     DX,BP                   ;Greater than buffer size?
  965.                JA      ASSIGN_FLUSH            ;If yes, flush new string.
  966. STORE_NEW:     MOV     SI,OFFSET PARSE_BUFFER  ;Else, room for new string.
  967.                STOSW                           ;Store string length first.
  968.                MOV     CX,AX                   ;Adjust counter.
  969.                DEC     CX
  970.                DEC     CX
  971.                REP     MOVSB                   ;Store the actual string.
  972.                MOV     REASSIGN_END,DI         ;Store new string end.
  973.                RET
  974.  
  975. ASSIGN_FLUSH:  MOV     AL,"p"                  ;If buffer full, flush string
  976.                JMP     FLUSH_BUFFER            ; including ending "p" command.
  977.  
  978. ;------------------------------------------------------------------------------;
  979. ; INPUT:  BL = first ASCII code to match; BH = extended if BL = 0              ;
  980. ; OUTPUT: CY = 1 if no match found; CY = 0 if match found.                     ;
  981. ;         DI points to string length of match; DI+2 points to start of string. ;
  982. ;         SI = end of string; DX = REASSIGN_END; BP = BUFFER_END               ;
  983. ;------------------------------------------------------------------------------;
  984.  
  985. CK_MATCH:      MOV     DX,REASSIGN_END                ;Current strings end.
  986.                MOV     SI,OFFSET REASSIGNMENT_BUFFER  ;Point to buffer.
  987. NEXT_MATCH:    MOV     DI,SI                          ;Current record.
  988.                CMP     DI,DX                   ;End of strings?
  989.                JAE     NO_MATCH                ;If yes, no match.
  990.                MOV     CX,[DI+2]               ;CL=current first ASCII; CH=2nd
  991.                ADD     SI,[DI]                 ;Point to next record.
  992.                CMP     BL,CL                   ;First characters match?
  993.                JNZ     NEXT_MATCH              ;If no, check next record.
  994.                OR      BL,BL                   ;Extended code? ie zero.
  995.                JNZ     MATCH                   ;If no, then match.
  996.                CMP     BH,CH                   ;Else, check second code.
  997.                JNZ     NEXT_MATCH              ;If not same, no match.
  998. MATCH:         CLC                             ;Else return with CY = 0
  999.                RET
  1000.  
  1001. NO_MATCH:      STC                             ;No match; CY = 1.
  1002.                RET
  1003.  
  1004. ;----------------------------------------------------------------------;
  1005. ; Buffer area will write over disposable data and initialization code. ;
  1006. ;----------------------------------------------------------------------;
  1007.  
  1008. ESC_BUFFER            =       $
  1009. NUMBER_BUFFER         =       ESC_BUFFER + ESC_BUFFER_SIZE
  1010. PARSE_BUFFER          =       NUMBER_BUFFER
  1011. DEVICE_STATUS_BUFFER  =       PARSE_BUFFER + ESC_BUFFER_SIZE
  1012. DEVICE_STATUS_SIZE    =       11
  1013. REASSIGNMENT_BUFFER   =       DEVICE_STATUS_BUFFER + DEVICE_STATUS_SIZE
  1014.  
  1015. ;              DISPOSABLE DATA
  1016. ;              ---------------
  1017.  
  1018. SYNTAX         LABEL   BYTE
  1019. DB      "Syntax:  ANSI [FAST | SLOW][ON | OFF][KON | KOFF][PON | POFF]",CR,LF ;:
  1020. DB      "              [/B nnn][/C][/Q][/U]",CR,LF ;:                       ;:
  1021. DB      "FAST     = direct screen writes; default",CR,LF                    ;:
  1022. DB      "SLOW     = screen writes via BIOS",CR,LF                           ;:
  1023. DB      "ON/OFF   = active/inactive; default is ON",CR,LF                   ;:
  1024. DB      "KON/KOFF = active/inactive reassignments; default is ON",CR,LF     ;:
  1025. DB      "PON/POFF = active/inactive NEW reassignments; default is ON",CR,LF ;:
  1026. DB      "nnn = buffer size in bytes (0 - 60K) reserved for key reassignment; "
  1027. DB      "default 200",CR,LF
  1028. DB      "/Q  = Quiet, no output when executed",CR,LF                        ;:
  1029. DB      "/U  = Uninstall"                                                   ;:
  1030. CR_LF   DB     CR,LF,LF,"$"
  1031.  
  1032. STATUS_MSG     DB      "Status: $"
  1033. BUFFER_MSG     DB      CR,LF,"Buffer size: $"
  1034. BYTES_FREE     DB      CR,LF,"Bytes free:  $"
  1035. ANSI_SYS_MSG   DB      "ANSI.SYS is installed so "
  1036. NOT_INSTALLED  DB      "ANSI.COM not installed",CR,LF,"$"
  1037. CON            DB      "CON"
  1038. CON_OFFSET     EQU     10
  1039. QUIET          DB      0                             ;| Quiet Mode
  1040.  
  1041. SIZE_MSG       DB      "Uninstall to change buffer size",CR,LF,LF,"$"
  1042. UNLOAD_MSG     DB      "ANSI can't be uninstalled",CR,LF
  1043.                DB      "Uninstall resident programs in reverse order",CR,LF,"$"
  1044. NOT_ENOUGH     DB      "Not enough memory",CR,LF,"$"
  1045. ALLOCATE_MSG   DB      "Memory allocation error",CR,LF,BELL,"$"
  1046. INSTALL_MSG    DB      "Installed",CR,LF,"$"
  1047. UNINSTALL_MSG  DB      "Uninstalled",CR,LF,"$"
  1048.  
  1049. ;--------------------------------------------------------------------;
  1050. ; Search memory for a copy of our code, to see if already installed. ;
  1051. ;--------------------------------------------------------------------;
  1052.  
  1053. INITIALIZE     PROC    NEAR
  1054.                CLD                             ;All string operations forward.
  1055.                MOV     BX,OFFSET START         ;Point to start of code.
  1056.                NOT     BYTE PTR [BX]           ;Change a byte so no false match.
  1057.                XOR     DX,DX                   ;Start at segment zero.
  1058.                MOV     AX,CS                   ;Store our segment in AX.
  1059. NEXT_PARAG:    INC     DX                      ;Next paragraph.
  1060.                MOV     ES,DX
  1061.                CMP     DX,AX                   ;Is it our segment?
  1062.                JZ      ANNOUNCE                ;If yes, search is done.
  1063.                MOV     SI,BX                   ;Else, point to our signature.
  1064.                MOV     DI,BX                   ; and offset of possible match.
  1065.                MOV     CX,16                   ;Check 16 bytes for match.
  1066.                REP     CMPSB
  1067.                JNZ     NEXT_PARAG              ;If no match, keep looking.
  1068.  
  1069. ;------------------------------------------------;
  1070.  
  1071. ANNOUNCE:                                      ;|
  1072.                MOV     SI,81H                  ;Point to command line.
  1073. NEXT_CAP:      LODSB                           ;Capitalize parameters.
  1074.                CMP     AL,CR
  1075.                JZ      PARSE
  1076.                CMP     AL,"a"
  1077.                JB      NEXT_CAP
  1078.                CMP     AL,"z"
  1079.                JA      NEXT_CAP
  1080.                AND     BYTE PTR [SI - 1],5FH
  1081.                JMP     SHORT NEXT_CAP
  1082.  
  1083. ;------------------------------------------------;
  1084.  
  1085. PARSE:         MOV     SI,81H                  ;Point to command line again.
  1086. NEXT_PARA:     XOR     AX,AX                   ;Position in status parameters.
  1087.                MOV     BX,4                    ;Status parameters each 4 bytes.
  1088. NEXT_STATUS:   MOV     DI,OFFSET PARAMETERS    ;Point to "OFF ON  SLOWFAST"
  1089.                ADD     DI,AX                   ;Point to next parameter.
  1090.                ADD     AX,BX
  1091.                CMP     AX,LAST_PARAMETER       ;:Check all 8 possible statuses
  1092.                JA      CK_SWITCHES
  1093.                PUSH    SI                      ;Save command line pointer.
  1094.                MOV     CX,2                    ;Check first two bytes for match.
  1095.                CMP     AX,20                   ;: 3 Bytes for
  1096.                JB      Len_OK                  ;:   KON | KOFF | PON | POFF
  1097.                INC     CX                      ;:
  1098. Len_OK:                                        ;:
  1099.                REP     CMPSB
  1100.                POP     SI                      ;Recover command line pointer.
  1101.                JNZ     NEXT_STATUS
  1102.  
  1103.                DIV     BL                      ;If match, divide offset by four.
  1104. ;:
  1105. ;:  Next 12 lines of routine changed
  1106. ;:
  1107. ;:             MOV     CL,1                    ;Set a bit to match offset.
  1108. ;:NEXT_SHIFT:  SHL     CL,1
  1109. ;:             DEC     AL
  1110. ;:             JNZ     NEXT_SHIFT
  1111. ;:             SHR     CL,1                    ;Adjust.
  1112. ;:             MOV     AH,STATUS_MASK          ;Retrieve appropriate ON/OFF
  1113. ;:             CMP     CL,ON                   ; or FAST/SLOW mask.
  1114. ;:             JBE     ADD_STATUS
  1115. ;:             ROL     AH,1
  1116. ;:             ROL     AH,1
  1117. ;:
  1118. ;:ADD_STATUS:  AND     ES:STATUS,AH            ;Mask off old status.
  1119. ;:             OR      ES:STATUS,CL            ;Add new status.
  1120. ;:
  1121.                MOV     CL,AL                   ;:place count in CL (1 - 8)
  1122.                MOV     AX,STATUS_MASK          ;:Retrieve appropriate ON/OFF
  1123.                                                ;:FAST|SLOW|KON|KOFF|PON|POFF
  1124.                DEC     CL                      ;:Make 1 - 8 into 0 - 7
  1125.                JZ      Skip_1                  ;:In position already if zero
  1126.                SHL     AL,CL                   ;:Shift bit into position
  1127. Skip_1:        SHR     CL,1                    ;:divide count by 2
  1128.                JZ      Skip_2                  ;:
  1129.                ROL     AH,CL                   ;:Shift Mask into position
  1130.                ROL     AH,CL                   ;:
  1131. Skip_2:                                        ;:
  1132.  
  1133.                AND     ES:STATUS,AH            ;Mask off old status.
  1134.                OR      ES:STATUS,AL            ;:Add new status.
  1135.                INC     SI                      ;Bump command line pointer.
  1136.                INC     SI
  1137.  
  1138. CK_SWITCHES:   LODSB                           ;Get a byte.
  1139.                CMP     AL,CR                   ;Is it carriage return?
  1140.                JZ      INSTALL                 ;If yes, done here.
  1141.                CMP     AL,"/"                  ;Is there a switch character?
  1142.                JNZ     NEXT_PARA               ;If no, keep looking.
  1143.                LODSB                           ;Else, get the switch character.
  1144.                CMP     AL,"B"                  ;Is it "B" ?
  1145.                JNZ     CK_C                    ;If no, check "C".
  1146.                CALL    CK_INSTALLED            ;Else, see if already installed.
  1147.                JZ      GET_SIZE                ;If no, get buffer request size.
  1148.                MOV     DX,OFFSET SIZE_MSG      ;Else, display error message.
  1149.                CALL    PRINT_STRING
  1150.                JMP     SHORT NEXT_PARA
  1151. GET_SIZE:      CALL    DECIMAL_INPUT           ;Get number parameter.
  1152.                CMP     BX,REASSIGNMENT_MAX     ;If greater than maximum, use
  1153.                JBE     STORE_BUFFER            ; maximum, else use requested
  1154.                MOV     BX,REASSIGNMENT_MAX     ; size.
  1155. STORE_BUFFER:  MOV     REASSIGNMENT_SIZE,BX
  1156.                ADD     BX,OFFSET REASSIGNMENT_BUFFER  ;Calculate end of buffer.
  1157.                MOV     BUFFER_END,BX
  1158.                JMP     SHORT NEXT_PARA
  1159.  
  1160. CK_C:          CMP     AL,"C"                  ;Is it "C" ?
  1161.                JNZ     CK_Q                    ;|If not, check "Q".
  1162.                MOV     ES:REASSIGN_END,OFFSET REASSIGNMENT_BUFFER  ;Clear buffer
  1163. CK_Q:          CMP     AL,"Q"                  ;|See if Quiet Mode
  1164.                JNZ     CK_U                    ;|If not, check "U".
  1165.                NOT     QUIET                   ;|If yes turn on Quiet
  1166. CK_U:          CMP     AL,"U"                  ;Is it "U" ?
  1167.                JZ      DO_U                    ;If yes, try to uninstall.
  1168.                JMP     NEXT_PARA               ;Else, next parmater.
  1169. DO_U:          CALL    CK_INSTALLED            ;Else, see if installed.
  1170.                MOV     DX,OFFSET NOT_INSTALLED ;If no, exit with error message.
  1171.                JZ      LILLY_PAD               ;Too far for short jump.
  1172.                JMP     UNINSTALL               ;Else, uninstall.
  1173.  
  1174. ;------------------------------------------------;
  1175.  
  1176. INSTALL:
  1177.                MOV     DX,OFFSET SIGNATURE     ;|Display our signature.
  1178.                CALL    PRINT_STRING            ;|
  1179.                MOV     DX,OFFSET SYNTAX        ;|And syntax.
  1180.                CALL    PRINT_STRING            ;|
  1181.                CALL    STATUS_REPORT           ;|Display status.
  1182.                CALL    CK_INSTALLED            ;Check if already installed.
  1183.                JZ      CK_AVAILABLE            ;If no, see if enough memory.
  1184.                OR      AL,AL                   ;Else, done.
  1185.                JMP     EXIT                    ;Exit with ERRORLEVEL = 0.
  1186.  
  1187. ;--------------------------------;
  1188. ; This is the install procedure. ;
  1189. ;--------------------------------;
  1190.  
  1191. CK_AVAILABLE:  MOV     BP,OFFSET REASSIGNMENT_BUFFER   ;TSR ends at end
  1192.                ADD     BP,REASSIGNMENT_SIZE            ; of reassignment buffer.
  1193.                ADD     BP,15                           ;Round up.
  1194.                CMP     BP,DS:[6]               ;Buffer > PSP bytes in segment?
  1195.                MOV     DX,OFFSET NOT_ENOUGH    ;If yes, exit without installing
  1196.                JA      MSG_EXIT                ; with message.
  1197.  
  1198.                MOV     AX,3529H                ;Get undocumented INT 29 vector.
  1199.                INT     21H
  1200.                MOV     SI,OFFSET CON           ;Does it point to ANSI.SYS?
  1201.                MOV     DI,CON_OFFSET           ;Check by looking for "CON"
  1202.                MOV     CX,3                    ; as device name.
  1203.                REP     CMPSB
  1204.                MOV     DX,OFFSET ANSI_SYS_MSG  ;Exit with error message if
  1205. LILLY_PAD:     JZ      MSG_EXIT                ;ANSI.SYS loaded.
  1206.  
  1207.                MOV     OLD_INT_29[0],BX        ;Else save old interrupt.
  1208.                MOV     OLD_INT_29[2],ES
  1209.                MOV     DX,OFFSET ANSI_INT_29   ;Install new interrupt.
  1210.                MOV     AX,2529H
  1211.                INT     21H
  1212.                MOV     AX,3516H                ;Get INT 16 vector.
  1213.                INT     21H
  1214.                MOV     OLD_INT_16[0],BX        ;Save old interrupt.
  1215.                MOV     OLD_INT_16[2],ES
  1216.                MOV     DX,OFFSET ANSI_INT_16   ;Install new interrupt.
  1217.                MOV     AX,2516H
  1218.                INT     21H
  1219.                MOV     AX,3521H                ;Get DOS 21h interrupt.
  1220.                INT     21H
  1221.                MOV     OLD_INT_21[0],BX        ;Save old interrupt.
  1222.                MOV     OLD_INT_21[2],ES
  1223.                MOV     DX,OFFSET ANSI_INT_21   ;Install new interrupt.
  1224.                MOV     AX,2521H
  1225.                INT     21H
  1226.  
  1227.                MOV     AX,DS:[2CH]             ;Get environment segment.
  1228.                MOV     ES,AX
  1229.                MOV     AH,49H                  ;Free up environment.
  1230.                INT     21H
  1231.  
  1232.                MOV     DX,OFFSET INSTALL_MSG   ;Display install message.
  1233.                CALL    PRINT_STRING
  1234.                CALL    FLUSH_END               ;Setup the number buffer.
  1235.                MOV     DX,BP                   ;Retrieve resident byte request.
  1236.                MOV     CL,4
  1237.                SHR     DX,CL                   ;Convert to paragraphs.
  1238.                MOV     AX,3100H                ;Return error code of zero.
  1239.                INT     21H                     ;Terminate but stay resident.
  1240.  
  1241. ;-------------------------------------------------------------------;
  1242. ; Exit.  Return ERRORLEVEL code 0 if successful, 1 if unsuccessful. ;
  1243. ;-------------------------------------------------------------------;
  1244.  
  1245. MSG_EXIT:      CALL    PRINT_STRING
  1246. ERROR_EXIT:    MOV     AL,1                    ;ERRORLEVEL = 1.
  1247. EXIT:          MOV     AH,4CH                  ;Terminate.
  1248.                INT     21H
  1249.  
  1250. ;---------------------------------------------------;
  1251. ; This subroutine uninstalls the resident ANSI.COM. ;
  1252. ;---------------------------------------------------;
  1253.  
  1254. UNINSTALL:     AND     ES:STATUS,NOT ON        ;Turn OFF ANSI, just in case
  1255.                OR      ES:STATUS,OFF           ; can't uninstall.
  1256.                MOV     CX,ES                   ;Save segment in CX.
  1257.                MOV     AX,3529H                ;Get interrupt 29h.
  1258.                INT     21H
  1259.                CMP     BX,OFFSET ANSI_INT_29   ;Has it been hooked by another?
  1260.                JNZ     UNINSTALL_ERR           ;If yes, exit with error message.
  1261.                MOV     BX,ES
  1262.                CMP     BX,CX                   ;Is the segment vector same?
  1263.                JNZ     UNINSTALL_ERR           ;If no, exit with error message.
  1264.  
  1265.                MOV     AX,3516H                ;Get interrupt 16h.
  1266.                INT     21H
  1267.                CMP     BX,OFFSET ANSI_INT_16   ;Has it been hooked by another?
  1268.                JNZ     UNINSTALL_ERR           ;If yes, exit with error message.
  1269.                MOV     BX,ES
  1270.                CMP     BX,CX                   ;Is the segment vector same?
  1271.                JNZ     UNINSTALL_ERR           ;If no, exit with error message.
  1272.  
  1273.                MOV     AX,3521H                ;Get interrupt 21h.
  1274.                INT     21H
  1275.                CMP     BX,OFFSET ANSI_INT_21   ;Has it been hooked by another?
  1276.                JNZ     UNINSTALL_ERR           ;If yes, exit with error message.
  1277.                MOV     BX,ES
  1278.                CMP     BX,CX                   ;Is the segment vector same?
  1279.                JNZ     UNINSTALL_ERR           ;If no, exit with error message.
  1280.  
  1281.                MOV     AH,49H                  ;Return memory to system pool.
  1282.                INT     21H
  1283.                MOV     DX,OFFSET ALLOCATE_MSG
  1284.                JC      MSG_EXIT                ;Display message if problem.
  1285.  
  1286.                MOV     DX,ES:OLD_INT_29[0]     ;Restore old INT 29.
  1287.                MOV     DS,ES:OLD_INT_29[2]
  1288.                MOV     AX,2529H
  1289.                INT     21H
  1290.                MOV     DX,ES:OLD_INT_16[0]     ;Restore old INT 16.
  1291.                MOV     DS,ES:OLD_INT_16[2]
  1292.                MOV     AX,2516H
  1293.                INT     21H
  1294.                MOV     DX,ES:OLD_INT_21[0]     ;Restore old INT 21.
  1295.                MOV     DS,ES:OLD_INT_21[2]
  1296.                MOV     AX,2521H
  1297.                INT     21H
  1298.  
  1299.                PUSH    CS
  1300.                POP     DS                      ;Point to our data.
  1301.                MOV     DX,OFFSET UNINSTALL_MSG ;Display uninstall message.
  1302.                CALL    PRINT_STRING
  1303.                OR      AL,AL                   ;Exit with ERRORLEVEL = 0.
  1304.                JMP     EXIT
  1305.  
  1306. UNINSTALL_ERR: MOV     ES,CX                   ;If error, display OFF status.
  1307.                CALL    STATUS_REPORT
  1308.                MOV     DX,OFFSET UNLOAD_MSG    ;And exit with error message.
  1309.                JMP     MSG_EXIT
  1310. INITIALIZE     ENDP
  1311.  
  1312. ;--------------------------------------------------;
  1313. ; INPUT:  SI points to parameter start.            ;
  1314. ; OUTPUT: SI points to parameter end; BX = number. ;
  1315. ;--------------------------------------------------;
  1316.  
  1317. DECIMAL_INPUT  PROC    NEAR
  1318.                XOR     BX,BX                   ;Start with zero as number.
  1319. NEXT_DECIMAL:  LODSB                           ;Get a character.
  1320.                CMP     AL,CR                   ;Carriage return?
  1321.                JZ      ADJUST_DEC              ;If yes, done here.
  1322.                CMP     AL,"/"                  ;Forward slash?
  1323.                JZ      ADJUST_DEC              ;If yes, done here.
  1324.                SUB     AL,"0"                  ;ASCII to binary.
  1325.                JC      NEXT_DECIMAL            ;If not between 0 and 9, skip.
  1326.                CMP     AL,9
  1327.                JA      NEXT_DECIMAL
  1328.                CBW                             ;Convert byte to word.
  1329.                XCHG    AX,BX                   ;Swap old and new number.
  1330.                MOV     CX,10                   ;Shift to left by multiplying
  1331.                MUL     CX                      ; last entry by ten.
  1332.                JC      DECIMAL_ERROR           ;If carry, too big.
  1333.                ADD     BX,AX                   ;Add new number and store in BX.
  1334.                JNC     NEXT_DECIMAL            ;If not carry, next number.
  1335. DECIMAL_ERROR: MOV     BX,-1                   ;Else, too big; return -1.
  1336.  
  1337. ADJUST_DEC:    DEC     SI                      ;Adjust pointer.
  1338.                RET
  1339. DECIMAL_INPUT  ENDP
  1340.  
  1341. ;-------------------------------------------------------;
  1342. ; OUTPUT: ZR = 1 if not installed; ZR = 0 if installed. ;
  1343. ;-------------------------------------------------------;
  1344.  
  1345. CK_INSTALLED:  MOV     AX,ES
  1346.                MOV     BX,CS
  1347.                CMP     AX,BX                   ;Compare segments.
  1348.                RET
  1349.  
  1350. ;------------------------------------------------;
  1351.  
  1352. STATUS_REPORT: MOV     DX,OFFSET STATUS_MSG
  1353.                CALL    PRINT_STRING            ;Display "Status: ".
  1354.                MOV     BL,ES:STATUS
  1355.                MOV     BH,1
  1356. NEXT_REPORT:   TEST    BL,BH
  1357.                JZ      LOOP_STATUS
  1358.  
  1359.                MOV     DL,BH
  1360. ;:
  1361. ;: Next 9 Lines changed
  1362. ;:
  1363. ;:             MOV     AX,-1
  1364. ;:NEXT_BIT:    INC     AX
  1365. ;:             SHR     DL,1
  1366. ;:             JNC     NEXT_BIT
  1367. ;:             SHL     AX,1
  1368. ;:             SHL     AX,1
  1369. ;:             MOV     SI,OFFSET PARAMETERS    ;Display appropriate ON or OFF,
  1370. ;:             ADD     SI,AX                   ; SLOW or FAST.
  1371. ;:             MOV     CX,4
  1372. ;:
  1373.                MOV     SI,OFFSET PARAMETERS    ;:Display appropriate ON or OFF,
  1374.                                                ;: SLOW|FAST.  KON|KOFF|PON|POFF
  1375.                MOV     CX,4                    ;:Each four characters long
  1376. NEXT_BIT:      SHR     DL,1                    ;:Is this our Word?
  1377.                JC      REPORT                  ;:Yes - Print it
  1378.                ADD     SI,CX                   ;:No  - Skip over it
  1379.                JMP     NEXT_BIT                ;:test the next bit
  1380. REPORT:        LODSB
  1381.                CALL    PRINT_CHAR
  1382.                LOOP    REPORT
  1383.                MOV     AL, " "                 ;: Space words
  1384.                CALL    PRINT_CHAR              ;:
  1385.  
  1386. LOOP_STATUS:   SHL     BH,1
  1387.                JNC     NEXT_REPORT             ;: Check all 8 bits
  1388.                                                ;:
  1389.                MOV     DX,OFFSET BUFFER_MSG    ;Display "Buffer size: ".
  1390.                CALL    PRINT_STRING
  1391.                MOV     AX,ES:REASSIGNMENT_SIZE
  1392.                PUSH    AX
  1393.                XOR     BP,BP
  1394.                CALL    DECIMAL_OUT             ;Display the size.
  1395.                MOV     DX,OFFSET BYTES_FREE    ;Display "Bytes free: ".
  1396.                CALL    PRINT_STRING
  1397.                POP     AX
  1398.                ADD     AX,OFFSET REASSIGNMENT_BUFFER
  1399.                SUB     AX,ES:REASSIGN_END
  1400.                CALL    DECIMAL_OUT             ;Display the bytes free.
  1401.                MOV     DX,OFFSET CR_LF
  1402.                CALL    PRINT_STRING
  1403.                RET
  1404.  
  1405. ;------------------------------------------------;
  1406.  
  1407. PRINT_CHAR:    MOV     DL,AL
  1408.                MOV     AH,2                    ;Print character via DOS.
  1409.                OR      AL,AL                   ;:Skip NULLs
  1410.                JNZ     SHORT DOS_INT           ;:
  1411.                RET                             ;:
  1412.  
  1413. PRINT_STRING:  MOV     AH,9                    ;Print string via DOS.
  1414. DOS_INT:       CMP     QUIET,0                 ;|Do we suppress output
  1415.                JNE     NO_PRINT                ;|Yes
  1416.                INT     21H                     ;|No
  1417. NO_PRINT:                                      ;|
  1418.                RET
  1419.  
  1420. _TEXT          ENDS
  1421.                END     START
  1422.