Directly Accessing Video Memory from a C Program

ID: Q12010

6.00 6.00a 6.00ax 7.00 | 1.00 1.50

MS-DOS                 | WINDOWS
kbprg

The information in this article applies to:

SUMMARY

The text below presents a sample C function that demonstrates directly accessing video memory addressed starting at segment B800 in the MS-DOS environment.

MORE INFORMATION

When an application directly accesses a particular memory location, it becomes less portable, and may not run as expected on another machine or on a future machine. Given this warning, the code example below directly accesses video memory.

Sample Code

/*
 * Compile options needed: None
 */ 

// Video.c -- Function to place a character and its standard
//            attribute into the desired video memory page.
// 
//   Note: This function assumes that the display is set to
//         color or monochrome, 80-column, text mode.

#include <dos.h>

#define MAKELONG(a, b)  ((long)(((unsigned)a) \ 
                       | ((unsigned long)((unsigned)b)) << 16))
#define COLORTEXT_BUFFER   0XB800

void video(int pageno, int row, int col, char *ch, char attrib)
// pageno : page number to load character into (0 to 3)
// row    : row of location 0 to 24
// col    : column of location 0 to 79
// ch     : character to be placed there
// attrib : standard character attribute
{
   unsigned int offset; // Offset from the segment address of
                        //   the desired video page
   char far *y;         // Long pointer to the position in memory
                        // where we will put the character and
                        // its attribute (next byte)

// Calculate the in-page offset with
// page number offset and segment address
   offset = (unsigned int) ((row * 160 ) + (col * 2)
      + (pageno * 4096));

// Set the character.
   y = (char far *)MAKELONG(offset, COLORTEXT_BUFFER);
   *y = *ch;

// Set the attribute byte. See an MS-DOS Programmer's
// Reference manual for more information on video attributes.
   offset++;
   y = (char far *)MAKELONG(offset, COLORTEXT_BUFFER);
   *y = attrib;
}

Additional reference words: kbinf 1.00 1.50 6.00 6.00a 6.00ax 7.00 KBCategory: kbprg KBSubcategory: CLngIss Keywords : kb16bitonly

Last Reviewed: July 19, 1997