Search code examples
cdosturbo-c

In C, how do I write to a particular memory location e.g. video memory b800, in DOS (real DOS, MS DOS 6.22)


In C, how do I write to a particular memory location e.g. video memory b800, in DOS (real DOS, MS DOS 6.22)

I understand that C doesn't have anything built in to do that, but that there may be some platform specific e.g. DOS specific API functions that can.

A small demo program that does it would be great.

I have Turbo C (TCC.EXE - not tiny c compiler, Turbo C compiler)

I know debug can do it (e.g. some of the tiny bit of debug that I know) -f b800:0 FA0 21 CE (that writes some exclamation marks to the command line). But i'd like a C program to write to b800:0


Solution

  • The address b800:0000 uses a segment of 0xb800 and an offset of 0x0000. This corresponds to the linear address 0xb8000 (note the extra 0, as the segment is shifted left by 4 bits).

    To create a pointer to this address in protected mode, you'd use

    char *p = (char *)0xb8000;
    

    However, you are most likely in real mode, so you need to construct a far pointer:

    char far *p = (char far *)0xb8000000;
    

    The 32 bit value is split in two 16 bit values, which are assigned to segment and offset.

    You can use this pointer normally, then:

    *p = '!';