home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / dos_ency / 6 / ch2.asm < prev    next >
Encoding:
Assembly Source File  |  1988-08-11  |  7.3 KB  |  257 lines

  1.         TITLE   CH2.ASM
  2.  
  3. ; CH2.ASM -- support file for CTERM.C terminal emulator
  4. ;       for use with Microsoft C and SMALL model only...
  5.  
  6. _TEXT   segment byte public 'CODE'
  7. _TEXT   ends
  8. _DATA   segment byte public 'DATA'
  9. _DATA   ends
  10. CONST   segment byte public 'CONST'
  11. CONST   ends
  12. _BSS    segment byte public 'BSS'
  13. _BSS    ends
  14.  
  15. DGROUP  GROUP   CONST, _BSS, _DATA
  16.         assume  CS:_TEXT, DS:DGROUP, ES:DGROUP, SS:DGROUP
  17.  
  18. _TEXT   segment
  19.  
  20.         public  __cls,__color,__deol,__i_v,__key,__wrchr,__wrpos
  21.  
  22. atrib   DB      0               ; attribute
  23. _colr   DB      0               ; color
  24. v_bas   DW      0               ; video segment
  25. v_ulc   DW      0               ; upper left corner cursor
  26. v_lrc   DW      184Fh           ; lower right corner cursor
  27. v_col   DW      0               ; current col/row
  28.  
  29. __key   proc    near            ; get keystroke
  30.         PUSH    BP
  31.         MOV     AH,1            ; check status via BIOS
  32.         INT     16h
  33.         MOV     AX,0FFFFh
  34.         JZ      key00           ; none ready, return EOF
  35.         MOV     AH,0            ; have one, read via BIOS
  36.         INT     16h
  37. key00:  POP     BP
  38.         RET
  39. __key   endp
  40.  
  41. __wrchr proc    near
  42.         PUSH    BP
  43.         MOV     BP,SP
  44.         MOV     AL,[BP+4]       ; get char passed by C
  45.         CMP     AL,' '
  46.         JNB     prchr           ; printing char, go do it
  47.         CMP     AL,8
  48.         JNZ     notbs
  49.         DEC     BYTE PTR v_col  ; process backspace
  50.         MOV     AL,byte ptr v_col
  51.         CMP     AL,byte ptr v_ulc
  52.         JB      nxt_c           ; step to next column
  53.         JMP     norml
  54.  
  55. notbs:  CMP     AL,9
  56.         JNZ     notht
  57.         MOV     AL,byte ptr v_col       ; process HTAB
  58.         ADD     AL,8
  59.         AND     AL,0F8h
  60.         MOV     byte ptr v_col,AL
  61.         CMP     AL,byte ptr v_lrc
  62.         JA      nxt_c
  63.         JMP     SHORT   norml
  64.  
  65. notht:  CMP     AL,0Ah
  66.         JNZ     notlf
  67.         MOV     AL,byte ptr v_col+1     ; process linefeed
  68.         INC     AL
  69.         CMP     AL,byte ptr v_lrc+1
  70.         JBE     noht1
  71.         CALL    scrol
  72.         MOV     AL,byte ptr v_lrc+1
  73. noht1:  MOV     byte ptr v_col+1,AL
  74.         JMP     SHORT   norml
  75.  
  76. notlf:  CMP     AL,0Ch
  77.         JNZ     ck_cr
  78.         CALL    __cls           ; process formfeed
  79.         JMP     SHORT   ignor
  80.  
  81. ck_cr:  CMP     AL,0Dh
  82.         JNZ     ignor           ; ignore all other CTL chars
  83.         MOV     AL,byte ptr v_ulc       ; process CR
  84.         MOV     byte ptr v_col,AL
  85.         JMP     SHORT   norml
  86.  
  87. prchr:  MOV     AH,_colr        ; process printing char
  88.         PUSH    AX
  89.         XOR     AH,AH
  90.         MOV     AL,byte ptr v_col+1
  91.         PUSH    AX
  92.         MOV     AL,byte ptr v_col
  93.         PUSH    AX
  94.         CALL    wrtvr
  95.         MOV     SP,BP
  96. nxt_c:  INC     BYTE PTR v_col  ; advance to next column
  97.         MOV     AL,byte ptr v_col
  98.         CMP     AL,byte ptr v_lrc
  99.         JLE     norml
  100.         MOV     AL,0Dh          ; went off end, do CR/LF
  101.         PUSH    AX
  102.         CALL    __wrchr
  103.         POP     AX
  104.         MOV     AL,0Ah
  105.         PUSH    AX
  106.         CALL    __wrchr
  107.         POP     AX
  108. norml:  CALL    set_cur
  109. ignor:  MOV     SP,BP
  110.         POP     BP
  111.         RET
  112. __wrchr endp
  113.  
  114. __i_v   proc    near            ; establish video base segment
  115.         PUSH    BP
  116.         MOV     BP,SP
  117.         MOV     AX,0B000h       ; mono, B800 for CGA
  118.         MOV     v_bas,AX        ; could be made automatic
  119.         MOV     SP,BP
  120.         POP     BP
  121.         RET
  122. __i_v   endp
  123.  
  124. __wrpos proc    near            ; set cursor position
  125.         PUSH    BP
  126.         MOV     BP,SP
  127.         MOV     DH,[BP+4]       ; row from C program
  128.         MOV     DL,[BP+6]       ; col from C program
  129.         MOV     v_col,DX        ; cursor position
  130.         MOV     BH,atrib        ; attribute
  131.         MOV     AH,2
  132.         PUSH    BP
  133.         INT     10h
  134.         POP     BP
  135.         MOV     AX,v_col        ; return cursor position
  136.         MOV     SP,BP
  137.         POP     BP
  138.         RET
  139. __wrpos endp
  140.  
  141. set_cur proc    near            ; set cursor to v_col
  142.         PUSH    BP
  143.         MOV     BP,SP
  144.         MOV     DX,v_col        ; use where v_col says
  145.         MOV     BH,atrib
  146.         MOV     AH,2
  147.         PUSH    BP
  148.         INT     10h
  149.         POP     BP
  150.         MOV     AX,v_col
  151.         MOV     SP,BP
  152.         POP     BP
  153.         RET
  154. set_cur endp
  155.  
  156. __color proc    near            ; _color(fg, bg)
  157.         PUSH    BP
  158.         MOV     BP,SP
  159.         MOV     AH,[BP+6]       ; background from C
  160.         MOV     AL,[BP+4]       ; foreground from C
  161.         MOV     CX,4
  162.         SHL     AH,CL
  163.         AND     AL,0Fh
  164.         OR      AL,AH           ; pack up into 1 byte
  165.         MOV     _colr,AL        ; store for handler's use
  166.         XOR     AH,AH
  167.         MOV     SP,BP
  168.         POP     BP
  169.         RET
  170. __color endp
  171.  
  172. scrol   proc    near            ; scroll CRT up by one line
  173.         PUSH    BP
  174.         MOV     BP,SP
  175.         MOV     AL,1            ; count of lines to scroll
  176.         MOV     CX,v_ulc
  177.         MOV     DX,v_lrc
  178.         MOV     BH,_colr
  179.         MOV     AH,6
  180.         PUSH    BP
  181.         INT     10h             ; use BIOS
  182.         POP     BP
  183.         MOV     SP,BP
  184.         POP     BP
  185.         RET
  186. scrol   endp
  187.  
  188. __cls   proc    near            ; clear CRT
  189.         PUSH    BP
  190.         MOV     BP,SP
  191.         MOV     AL,0            ; flags CLS to BIOS
  192.         MOV     CX,v_ulc
  193.         MOV     v_col,CX        ; set to HOME
  194.         MOV     DX,v_lrc
  195.         MOV     BH,_colr
  196.         MOV     AH,6
  197.         PUSH    BP
  198.         INT     10h             ; use BIOS scroll up
  199.         POP     BP
  200.         CALL    set_cur         ; cursor to HOME
  201.         MOV     SP,BP
  202.         POP     BP
  203.         RET
  204. __cls   endp
  205.  
  206. __deol  proc    near            ; delete to end of line
  207.         PUSH    BP
  208.         MOV     BP,SP
  209.         MOV     AL,' '
  210.         MOV     AH,_colr        ; set up blanks
  211.         PUSH    AX
  212.         MOV     AL,byte ptr v_col+1
  213.         XOR     AH,AH           ; set up row value
  214.         PUSH    AX
  215.         MOV     AL,byte ptr v_col
  216.  
  217. deol1:  CMP     AL,byte ptr v_lrc
  218.         JA      deol2           ; at RH edge 
  219.         PUSH    AX              ; current location
  220.         CALL    wrtvr           ; write a blank
  221.         POP     AX
  222.         INC     AL              ; next column
  223.         JMP     deol1           ; do it again
  224.  
  225. deol2:  MOV     AX,v_col        ; return cursor position
  226.         MOV     SP,BP
  227.         POP     BP
  228.         RET
  229. __deol  endp
  230.  
  231. wrtvr   proc    near            ; write video RAM (col, row, char/atr)
  232.         PUSH    BP
  233.         MOV     BP,SP           ; set up arg ptr
  234.         MOV     DL,[BP+4]       ; column
  235.         MOV     DH,[BP+6]       ; row
  236.         MOV     BX,[BP+8]       ; char/atr
  237.         MOV     AL,80           ; calc offset
  238.         MUL     DH
  239.         XOR     DH,DH
  240.         ADD     AX,DX
  241.         ADD     AX,AX           ; adjust bytes to words
  242.         PUSH    ES              ; save seg reg
  243.         MOV     DI,AX
  244.         MOV     AX,v_bas        ; set up segment
  245.         MOV     ES,AX
  246.         MOV     AX,BX           ; get the data
  247.         STOSW                   ; put on screen
  248.         POP     ES              ; restore regs
  249.         MOV     SP,BP
  250.         POP     BP
  251.         RET
  252. wrtvr   endp
  253.  
  254. _TEXT   ends
  255.  
  256.         END
  257.