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

  1. ; Program to illustrate the color mapping capabilities of the
  2. ; EGA's palette registers.
  3. ;
  4. ; Updated 6/29/89.
  5. ;
  6. VGA_SEGMENT     equ     0a000h
  7. SC_INDEX        equ     3c4h    ;Sequence Controller Index register
  8. MAP_MASK        equ     2       ;Map Mask register index in SC
  9. BAR_HEIGHT      equ     14      ;height of each bar
  10. TOP_BAR         equ     BAR_HEIGHT*6    ;start the bars down a bit to
  11.                                         ; leave room for text
  12. ;
  13. stackseg        segment para stack 'STACK'
  14.         db      512 dup (?)
  15. stackseg        ends
  16. ;
  17. Data    segment word 'DATA'
  18. KeyMsg  db      'Press any key to see the next color set. '
  19.         db      'There are 64 color sets in all.'
  20.         db      0dh, 0ah, 0ah, 0ah, 0ah
  21.         db      13 dup (' '), 'Attribute'
  22.         db      38 dup (' '), 'Color$'
  23. ;
  24. ; Used to label the attributes of the color bars.
  25. ;
  26. AttributeNumbers        label   byte
  27. x=      0
  28.         rept    16
  29. if x lt 10
  30.         db      '0', x+'0', 'h', 0ah, 8, 8, 8
  31. else
  32.         db      '0', x+'A'-10, 'h', 0ah, 8, 8, 8
  33. endif
  34. x=      x+1
  35.         endm
  36.         db      '$'
  37. ;
  38. ; Used to label the colors of the color bars. (Color values are
  39. ; filled in on the fly.)
  40. ;
  41. ColorNumbers    label   byte
  42.         rept    16
  43.         db      '000h', 0ah, 8, 8, 8, 8
  44.         endm
  45. COLOR_ENTRY_LENGTH      equ     ($-ColorNumbers)/16
  46.         db      '$'
  47. ;
  48. CurrentColor    db      ?
  49. ;
  50. ; Space for the array of 16 colors we'll pass to the BIOS, plus
  51. ; an overscan setting of black.
  52. ;
  53. ColorTable      db      16 dup (?), 0
  54. Data    ends
  55. ;
  56. Code    segment
  57.         assume  cs:Code, ds:Data
  58. Start   proc    near
  59.         cld
  60.         mov     ax,Data
  61.         mov     ds,ax
  62. ;
  63. ; Go to hi-res graphics mode.
  64. ;
  65.         mov     ax,10h  ;AH = 0 means mode set, AL = 10h selects
  66.                         ; hi-res graphics mode
  67.         int     10h     ;BIOS video interrupt
  68. ;
  69. ; Put up relevant text.
  70. ;
  71.         mov     ah,9    ;DOS print string function
  72.         mov     dx,offset KeyMsg
  73.         int     21h
  74. ;
  75. ; Put up the color bars, one in each of the 16 possible pixel values
  76. ; (which we'll call attributes).
  77. ;
  78.         mov     cx,16   ;we'll put up 16 color bars
  79.         sub     al,al   ;start with attribute 0
  80. BarLoop:
  81.         push    ax
  82.         push    cx
  83.         call    BarUp
  84.         pop     cx
  85.         pop     ax
  86.         inc     ax      ;select the next attribute
  87.         loop    BarLoop
  88. ;
  89. ; Put up the attribute labels.
  90. ;
  91.         mov     ah,2    ;video interrupt set cursor position function
  92.         sub     bh,bh   ;page 0
  93.         mov     dh,TOP_BAR/14   ;counting in character rows, match to
  94.                                 ; top of first bar, counting in
  95.                                 ; scan lines
  96.         mov     dl,16   ;just to left of bars
  97.         int     10h
  98.         mov     ah,9    ;DOS print string function
  99.         mov     dx,offset AttributeNumbers
  100.         int     21h
  101. ;
  102. ; Loop through the color set, one new setting per keypress.
  103. ;
  104.         mov     [CurrentColor],0        ;start with color zero
  105. ColorLoop:
  106. ;
  107. ; Set the palette registers to the current color set, consisting
  108. ; of the current color mapped to attribute 0, current color + 1
  109. ; mapped to attribute 1, and so on.
  110. ;
  111.         mov     al,[CurrentColor]
  112.         mov     bx,offset ColorTable
  113.         mov     cx,16   ;we have 16 colors to set
  114. PaletteSetLoop:
  115.         and     al,3fh          ;limit to 6-bit color values
  116.         mov     [bx],al ;built the 16-color table used for setting
  117.         inc     bx      ; the palette registers
  118.         inc     ax
  119.         loop    PaletteSetLoop
  120.         mov     ah,10h  ;video interrupt palette function
  121.         mov     al,2    ;subfunction to set all 16 palette registers
  122.                         ; and overscan at once
  123.         mov     dx,offset ColorTable
  124.         push    ds
  125.         pop     es      ;ES:DX points to the color table
  126.         int     10h     ;invoke the video interrupt to set the palette
  127. ;
  128. ; Put up the color numbers, so we can see how attributes map
  129. ; to color values, and so we can see how each color # looks
  130. ; (at least on this particular screen).
  131. ;
  132.         call    ColorNumbersUp
  133. ;
  134. ; Wait for a keypress, so they can see this color set.
  135. ;
  136. WaitKey:
  137.         mov     ah,8    ;DOS input without echo function
  138.         int     21h
  139. ;
  140. ; Advance to the next color set.
  141. ;
  142.         mov     al,[CurrentColor]
  143.         inc     ax
  144.         mov     [CurrentColor],al
  145.         cmp     al,64
  146.         jbe     ColorLoop
  147. ;
  148. ; Do a mode set to restore text mode to its default state.
  149. ;
  150.         mov     ax,3
  151.         int     10h
  152. ;
  153. ; Done.
  154. ;
  155. Done:
  156.         mov     ah,4ch  ;DOS terminate function
  157.         int     21h
  158. ;
  159. ; Puts up a bar consisting of the specified attribute (pixel value),
  160. ; at a vertical position corresponding to the attribute.
  161. ;
  162. ; Input: AL = attribute
  163. ;
  164. BarUp   proc    near
  165.         mov     dx,SC_INDEX
  166.         mov     ah,al
  167.         mov     al,MAP_MASK
  168.         out     dx,al
  169.         inc     dx
  170.         mov     al,ah
  171.         out     dx,al   ;set the Map Mask register to produce
  172.                         ; the desired color
  173.         mov     ah,BAR_HEIGHT
  174.         mul     ah      ;row of top of bar
  175.         add     ax,TOP_BAR ;start a few lines down to leave room for
  176.                            ; text
  177.         mov     dx,80   ;rows are 80 bytes long
  178.         mul     dx      ;offset in bytes of start of scan line bar
  179.                         ; starts on
  180.         add     ax,20   ;offset in bytes of upper left corner of bar
  181.         mov     di,ax
  182.         mov     ax,VGA_SEGMENT
  183.         mov     es,ax   ;ES:DI points to offset of upper left
  184.                         ; corner of bar
  185.         mov     dx,BAR_HEIGHT
  186.         mov     al,0ffh
  187. BarLineLoop:
  188.         mov     cx,40   ;make the bars 40 wide
  189.         rep     stosb   ;do one scan line of the bar
  190.         add     di,40   ;point to the start of the next scan line
  191.                         ; of the bar
  192.         dec     dx
  193.         jnz     BarLineLoop
  194.         ret
  195. BarUp   endp
  196. ;
  197. ; Converts AL to a hex digit in the range 0-F.
  198. ;
  199. BinToHexDigit   proc    near
  200.         cmp     al,9
  201.         ja      IsHex
  202.         add     al,'0'
  203.         ret
  204. IsHex:
  205.         add     al,'A'-10
  206.         ret
  207. BinToHexDigit   endp
  208. ;
  209. ; Displays the color values generated by the color bars given the
  210. ; current palette register settings off to the right of the color
  211. ; bars.
  212. ;
  213. ColorNumbersUp  proc    near
  214.         mov     ah,2    ;video interrupt set cursor position function
  215.         sub     bh,bh   ;page 0
  216.         mov     dh,TOP_BAR/14   ;counting in character rows, match to
  217.                                 ; top of first bar, counting in
  218.                                 ; scan lines
  219.         mov     dl,20+40+1      ;just to right of bars
  220.         int     10h
  221.         mov     al,[CurrentColor]       ;start with the current color
  222.         mov     bx,offset ColorNumbers+1
  223.                         ;build the color number text string on the fly
  224.         mov     cx,16   ;we've got 16 colors to do
  225. ColorNumberLoop:
  226.         push    ax              ;save the color #
  227.         and     al,3fh          ;limit to 6-bit color values
  228.         shr     al,1
  229.         shr     al,1
  230.         shr     al,1
  231.         shr     al,1            ;isolate the high nibble of the
  232.                                 ; color #
  233.         call    BinToHexDigit   ;convert the high color # nibble
  234.         mov     [bx],al         ; and put it into the text
  235.         pop     ax              ;get back the color #
  236.         push    ax              ;save the color #
  237.         and     al,0fh          ;isolate the low color # nibble
  238.         call    BinToHexDigit   ;convert the low nibble of the
  239.                                 ; color # to ASCII
  240.         mov     [bx+1],al       ; and put it into the text
  241.         add     bx,COLOR_ENTRY_LENGTH   ;point to the next entry
  242.         pop     ax              ;get back the color #
  243.         inc     ax              ;next color #
  244.         loop    ColorNumberLoop
  245.         mov     ah,9            ;DOS print string function
  246.         mov     dx,offset ColorNumbers
  247.         int     21h             ;put up the attribute numbers
  248.         ret
  249. ColorNumbersUp  endp
  250. ;
  251. Start   endp
  252. Code    ends
  253.         end     Start