home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 December / simtel1292_SIMTEL_1292_Walnut_Creek.iso / msdos / turbopas / bonus507.arc / EXECWIN.ARC / EXECWIN.ASM next >
Assembly Source File  |  1988-12-11  |  8KB  |  240 lines

  1. ;EXECWIN.ASM
  2. ;Written by Kim Kokkonen, TurboPower Software
  3. ;10/88
  4. ;Released to the public domain
  5. ;Version 1.01 11/14/88
  6. ;  save BP register since some int 10 BIOS handlers trash it
  7.  
  8. col        equ     (byte ptr 0)
  9. row        equ     (byte ptr 1)
  10.  
  11. ofst       equ     (word ptr 0)
  12. segm       equ     (word ptr 2)
  13.  
  14. ToDos      macro                        ;Transfer control to DOS
  15.            jmp  dword ptr Int21CS
  16.            endm
  17.  
  18. ToApp      macro                        ;Transfer control back to caller
  19.            clc                          ;Clear error flag
  20.            ret     2                    ;Return with flags intact
  21.            endm
  22.  
  23. data       segment word public
  24.            extrn   SaveInt21 : dword    ;Previous $21 vector
  25.            extrn   WindPos   : word     ;Cursor position in window
  26.            extrn   WindLo    : word     ;Top left corner of window
  27.            extrn   WindHi    : word     ;   and bottom right corner.
  28.            extrn   WindAttr  : byte     ;Attribute with which to
  29.                                         ;   display all characters.
  30. data       ends
  31.  
  32. code       segment byte public
  33.            assume  cs:code,ds:data,es:nothing
  34.            public  SetCsInts
  35.            public  NewInt21
  36.  
  37. Int21CS    dd      ?                    ;Old interrupt 21 in code segment
  38.  
  39. ; Save ints in code segment
  40. SetCsInts  proc    near
  41.            les     ax,SaveInt21
  42.            mov     Int21CS.ofst,ax
  43.            mov     Int21CS.segm,es
  44.            ret
  45. SetCsInts  endp
  46.  
  47. ; Handle interrupt 21 to trap output calls
  48. NewInt21   proc    far
  49.  
  50.            cmp     ah,2                 ;Just get functions that go to StdOut
  51.            jz      DispOut
  52.            cmp     ah,6
  53.            jz      DirectOut
  54.            cmp     ah,9
  55.            jz      StringOut
  56.            cmp     ah,40h               ;Or maybe to StdErr
  57.            jz      BlockOut
  58.            ToDos
  59.  
  60. ;-----------
  61. DispOut:                                ;DOS function 2
  62.            push    ax
  63.            mov     al,dl                ;Character to write in AL
  64.            call    WriteChar            ;Write via video BIOS
  65.            pop     ax
  66.            ToApp                        ;Return successfully
  67.  
  68. ;-----------
  69. DirectOut:                              ;DOS function 6
  70.            cmp     dl,0FFh              ;Console input?
  71.            jnz     DispOut              ;Jump if not
  72.            ToDos                        ;Else transfer to DOS
  73.  
  74. ;------------
  75. StringOut:                              ;DOS function 9
  76.            push    ax                   ;Save AX
  77.            push    bx                   ;Save string index
  78.            mov     bx,dx                ;DS:BX -> string
  79. StringOut1:
  80.            mov     al,[bx]              ;AL = next character to write
  81.            cmp     al,'$'               ;Terminator?
  82.            jz      StringOut2           ;Exit if so
  83.            call    WriteChar            ;Write it
  84.            inc     bx                   ;Next character
  85.            jmp     StringOut1           ;Loop
  86. StringOut2:
  87.            pop     bx
  88.            pop     ax
  89.            ToApp                        ;Back to application
  90.  
  91. ;------------
  92. BlockOut:                               ;DOS function 40h
  93.            cmp     bx,1                 ;To StdOut?
  94.            jz      BlockOut1            ;Jump if so
  95.            cmp     bx,2                 ;To StdErr?
  96.            jz      BlockOut1            ;Jump if so
  97.            ToDos                        ;Else let DOS handle it
  98. BlockOut1:
  99.            jcxz    BlockOut3            ;Get out if none to write
  100.            push    ax
  101.            push    bx
  102.            push    cx                   ;Save loop counter
  103.            mov     bx,dx                ;DS:BX -> stuff to write
  104. BlockOut2:
  105.            mov     al,[bx]              ;Next character to write
  106.            call    WriteChar            ;Write it
  107.            inc     bx                   ;Next index
  108.            loop    BlockOut2            ;Loop for all the characters
  109.            pop     cx
  110.            pop     bx
  111.            pop     ax
  112.            mov     ax,cx                ;Wrote all the characters
  113. BlockOut3:
  114.            ToApp                        ;Back to application
  115.  
  116. NewInt21 endp
  117.  
  118. ;------------
  119. ; Write a character to current position via BIOS
  120. ; Entry: AL is character to write
  121. ; Must preserve all but AX
  122. WriteChar  proc    near
  123.            push    bp                   ;some versions of int 10 BIOS trash BP
  124.            push    bx
  125.            push    cx
  126.            push    dx
  127.            push    ds
  128.  
  129.            mov     bx,seg data          ;set up ds
  130.            mov     ds,bx
  131.  
  132.            cmp     al,7                 ;Bell character?
  133.            jz      BiosWriteDone        ;Don't write
  134.  
  135.            mov     dx,WindPos           ;Current cursor pos in DX
  136.  
  137.            cmp     al,8                 ;Backspace?
  138.            jz      BackSpace
  139.            cmp     al,9                 ;Tab?
  140.            jz      Tab
  141.            cmp     al,10                ;Line feed?
  142.            jz      LineFeed
  143.            cmp     al,13                ;Carriage return?
  144.            jz      Carriage
  145.  
  146.            call    WriteOne             ;Write one normal character
  147.  
  148. BiosSetCursor:                          ;Position cursor
  149.            xor     bh,bh
  150.            mov     ah,2
  151.            int     10h
  152.            mov     WindPos,dx           ;Save new cursor position
  153.  
  154. BiosWriteDone:
  155.            pop     ds
  156.            pop     dx
  157.            pop     cx
  158.            pop     bx
  159.            pop     bp
  160.            ret
  161.  
  162. Carriage:  mov     dl,WindLo.col        ;Move to left edge
  163.            jmp     BiosSetCursor
  164.  
  165. LineFeed:  cmp     dh,WindHi.row        ;Room to increment row?
  166.            jb      LineFeed1
  167.            mov     ax,0601h             ;Scroll up one line
  168.            mov     cx,WindLo
  169.            mov     dx,WindHi
  170.            mov     bh,WindAttr
  171.            int     10h
  172.            jmp     BiosWriteDone
  173. LineFeed1: inc     dh                   ;Increment row
  174.            jmp     BiosSetCursor        ;Set cursor
  175.  
  176. Tab:       mov     cl,dl
  177.            sub     cl,WindLo.Col        ;Characters beyond left edge
  178.            add     cl,8
  179.            and     cl,0F8h              ;To next tab stop
  180.            add     cl,WindLo.Col        ;Window coords
  181.            sub     cl,dl                ;Spaces to write
  182.            xor     ch,ch                ;CX = spaces to write
  183. Tab1:      mov     al,20h               ;Write spaces
  184.            push    cx
  185.            call    WriteOne             ;One at a time
  186.            xor     bh,bh
  187.            mov     ah,2
  188.            int     10h
  189.            mov     WindPos,dx           ;Save new cursor position
  190.            pop     cx
  191.            loop    Tab1                 ;Do all of them
  192.            jmp     BiosWriteDone
  193.  
  194. BackSpace: cmp     dl,WindLo.col        ;Beyond left edge?
  195.            jbe     BiosWriteDone        ;Exit if not
  196.            dec     dl                   ;One left
  197.            xor     bh,bh
  198.            mov     ah,2                 ;Position cursor
  199.            int     10h
  200.            mov     WindPos,dx
  201.            mov     cx,1                 ;Write character
  202.            mov     bl,WindAttr
  203.            mov     ax,0920h             ;Write a space
  204.            int     10h
  205.            jmp     BiosWriteDone        ;Done now
  206.  
  207. WriteChar  endp
  208.  
  209. ;---------------
  210. ; Write one character and update cursor variable
  211. WriteOne   proc    near
  212.            mov     cx,1                 ;Write character
  213.            mov     bl,WindAttr
  214.            xor     bh,bh
  215.            mov     ah,9
  216.            int     10h
  217.  
  218.            cmp     dl,WindHi.col        ;Below right border?
  219.            jb      IncCol               ;If so, just increment column
  220.            cmp     dh,WindHi.row        ;Room for CR/LF?
  221.            jb      IncRow               ;Jump if so
  222.  
  223.            mov     ax,0601h             ;Scroll up one line
  224.            mov     cx,WindLo
  225.            mov     dx,WindHi
  226.            mov     bh,WindAttr
  227.            int     10h
  228.            dec     dh                   ;Compensate for inc to follow
  229.  
  230. IncRow:    inc     dh                   ;Next row
  231.            mov     dl,WindLo.col        ;First col
  232.            dec     dl                   ;Compensate for inc to follow
  233.  
  234. IncCol:    inc     dl                   ;Increment column
  235.            ret
  236. WriteOne   endp
  237.  
  238. code       ends
  239.            end
  240.