INightmare's Blog

(x86 Assembly) Graphics Part I - Mode 13h

(This tutorial was originally written in 2004 and featured in http://asm.inightmare.org/)

Hello this was the first assembly tutorial I wrote. It was in my Language (Lithuanian), but now I decided to make English site and translate my tutorials to English.

The aim of tutorial is to teach VGA graphics programming in VGA mode 13h (320x200x8bit). So now I’m going to explain how to switch to the mode and put a pixel on the screen.

So switching to the mode 13h is as simple as that:

mov ax, 13h ; AH=0 (Change video mode), AL=13h (Mode)
int 10h ; Video BIOS interrupt

OK. We have the mode switched. Lets put a single pixel on the screen:

mov ax, 0A000h ; The offset to video memory
mov es, ax ; We load it to ES through AX, becouse immediate operation is not allowed on ES
mov ax, 0 ; 0 will put it in top left corner. To put it in top right corner load with 320, in the middle of the screen 32010.
mov di, ax ; load Destination Index register with ax value (the coords to put the pixel)
mov dl, 7 ; Grey color.
mov [es:di], dl ; And we put the pixel

Easy, but not very comfortable when you want to write to coords like (45, 66) or (183, 112). So now lets write a little function that helps us.

mov ax,67 ; Y coord
mov bx,112 ; X coord
mov cx,320
mul cx; multiply AX by 320 (cx value)
add ax,bx ; and add X
mov di,ax
mov dl,7
mov [es:di],dl

Or we can write a little “procedure” that puts pixel on the screen:

putpixel:
mov cx,320
mul cx; multiply AX by 320 (cx value)
add ax,bx ; and add X
mov di,ax
mov [es:di],dl
ret

Before calling it we set ax with Y coord, bx with X and dl with the color.

mov ax, 10,
mov bx, 20
mov dl, 1
call putpixel

To get the pixel from video memory we just change the last assembly line in our putpixel procedure to:

mov dl, [es:di]

Now lets set back the old mode (usually 3):

mov ax,3
int 10h

I hope I helped a little. ;) Here’s the source for the tutorial. Good luck!