CSE240

Direct video memory access


On standard VGA graphics cards, the video memory for the screen in standard 80x25 text mode is located at segment B800h. In addition, it is important to note that video memory is set up in charcter-attribute pairs, with characters at the even offsets and attributes at the odd offsets. It is not necessary to modify any of the attribute bytes to display text on the screen. The attributes are usually preset to the value 7, which is light gray text on a black background.

The following code example places the letter 'A' in the upper left corner of the screen:

        mov ax, 0B800h          ;- set up es to point to video memory
        mov es, ax
        mov di, 0000            ;- point to first (upper left) character
        mov al, 'A'             ;- character to display
        mov es:[di], al         ;- display it
To display a null-terminated string:
        mov ax, 0B800h
        mov es, ax
        mov di, 0000
        mov bx, 0
        lea si, String
LoopTop:
        mov al, [si+bx]
        cmp al, 0
        jz Finished
        mov es:[di+bx], al
        inc bx
        jmp LoopTop

If these code fragments are tested, it is important to clear the screen from the DOS command prompt just prior to executing the test program. If the command was typed on the bottom line of the screen, the screen will scroll by 1 or 2 lines when the program returns to DOS, scrolling the output off the screen faster than it can be seen.
Back to CSE240 main page
Copyright © 1997 Jeffrey A. Meunier