问题
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
回答1:
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 = '!';
回答2:
Can you try this (untested as I don't have my old PC)
char far* video = 0xb8000000L;
*(video++) = '!';
*(video++) = 0x0A;
回答3:
Just create a pointer to the base address and then access the memory like it's an array. Recall that in text mode, there are two bytes for each character shown on screen. The first, holds the character itself. The second holds the attribute. The high-order 4 bits are the background attribute and the low 4 are the foreground. Setting the highest bit in the foreground attribute makes it a high-intensity colour, while setting the high order bit in the background attribute causes the foreground to flash. This means that there are 8 colours available for the background, 16 available for the foreground and finally the ability to make the text blink.
E.g for mode 0x13 stuff:
char far *graphScreen = (char far*) 0xA0000000;
And for text mode stuff,
char far *textScreen = (char far*) 0xB8000000;
To write to screen memory is then as simple as
textScreen[ someIndex ] = someChar;
textScreen[ someIndex+1 ] = someAttrib;
来源:https://stackoverflow.com/questions/32972051/in-c-how-do-i-write-to-a-particular-memory-location-e-g-video-memory-b800-in