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