home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / asm / PJGRAPH.ZIP / CHAP06.2 < prev    next >
Encoding:
Text File  |  1989-09-26  |  8.5 KB  |  327 lines

  1. ; Program to illustrate one use of write mode 2 of the VGA and EGA by
  2. ; drawing lines in color patterns.
  3. ;
  4. ; Assembled with MASM 5.0, linked with MS-LINK 3.60.
  5. ;
  6. ; By Michael Abrash 11/8/87
  7. ; Updated 6/27/89.
  8. ;
  9. stackseg        segment para stack 'STACK'
  10.         db      512 dup(0)
  11. stackseg        ends
  12.  
  13. SCREEN_WIDTH_IN_BYTES   equ     80
  14. GRAPHICS_SEGMENT equ    0a000h  ;mode 10 bit-map segment
  15. SC_INDEX        equ     3c4h    ;Sequence Controller Index register
  16. MAP_MASK        equ     2       ;index of Map Mask register
  17. GC_INDEX        equ     03ceh   ;Graphics Controller Index reg
  18. GRAPHICS_MODE   equ     5       ;index of Graphics Mode reg
  19. BIT_MASK        equ     8       ;index of Bit Mask reg
  20.  
  21. Data    segment para common 'DATA'
  22. Pattern0        db      16
  23.                 db      0, 1, 2, 3, 4, 5, 6, 7, 8
  24.                 db      9, 10, 11, 12, 13, 14, 15
  25. Pattern1        db      6
  26.                 db      2, 2, 2, 10, 10, 10
  27. Pattern2        db      8
  28.                 db      15, 15, 15, 0, 0, 15, 0, 0
  29. Pattern3        db      9
  30.                 db      1, 1, 1, 2, 2, 2, 4, 4, 4
  31. Data    ends
  32.  
  33. Code    segment para public 'CODE'
  34.         assume  cs:Code, ds:Data
  35. Start   proc    near
  36.         mov     ax,Data
  37.         mov     ds,ax
  38.         mov     ax,10h
  39.         int     10h             ;select video mode 10h (640x350)
  40. ;
  41. ; Draw 8 radial lines in upper left quadrant in pattern 0.
  42. ;
  43.         mov     bx,0
  44.         mov     cx,0
  45.         mov     si,offset Pattern0
  46.         call    QuadrantUp
  47. ;
  48. ; Draw 8 radial lines in upper right quadrant in pattern 1.
  49. ;
  50.         mov     bx,320
  51.         mov     cx,0
  52.         mov     si,offset Pattern1
  53.         call    QuadrantUp
  54. ;
  55. ; Draw 8 radial lines in lower left quadrant in pattern 2.
  56. ;
  57.         mov     bx,0
  58.         mov     cx,175
  59.         mov     si,offset Pattern2
  60.         call    QuadrantUp
  61. ;
  62. ; Draw 8 radial lines in lower right quadrant in pattern 3.
  63. ;
  64.         mov     bx,320
  65.         mov     cx,175
  66.         mov     si,offset Pattern3
  67.         call    QuadrantUp
  68. ;
  69. ; Wait for a key before returning to text mode and ending.
  70. ;
  71.         mov     ah,01h
  72.         int     21h
  73.         mov     ax,03h
  74.         int     10h
  75.         mov     ah,4ch
  76.         int     21h
  77. ;
  78. ; Draws 8 radial lines with specified pattern in specified mode 10h
  79. ; quadrant.
  80. ;
  81. ; Input:
  82. ;       BX = X coordinate of upper left corner of quadrant
  83. ;       CX = Y coordinate of upper left corner of quadrant
  84. ;       SI = pointer to pattern, in following form:
  85. ;               Byte 0: Length of pattern
  86. ;               Byte 1: Start of pattern, one color per byte
  87. ;
  88. ; AX, BX, CX, DX destroyed
  89. ;
  90. QuadrantUp      proc    near
  91.         add     bx,160
  92.         add     cx,87           ;point to the center of the quadrant
  93.         mov     ax,0
  94.         mov     dx,160
  95.         call    LineUp          ;draw horizontal line to right edge
  96.         mov     ax,1
  97.         mov     dx,88
  98.         call    LineUp          ;draw diagonal line to upper right
  99.         mov     ax,2
  100.         mov     dx,88
  101.         call    LineUp          ;draw vertical line to top edge
  102.         mov     ax,3
  103.         mov     dx,88
  104.         call    LineUp          ;draw diagonal line to upper left
  105.         mov     ax,4
  106.         mov     dx,161
  107.         call    LineUp          ;draw horizontal line to left edge
  108.         mov     ax,5
  109.         mov     dx,88
  110.         call    LineUp          ;draw diagonal line to lower left
  111.         mov     ax,6
  112.         mov     dx,88
  113.         call    LineUp          ;draw vertical line to bottom edge
  114.         mov     ax,7
  115.         mov     dx,88
  116.         call    LineUp          ;draw diagonal line to bottom right
  117.         ret
  118. QuadrantUp      endp
  119. ;
  120. ; Draws a horizontal, vertical, or diagonal line (one of the eight
  121. ; possible radial lines) of the specified length from the specified
  122. ; starting point.
  123. ;
  124. ; Input:
  125. ;       AX = line direction, as follows:
  126. ;               3  2  1
  127. ;               4  *  0
  128. ;               5  6  7
  129. ;       BX = X coordinate of starting point
  130. ;       CX = Y coordinate of starting point
  131. ;       DX = length of line (number of pixels drawn)
  132. ;
  133. ; All registers preserved.
  134. ;
  135. ; Table of vectors to routines for each of the 8 possible lines.
  136. ;
  137. LineUpVectors   label   word
  138.         dw      LineUp0, LineUp1, LineUp2, LineUp3
  139.         dw      LineUp4, LineUp5, LineUp6, LineUp7
  140.  
  141. ;
  142. ; Macro to draw horizontal, vertical, or diagonal line.
  143. ;
  144. ; Input:
  145. ;       XParm = 1 to draw right, -1 to draw left, 0 to not move horz.
  146. ;       YParm = 1 to draw down, -1 to draw up, 0 to not move vert.
  147. ;       BX = X start location
  148. ;       CX = Y start location
  149. ;       DX = number of pixels to draw
  150. ;       DS:SI = line pattern
  151. ;
  152. MLineUp macro   XParm, YParm
  153.         local   LineUpLoop, CheckMoreLine
  154.         mov     di,si           ;set aside start offset of pattern
  155.         lodsb                   ;get length of pattern
  156.         mov     ah,al
  157.  
  158. LineUpLoop:
  159.         lodsb                   ;get color of this pixel...
  160.         call    DotUpInColor    ;...and draw it
  161. if XParm EQ 1
  162.         inc     bx
  163. endif
  164. if XParm EQ -1
  165.         dec     bx
  166. endif
  167. if YParm EQ 1
  168.         inc     cx
  169. endif
  170. if YParm EQ -1
  171.         dec     cx
  172. endif
  173.         dec     ah              ;at end of pattern?
  174.         jnz     CheckMoreLine
  175.         mov     si,di           ;get back start of pattern
  176.         lodsb
  177.         mov     ah,al           ;reset pattern count
  178.  
  179. CheckMoreLine:
  180.         dec     dx
  181.         jnz     LineUpLoop
  182.         jmp     LineUpEnd
  183.         endm
  184.  
  185. LineUp  proc    near
  186.         push    ax
  187.         push    bx
  188.         push    cx
  189.         push    dx
  190.         push    si
  191.         push    di
  192.         push    es
  193.  
  194.         mov     di,ax
  195.  
  196.         mov     ax,GRAPHICS_SEGMENT
  197.         mov     es,ax
  198.  
  199.         push    dx              ;save line length
  200. ;
  201. ; Enable writes to all planes.
  202. ;
  203.         mov     dx,SC_INDEX
  204.         mov     al,MAP_MASK
  205.         out     dx,al
  206.         inc     dx
  207.         mov     al,0fh
  208.         out     dx,al
  209. ;
  210. ; Select write mode 2.
  211. ;
  212.         mov     dx,GC_INDEX
  213.         mov     al,GRAPHICS_MODE
  214.         out     dx,al
  215.         inc     dx
  216.         mov     al,02h
  217.         out     dx,al
  218. ;
  219. ; Vector to proper routine.
  220. ;
  221.         pop     dx              ;get back line length
  222.  
  223.         shl     di,1
  224.         jmp     cs:[LineUpVectors+di]
  225. ;
  226. ; Horizontal line to right.
  227. ;
  228. LineUp0:
  229.         MLineUp 1, 0
  230. ;
  231. ; Diagonal line to upper right.
  232. ;
  233. LineUp1:
  234.         MLineUp 1, -1
  235. ;
  236. ; Vertical line to top.
  237. ;
  238. LineUp2:
  239.         MLineUp 0, -1
  240. ;
  241. ; Diagonal line to upper left.
  242. ;
  243. LineUp3:
  244.         MLineUp -1, -1
  245. ;
  246. ; Horizontal line to left.
  247. ;
  248. LineUp4:
  249.         MLineUp -1, 0
  250. ;
  251. ; Diagonal line to bottom left.
  252. ;
  253. LineUp5:
  254.         MLineUp -1, 1
  255. ;
  256. ; Vertical line to bottom.
  257. ;
  258. LineUp6:
  259.         MLineUp 0, 1
  260. ;
  261. ; Diagonal line to bottom right.
  262. ;
  263. LineUp7:
  264.         MLineUp 1, 1
  265.  
  266. LineUpEnd:
  267.         pop     es
  268.         pop     di
  269.         pop     si
  270.         pop     dx
  271.         pop     cx
  272.         pop     bx
  273.         pop     ax
  274.         ret
  275. LineUp  endp
  276. ;
  277. ; Draws a dot in the specified color at the specified location.
  278. ; Assumes that the VGA is in write mode 2 with writes to all planes
  279. ; enabled and that ES points to display memory.
  280. ;
  281. ; Input:
  282. ;       AL = dot color
  283. ;       BX = X coordinate of dot
  284. ;       CX = Y coordinate of dot
  285. ;       ES = display memory segment
  286. ;
  287. ; All registers preserved.
  288. ;
  289. DotUpInColor    proc    near
  290.         push    bx
  291.         push    cx
  292.         push    dx
  293.         push    di
  294. ;
  295. ; Point ES:DI to the display memory byte in which the pixel goes, with
  296. ; the bit mask set up to access that pixel within the addressed byte.
  297. ;
  298.         push    ax              ;preserve dot color
  299.         mov     ax,SCREEN_WIDTH_IN_BYTES
  300.         mul     cx              ;offset of start of row pixel is in
  301.         mov     di,ax
  302.         mov     cl,bl
  303.         and     cl,111b
  304.         mov     dx,GC_INDEX
  305.         mov     al,BIT_MASK
  306.         out     dx,al
  307.         inc     dx
  308.         mov     al,80h
  309.         shr     al,cl
  310.         out     dx,al           ;set the bit mask for the pixel
  311.         shr     bx,1
  312.         shr     bx,1
  313.         shr     bx,1            ;X in bytes
  314.         add     di,bx           ;offset of byte pixel is in
  315.         mov     al,es:[di]      ;load latches
  316.         pop     ax              ;get back dot color
  317.         stosb                   ;write dot in desired color
  318.  
  319.         pop     di
  320.         pop     dx
  321.         pop     cx
  322.         pop     bx
  323.         ret
  324. DotUpInColor    endp
  325. Start   endp
  326. Code    ends
  327.         end     Start