home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / tasm / video2.inc < prev    next >
Text File  |  1988-08-28  |  17KB  |  536 lines

  1. ;*********************************************************
  2. ;* Show87 - (C) Copyright 1988 by Borland International  *
  3. ;* VIDEO2.INC - Include module for Show87                *
  4. ;*********************************************************
  5. ;
  6. ;=============================================================================
  7. ; High Level Video Routines
  8. ;
  9. ; These are routines to display formatted text strings.  All registers are
  10. ; preserved except those used to return parameters.  All parameters are passed
  11. ; through the registers.  VIDEO1.INC must be included in the source code.
  12. ; VIDEO_INIT must be called before any of these routines. It is assumed that
  13. ; DS = ES = CS.
  14. ;
  15. ;===========================================================================
  16. ; Global data.
  17.  
  18. ;--- display attributes
  19.  
  20. Video_Norm      db      07h             ;normal
  21. Video_Emph      db      02h             ;emphasized (underline)
  22. Video_High      db      70h             ;highlight (reverse video)
  23. Video_Bold      db      0fh             ;bold (intense)
  24.  
  25. ;--- attribute stack
  26.  
  27. Video_AtrM      equ     10              ;maximum stack entries (n > 0)
  28. Video_AtrN      dw      0               ;present stack number
  29. Video_AtrS      db      07h             ;start of stack
  30.   DD            Video_AtrM, ?           ;rest of stack
  31.  
  32. ;--- string formatting controls
  33.  
  34. FrmEnd          equ     0               ;end of string
  35. FrmCtl          equ     1               ;control character mask
  36. FrmNorm         equ     2               ;switch to normal attribute
  37. FrmEmph         equ     3               ;switch to emphasized
  38. FrmHigh         equ     4               ;switch to highlight
  39. FrmBold         equ     5               ;switch to bold
  40. FrmAtr          equ     6               ;user defined attribute
  41. FrmPush         equ     7               ;push the present attribute
  42. FrmPop          equ     8               ;pop an attribute
  43. FrmHor          equ     9               ;horizontally repeated character
  44.                                         ;  (left to right)
  45. FrmVer          equ     10              ;vertically repeated character
  46.                                         ;   (up to down)
  47. FrmStr          equ     11              ;nested string
  48. FrmLoc          equ     12              ;locate cursor
  49.  
  50. Video_StrM      equ     10              ;maximum nested strings
  51.  
  52. ;================================================
  53. ; Write a formatted string. Note: uses (32 =
  54. ; Video_AtrM * 2 + 14) stack bytes.
  55. ;
  56. ; In: si= string location.
  57. ;=================================================
  58. ;
  59. Video_Wstr proc near
  60.         pushf
  61.         push    ax
  62.         push    bx
  63.         push    cx
  64.         push    dx
  65.         push    di
  66.         push    si
  67.         push    bp
  68.         sub     sp, Video_AtrM * 2 + 2          ;room for local data
  69.         mov     bp, sp
  70.  
  71.         cld
  72.         mov     word ptr [bp], 0                ;clear nested string stack
  73.         mov     di, Video_AtrN                  ;get the present stack number
  74.         mov     bl, [Video_AtrS + di]           ;get attribute
  75.  
  76. ;--- loop for each byte in the string
  77.  
  78. Vidwst1 :
  79.         lodsb                                   ;get the next byte
  80.         cmp     al, FrmEnd                      ;check if end of string
  81.         je      Vidwst5
  82.         cmp     al, 32                          ;check if control character
  83.         jb      Vidwst2
  84.         call    Video_Wchr                      ;write character
  85.         jmp     Vidwst1
  86.  
  87. ;--- control character
  88.  
  89. Vidwst2 :
  90.         mov     di, offset Video_Ftab           ;location of table
  91.  
  92. Vidwst3 :
  93.         cmp     byte ptr [di], -1               ;check if end of table
  94.         je      Vidwst1                         ;jump if so, ignore this
  95.                                                 ;  character
  96.         cmp     al, [di]                        ;check if match command
  97.         je      Vidwst4                         ;jump if so, call command
  98.                                                 ;  routine
  99.         add     di, 3                           ;next location
  100.         jmp     Vidwst3                         ;loop back until end of table
  101.                                                 ;   or match
  102.  
  103. Vidwst4 :
  104.         call    word ptr [di + 1]               ;call the routine
  105.         jmp     Vidwst1
  106.  
  107. ;--- check if within nested string
  108.  
  109. Vidwst5  :
  110.         mov     ax, [bp]                        ;get the number
  111.         or      ax, ax                          ;check if any entries
  112.         jz      Vidwst6
  113.  
  114.         dec     ax                              ;reduce count
  115.         mov     [bp], ax                        ;save it
  116.         mov     di, ax
  117.         shl     di, 1                           ;times two
  118.         mov     si, [bp + 2 + di]               ;get the saved offset
  119.         jmp     Vidwst1                         ;continue
  120.  
  121. ;--- finished, save the attribute in the attribute stack
  122.  
  123. Vidwst6   :
  124.         mov     di, Video_AtrN                  ;get the present stack number
  125.         mov     [Video_AtrS + di], bl           ;save attribute
  126.  
  127.         add     sp, Video_AtrM * 2 + 2          ;fix stack
  128.         pop     bp
  129.         pop     si
  130.         pop     di
  131.         pop     dx
  132.         pop     cx
  133.         pop     bx
  134.         pop     ax
  135.         popf
  136.         ret
  137.  endp           ;Video_Wfstr
  138.  
  139. ;------------------------------------------------
  140. ; This is the table to access the formatting
  141. ; routines below. The table consists of a format
  142. ; command byte followed by the 16 bit address of
  143. ; the routine to handle the command.  The table
  144. ; is terminated with a -1 (FFH).
  145. ;-------------------------------------------------
  146. Video_Ftab label Byte
  147.  db FrmCtl
  148.  dw offset Vidwstr1
  149.  db FrmNorm
  150.  dw offset Vidwstr2
  151.  db FrmEmph
  152.  dw offset Vidwstr3
  153.  db FrmHigh
  154.  dw offset Vidwstr4
  155.  db FrmBold
  156.  dw offset Vidwstr5
  157.  db FrmAtr
  158.  dw offset Vidwstr6
  159.  db FrmPush
  160.  dw offset Vidwstr7
  161.  db FrmPop
  162.  dw offset Vidwstr8
  163.  db FrmHor
  164.  dw offset Vidwstr9
  165.  db FrmStr
  166.  dw offset Vidwstr10
  167.  db FrmLoc
  168.  dw offset Vidwstr11
  169.  db FrmVer
  170.  dw offset Vidwstr12
  171.  db -1
  172.  
  173. ;------------------------------------------------
  174. ; These are the individual routines to handle
  175. ; the special formatting commands.
  176.  
  177. ;--- control character mask
  178.  
  179. Vidwstr1 proc near
  180.         lodsb                                   ;get the character
  181.         call    Video_Wchr                      ;display
  182.         ret
  183.  endp
  184.  
  185. ;--- normal attribute
  186.  
  187. Vidwstr2 proc near
  188.         mov     bl, Video_Norm                  ;set to normal attribute
  189.         ret
  190.  endp
  191.  
  192. ;--- emphasized attribute
  193.  
  194. Vidwstr3 proc near
  195.         mov     bl, Video_Emph                  ;set to normal attribute
  196.         ret
  197.  endp
  198.  
  199. ;--- highlighted attribute
  200.  
  201. Vidwstr4 proc near
  202.         mov     bl, Video_High                  ;set to normal attribute
  203.         ret
  204.  endp
  205.  
  206. ;--- bold attribute
  207.  
  208. Vidwstr5 proc near
  209.         mov     bl, Video_Bold                  ;set to normal attribute
  210.         ret
  211.  endp
  212.  
  213. ;--- user defined attribute
  214.  
  215. Vidwstr6 proc near
  216.         lodsb                                   ;get the attribute
  217.         mov     bl, al                          ;set attribute
  218.         ret
  219.  endp
  220.  
  221. ;--- push the present attribute
  222.  
  223. Vidwstr7 proc near
  224.         mov     ax, Video_AtrN                  ;get the present stack number
  225.         cmp     ax, Video_AtrM - 1              ;check if stack full
  226.         jae     Vidwstr71
  227.  
  228.         mov     di, ax                          ;load index
  229.         mov     [Video_AtrS + di], bl           ;save the present attribute
  230.  
  231.         inc     ax                              ;increment stack
  232.         mov     Video_AtrN, ax                  ;save it
  233.  
  234. Vidwstr71 :
  235.         ret
  236.  endp
  237.  
  238. ;--- pop an attribute
  239.  
  240. Vidwstr8 proc near
  241.         mov     ax, Video_AtrN                  ;get the present stack number
  242.         or      ax, ax                          ;check if at bottom
  243.         jz      Vidwstr81
  244.  
  245.         dec     ax                              ;reduce stack number
  246.         mov     di, ax                          ;load index
  247.         mov     bl, [Video_AtrS + di]           ;set the present attribute
  248.  
  249.         mov     Video_AtrN, ax                  ;save stack number
  250. Vidwstr81 :
  251.         ret
  252.  endp
  253.  
  254. ;--- repeat display a character
  255.  
  256. Vidwstr9 proc near
  257.         lodsb                                   ;get the character
  258.         push    ax
  259.         lodsb                                   ;load the count
  260.         mov     cl, al
  261.         sub     ch, ch                          ;count in cx
  262.         pop     ax
  263.         call    Video_Wchrs                     ;display characters
  264.         ret
  265.  endp
  266.  
  267. ;--- nested string
  268.  
  269. Vidwstr10 proc near
  270.         lodsw                                   ;get the offset
  271.         cmp     word ptr [bp], Video_StrM       ;check if stack full
  272.         je      Vidwstr101
  273.  
  274.         mov     di, [bp]                        ;stack entries
  275.         shl     di, 1                           ;times two
  276.         mov     [bp + 2 + di], si               ;save present location
  277.         inc     word ptr [bp]                   ;increment entries
  278.         mov     si, ax                          ;new string location
  279. Vidwstr101 :
  280.         ret
  281.  endp
  282.  
  283. ;--- cursor move
  284.  
  285. Vidwstr11 proc near
  286.         lodsw                                   ;load location
  287.         xchg    ah, al                          ;get row and column in right
  288.                                                 ;  register halves
  289.         mov     dx, ax
  290.         call    Video_Cset                      ;set cursor location
  291.         ret
  292.  endp
  293.  
  294. ;--- vertically repeated character
  295.  
  296. Vidwstr12 proc near
  297.         lodsb                                   ;get the character
  298.         push    ax
  299.         lodsb                                   ;load the count
  300.         mov     cl, al
  301.         sub     ch, ch                          ;count in cx
  302.         pop     ax
  303.         call    Video_Cget                      ;get the cursor location
  304.  
  305. Vidwstr121 :
  306.         push    dx
  307.         call    Video_Wchr                      ;display character
  308.         pop     dx
  309.         inc     dh                              ;next row
  310.         call    Video_Cset                      ;set the cursor location
  311.         loop    Vidwstr121
  312.         ret
  313.  endp
  314.  
  315. ;================================================
  316. ; Calculate the horizontal length of the text in
  317. ; a formatted string. Note: uses (32 =
  318. ; Video_AtrM * 2 + 10) stack bytes.  Used for
  319. ; justifying strings.
  320. ;
  321. ; In: si= string location.
  322. ;
  323. ; Out: bx= length.
  324. ;==================================================
  325. ;
  326. Video_Scnt proc near
  327.         push    ax
  328.         push    di
  329.         push    si
  330.         push    bp
  331.         sub     sp, Video_AtrM * 2 + 2          ;room for local data
  332.         mov     bp, sp
  333.  
  334.         sub     bx, bx                          ;clear the count
  335.         mov     word ptr [bp], 0                ;clear nested string stack
  336.  
  337. ;--- loop for each byte in the string
  338.  
  339. Vidscn1     :
  340.         lodsb                                   ;get the next byte
  341.         cmp     al, FrmEnd                      ;check if end of string
  342.         je      Vidscn8
  343.         cmp     al, 32                          ;check if control character
  344.         jb      Vidscn2
  345.         inc     bx                              ;increment count
  346.         jmp     Vidscn1
  347.  
  348. ;--- control character
  349.  
  350. Vidscn2 :
  351.         cmp     al, FrmCtl
  352.         jne     Vidscn3
  353.         inc     si                              ;skip character
  354.         inc     bx                              ;add to total
  355.         jmp     Vidscn1
  356.  
  357. ;--- user attribute
  358.  
  359. Vidscn3 :
  360.         cmp     al, FrmAtr
  361.         jne     Vidscn4
  362.         inc     si                              ;skip attribute byte
  363.         jmp     Vidscn1
  364.  
  365. ;--- horizontal repeated byte
  366.  
  367. Vidscn4 :
  368.         cmp     al, FrmHor
  369.         jne     Vidscn5
  370.         inc     si                              ;skip character
  371.         lodsb                                   ;load length
  372.         sub     ah, ah
  373.         add     bx, ax                          ;add to total
  374.         jmp     Vidscn1
  375.  
  376. ;--- nested string
  377.  
  378. Vidscn5 :
  379.         cmp     al, FrmStr
  380.         jne     Vidscn6
  381.         call    Vidwstr10                       ;start nested string
  382.         jmp     Vidscn1
  383.  
  384. ;--- cursor move
  385.  
  386. Vidscn6 :
  387.         cmp     al, FrmLoc
  388.         jne     Vidscn7
  389.         inc     si
  390.         inc     si                              ;skip location
  391.         jmp     Vidscn1
  392.  
  393. ;--- vertical repeated byte
  394.  
  395. Vidscn7 :
  396.         cmp     al, FrmVer
  397.         jne     Vidscn1
  398.         inc     si
  399.         inc     si                              ;skip byte and count
  400.         inc     bx                              ;add one for the first byte
  401.         jmp     Vidscn1
  402.  
  403. ;--- check if within nested string
  404.  
  405. Vidscn8 :
  406.         mov     ax, [bp]                        ;get the number
  407.         or      ax, ax                          ;check if any entries
  408.         jz      Vidscn9
  409.  
  410.         dec     ax                              ;reduce count
  411.         mov     [bp], ax                        ;save it
  412.         mov     di, ax
  413.         shl     di, 1                           ;times two
  414.         mov     si, [bp + 2 + di]               ;get the saved offset
  415.         jmp     Vidscn1                         ;continue
  416.  
  417. ;--- finished
  418.  
  419. Vidscn9 :
  420.         add     sp, Video_AtrM * 2 + 2          ;fix stack
  421.         pop     bp
  422.         pop     si
  423.         pop     di
  424.         pop     ax
  425.         ret
  426.  endp           ;Video_Scnt
  427.  
  428. ;================================================
  429. ; Write a string padded with some character on
  430. ; both sides.
  431. ;
  432. ; In: si= string location; al= pad character;
  433. ; bx= left side pad number; cx= right side pad
  434. ; number.
  435. ;==================================================
  436. ;
  437. Video_Wstrp proc near
  438.         push    bx
  439.         push    cx
  440.         push    di
  441.         mov     di, Video_AtrN                  ;get the present stack number
  442.         push    cx
  443.         mov     cx, bx
  444.         mov     bl, [Video_AtrS + di]           ;get attribute
  445.         call    Video_Wchrs                     ;write preceding spaces
  446.         call    Video_Wstr                      ;write string
  447.         pop     cx
  448.         mov     bl, [Video_AtrS + di]           ;get attribute
  449.         call    Video_Wchrs                     ;write following spaces
  450.         pop     di
  451.         pop     cx
  452.         pop     bx
  453.         ret
  454.  endp           ;Video_Wstrp
  455.  
  456. ;================================================
  457. ; Write a left justified string.
  458. ;
  459. ; In: si= string location; al= pad character;
  460. ; cx= total column width.
  461. ;===============================================
  462. ;
  463. Video_Wstrl proc near
  464.         pushf
  465.         cld
  466.         push    bx
  467.         push    cx
  468.         call    Video_Scnt                      ;get string width
  469.         cmp     bx, cx                          ;check if too big
  470.         jbe     Vidwsl1
  471.         mov     bx, cx                          ;set to maximum length
  472. Vidwsl1 :
  473.         sub     cx, bx                          ;get difference
  474.         sub     bx, bx                          ;no padding in front
  475.         call    Video_Wstrp                     ;print with padding
  476.         pop     cx
  477.         pop     bx
  478.         popf
  479.         ret
  480.  endp           ;Video_Wstrl
  481.  
  482. ;================================================
  483. ; Write a right justified string.
  484. ;
  485. ; In: si= string location; al= pad character;
  486. ; cx= total column width.
  487. ;==================================================
  488. ;
  489. Video_Wstrr proc near
  490.         pushf
  491.         cld
  492.         push    bx
  493.         push    cx
  494.         call    Video_Scnt                      ;get string width
  495.         cmp     bx, cx                          ;check if too big
  496.         jbe     Vidwsr1
  497.         mov     bx, cx                          ;set to maximum length
  498. Vidwsr1  :
  499.         sub     cx, bx                          ;get difference
  500.         mov     bx, cx                          ;padding is before
  501.         sub     cx, cx                          ;no padding after
  502.         call    Video_Wstrp                     ;print with padding
  503.         pop     cx
  504.         pop     bx
  505.         popf
  506.         ret
  507.  endp           ;Video_Wstrr
  508.  
  509. ;================================================
  510. ; Write a center justified string.
  511. ;
  512. ; In: si= string location; al= pad character;
  513. ; cx= total column width.
  514. ;=================================================
  515. ;
  516. Video_Wstrc proc near
  517.         pushf
  518.         cld
  519.         push    bx
  520.         push    cx
  521.         call    Video_Scnt                      ;get string width
  522.         cmp     bx, cx                          ;check if too big
  523.         jbe     Vidwsc1
  524.         mov     bx, cx                          ;set to maximum length
  525. Vidwsc1   :
  526.         sub     cx, bx                          ;get difference
  527.         mov     bx, cx
  528.         shr     bx, 1                           ;divide by two, preceding pad
  529.         sub     cx, bx                          ;difference is end pad
  530.         call    Video_Wstrp                     ;print with padding
  531.         pop     cx
  532.         pop     bx
  533.         popf
  534.         ret
  535.  endp           ;Video_Wstrc
  536.