ACCESSING VGA MEMORY

Nearly all programs these days use graphics of some sort. In order to provide fast access to changing what the screen contains, the video-card reads the contents of video-memory for VGA modes from the same location in all PCs. The beginning of VGA video memory is at the SEGMENT:OFFSET address of 0xA000:0x0000 and goes on to 0xA000:0xFFFF for a total of 0xFFFF bytes (64k).

This technique to accessing VGA memory will not normally work under a 32-bit protected-mode environment. If you are targeting Win32 or 32-bit DOS then you will get an access violation if you try to use this pointer.
The only exception I know of is Watcom C when using DOS/4GW because if you use VGA's linear address (0xA0000) as a 32-bit memory offset (char *screenptr = (char*)0xA0000;) then, so I've heard, you can access it just like in 16-bit mode. This is a big feature that makes Watcom the compiler of choice for 32-bit DOS game programmers.

This address is represented as a far pointer so in order to access this memory directly you can declare a pointer in your program that points to this location. This location is outside of your program's own code and memory so the pointer must be far and since each pixel is represented by a char you should make it of 'char' type. There are two ways to code this and they get the same result ...

#include < dos.h > //Defines MK_FP(int,int) macro

//Create a pointer to VGA video-memory (0xA000:0x0000) using MK_FP(segment,offset)
char far *screenptr = (char far*) MK_FP(0xA000,0x0000); 

OR

//Create a pointer to VGA video-memory (0xA000:0x0000) by combining the SEG:OFF address
char far *screenptr = (char far*) 0xA0000000L; //Has to be a 'long' value since its so big
Now that you've got a pointer video-memory you can treat it just like an array of chars.
To determine which elements of your 'pixel array' change which pixels on the screen see putting a pixel at (x,y). If you take a look at the picture to the right you'll see how the video card scans VGA memory, converts it to a video-signal and sends it to the monitor, and there's you with your program's little block of memory. By making a pointer to VGA you are addressing the start of the memory block labelled VGA.