Chapter 11 Graphics

Table of Contents
11.1 Text Mode
11.2 VGA Mode 13h Graphics
11.3 Interrupt 10h Video Reference
11.4 PCX Graphic File Format

This chapter describes the various common graphics modes and how to use them in assembly language, as well as how to implement various algorithms for drawing lines and other images on the graphics screen.

11.1 Text Mode

11.1.1 Displaying Text on the Screen

11.1.1.1 INT 10h Functions

One way to display text on the screen quickly is to use the BIOS interrupt 10h functions. See the INT 10h function list elsewhere for a complete description of these functions. A brief list of the more useful functions is given here:

Function 0 Set Video Mode
Function 2 Set Cursor Position
Function 6 Scroll Active Page Up
Function 9 Write Attribute/character at Current Cursor Position

11.1.1.2 Library Routines

In addition to these interrupts, the following subroutines for displaying characters are available through the LIB291 library file:

dspout

This subroutine prints the character found in DL to the screen, at the current cursor position. The character must be in ASCII. The contents of DL are preserved upon return. The cursor is advanced after the write.

dspmsg

This subroutine will print to the screen, starting at the current cursor position, a string of ASCII characters. DX must contain an offset from DS to the location of the first character to be printed. In addition, the string must end with an ASCII dollar sign ($), and hence may not contain one. (The dollar sign is the text delimiter). The contents of DX are preserved upon return.

11.1.1.3 Direct Writes to Video Memory

A third method for displaying text involves accessing the video memory directly. The contents of the text screen are stored in memory beginning at address B8000h. Each character on the text display comprises one word in memory, the meaning of which is shown in the following figure:

The attribute byte gives information about the color of that particular character on the screen (discussed in the following section). The character byte is simply the 8-bit ASCII code for the character at that position.

The screen is divided into rows and columns, with the upper-left character position usually referred to as row 0 and column 0. The first row of the screen is stored first beginning with the first column (row 0, column 0), then the next row, and so on. Thus, an 80x25 text screen requires

to store the entire screen.

The following code fragment illustrates an example of how to access the video memory. This code fragment will change the top row of characters to yellow "A"s on a blue background. Note the use of segment register ES to access the memory at absolute address B8000h.

        mov     bx, 0           ; Begin pointer at row 0 and column 0
        mov     ax, 0B800h      ; Set up ES to point to video segment
        mov     es, ax
        mov     cx, 80          ; Counter for number of columns in top row
.lp:
        mov     [es:bx], 1E41h  ; Yellow 'A' on blue background
        add     bx, 2           ; Update pointer to next character position
        loop    .lp             ; Do 80 characters

11.1.2 Attributes

11.1.2.1 The Attribute Byte

The attribute byte specifies the colors for the character and its background. The table below describes the format for the attribute byte. You can write a character and its attribute to the display using Interrupt 10h, Function 09h (see Section 11.3.8), or by writing directly to video memory. The structure of the attribute is shown in the following figure:

Blink 1 = blink on, 0 = off.
Background Selects a color from the first eight colors in the palette.
Foreground Selects one of the 16 colors in the palette.

11.1.2.2 Color Palette

0 -- Black 8 -- Dark Gray
1 -- Blue 9 -- Light Blue
2 -- Green 10 -- Light Green
3 -- Cyan 11 -- Light Cyan
4 -- Red 12 -- Light Red
5 -- Magenta 13 -- Light Magenta
6 -- Brown 14 -- Yellow
7 -- Light Gray 15 -- White

After power up the attributes for the entire screen are set to (00000111b), the attribute for a light gray character and a black background, with no blinking.