Directly Accessing Video Memory from a C Program

Last reviewed: July 18, 1997
Article 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:

  • Microsoft C for MS-DOS, versions 6.0, 6.0a, and 6.0ax
  • Microsoft C/C++ for MS-DOS, version 7.0
  • Microsoft Visual C++ for Windows, versions 1.0 and 1.5

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


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.