home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter25 / l25-1.asm next >
Assembly Source File  |  1997-06-18  |  7KB  |  234 lines

  1. ; Listing 3.1.
  2. ; Program to illustrate operation of data rotate and bit mask
  3. ;  features of Graphics Controller.  Draws 8x8 character at
  4. ;  specified location, using VGA's 8x8 ROM font.  Designed
  5. ;  for use with modes 0Dh, 0Eh, 0Fh, 10h, and 12h.
  6. ; Assembled with TASM 4.0, linked with TLINK 6.10
  7. ; Checked by Jim Mischel 11/21/94
  8. ;
  9. stack   segment para stack 'STACK'
  10.         db      512 dup(?)
  11. stack   ends
  12. ;
  13. VGA_VIDEO_SEGMENT       equ     0a000h  ;VGA display memory segment
  14. SCREEN_WIDTH_IN_BYTES   equ     044ah   ;offset of BIOS variable
  15. FONT_CHARACTER_SIZE     equ     8       ;# bytes in each font char
  16. ;
  17. ; VGA register equates.
  18. ;
  19. GC_INDEX        equ     3ceh    ;GC index register
  20. GC_ROTATE       equ     3       ;GC data rotate/logical function
  21.                                 ; register index
  22. GC_BIT_MASK     equ     8       ;GC bit mask register index
  23. ;
  24. dseg    segment para common 'DATA'
  25. TEST_TEXT_ROW   equ     69      ;row to display test text at
  26. TEST_TEXT_COL   equ     17      ;column to display test text at
  27. TEST_TEXT_WIDTH equ     8       ;width of a character in pixels
  28.  
  29. TestString      label   byte
  30.         db      'Hello, world!',0       ;test string to print.
  31. FontPointer     dd      ?               ;font offset
  32. dseg    ends
  33. ;
  34. ; Macro to set indexed register INDEX of GC chip to SETTING.
  35. ;
  36. SETGC   macro   INDEX, SETTING
  37.         mov     dx,GC_INDEX
  38.         mov     ax,(SETTING SHL 8) OR INDEX
  39.         out     dx,ax
  40.         endm
  41. ;
  42. cseg    segment para public 'CODE'
  43.         assume  cs:cseg, ds:dseg
  44. start   proc    near
  45.         mov     ax,dseg
  46.         mov     ds,ax
  47. ;
  48. ; Select 640x480 graphics mode.
  49. ;
  50.         mov     ax,012h
  51.         int     10h
  52. ;
  53. ; Set driver to use the 8x8 font.
  54. ;
  55.         mov     ah,11h          ;VGA BIOS character generator function,
  56.         mov     al,30h          ; return info subfunction
  57.         mov     bh,3            ;get 8x8 font pointer
  58.         int     10h
  59.         call    SelectFont
  60. ;
  61. ; Print the test string.
  62. ;
  63.         mov     si,offset TestString
  64.         mov     bx,TEST_TEXT_ROW
  65.         mov     cx,TEST_TEXT_COL
  66. StringOutLoop:
  67.         lodsb
  68.         and     al,al
  69.         jz      StringOutDone
  70.         call    DrawChar
  71.         add     cx,TEST_TEXT_WIDTH
  72.         jmp     StringOutLoop
  73. StringOutDone:
  74. ;
  75. ; Reset the data rotate and bit mask registers.
  76. ;
  77.         SETGC   GC_ROTATE, 0
  78.         SETGC   GC_BIT_MASK, 0ffh
  79. ;
  80. ; Wait for a keystroke.
  81. ;
  82.         mov     ah,1
  83.         int     21h
  84. ;
  85. ; Return to text mode.
  86. ;
  87.         mov     ax,03h
  88.         int     10h
  89. ;
  90. ; Exit to DOS.
  91. ;
  92.         mov     ah,4ch
  93.         int     21h
  94. Start   endp
  95. ;
  96. ; Subroutine to draw a text character in a linear graphics mode
  97. ;  (0Dh, 0Eh, 0Fh, 010h, 012h).
  98. ; Font used should be pointed to by FontPointer.
  99. ;
  100. ; Input:
  101. ;  AL = character to draw
  102. ;  BX = row to draw text character at
  103. ;  CX = column to draw text character at
  104. ;
  105. ;  Forces ALU function to "move".
  106. ;
  107. DrawChar        proc    near
  108.         push    ax
  109.         push    bx
  110.         push    cx
  111.         push    dx
  112.         push    si
  113.         push    di
  114.         push    bp
  115.         push    ds
  116. ;
  117. ; Set DS:SI to point to font and ES to point to display memory.
  118. ;
  119.         lds     si,[FontPointer]        ;point to font
  120.         mov     dx,VGA_VIDEO_SEGMENT
  121.         mov     es,dx                   ;point to display memory
  122. ;
  123. ; Calculate screen address of byte character starts in.
  124. ;
  125.         push    ds              ;point to BIOS data segment
  126.         sub     dx,dx
  127.         mov     ds,dx
  128.         xchg    ax,bx
  129.         mov     di,ds:[SCREEN_WIDTH_IN_BYTES]   ;retrieve BIOS
  130.                                                 ; screen width
  131.         pop     ds
  132.         mul     di              ;calculate offset of start of row
  133.         push    di              ;set aside screen width
  134.         mov     di,cx           ;set aside the column
  135.         and     cl,0111b     ;keep only the column in-byte address
  136.         shr     di,1
  137.         shr     di,1
  138.         shr     di,1            ;divide column by 8 to make a byte address
  139.         add     di,ax           ;and point to byte
  140. ;
  141. ; Calculate font address of character.
  142. ;
  143.         sub     bh,bh
  144.         shl     bx,1            ;assumes 8 bytes per character; use
  145.         shl     bx,1            ; a multiply otherwise
  146.         shl     bx,1            ;offset in font of character
  147.         add     si,bx           ;offset in font segment of character
  148. ;
  149. ; Set up the GC rotation.
  150. ;
  151.         mov     dx,GC_INDEX
  152.         mov     al,GC_ROTATE
  153.         mov     ah,cl
  154.         out     dx,ax
  155. ;
  156. ; Set up BH as bit mask for left half,
  157. ; BL as rotation for right half.
  158. ;
  159.         mov     bx,0ffffh
  160.         shr     bh,cl
  161.         neg     cl
  162.         add     cl,8
  163.         shl     bl,cl
  164. ;
  165. ; Draw the character, left half first, then right half in the
  166. ; succeeding byte, using the data rotation to position the character
  167. ; across the byte boundary and then using the bit mask to get the
  168. ; proper portion of the character into each byte.
  169. ; Does not check for case where character is byte-aligned and
  170. ; no rotation and only one write is required.
  171. ;
  172.         mov     bp,FONT_CHARACTER_SIZE
  173.         mov     dx,GC_INDEX
  174.         pop     cx              ;get back screen width
  175.         dec     cx
  176.         dec     cx              ; -2 because do two bytes for each char
  177. CharacterLoop:
  178. ;
  179. ; Set the bit mask for the left half of the character.
  180. ;
  181.         mov     al,GC_BIT_MASK
  182.         mov     ah,bh
  183.         out     dx,ax
  184. ;
  185. ; Get the next character byte & write it to display memory.
  186. ; (Left half of character.)
  187. ;
  188.         mov     al,[si]         ;get character byte
  189.         mov     ah,es:[di]      ;load latches
  190.         stosb                   ;write character byte
  191. ;
  192. ; Set the bit mask for the right half of the character.
  193. ;
  194.         mov     al,GC_BIT_MASK
  195.         mov     ah,bl
  196.         out     dx,ax
  197. ;
  198. ; Get the character byte again & write it to display memory.
  199. ; (Right half of character.)
  200. ;
  201.         lodsb                   ;get character byte
  202.         mov     ah,es:[di]      ;load latches
  203.         stosb                   ;write character byte
  204. ;
  205. ; Point to next line of character in display memory.
  206. ;
  207.         add     di,cx
  208. ;
  209.         dec     bp
  210.         jnz     CharacterLoop
  211. ;
  212.         pop     ds
  213.         pop     bp
  214.         pop     di
  215.         pop     si
  216.         pop     dx
  217.         pop     cx
  218.         pop     bx
  219.         pop     ax
  220.         ret
  221. DrawChar        endp
  222. ;
  223. ; Set the pointer to the font to draw from to ES:BP.
  224. ;
  225. SelectFont      proc    near
  226.         mov     word ptr [FontPointer],bp       ;save pointer
  227.         mov     word ptr [FontPointer+2],es
  228.         ret
  229. SelectFont      endp
  230. ;  
  231. cseg    ends
  232.         end     start
  233.  
  234.