问题
#include <stdio.h>
#include <conio.h>
void main()
{
char far *v = (char far*)0xb8000000;
clrscr();
*v = 'w';
v += 2;
*v = 'e';
getch();
}
Output is: we
I don't get how the output is getting printed without any printf
or other print statements.
回答1:
This is a x86 real-mode IBM PC program that assumes CGA/EGA/VGA compatible graphics adapter in color text mode mapped at the default memory location (B800:0000); it is basically from the era of MS-DOS (1980s/1990s). In any case it's very old school!
char far *v=(char far*)0xb8000000;
Memory address (in real mode) of the video buffer (use 0xb0000000
if you have an old Hercules)
clrscr();
Clears the screen
*v='w';
Writes at row 0
, column 0
the character w
v+=2;
Skips 2 bytes
(in the character mode the buffer is interleaved: 1 byte
for the character and 1 byte
for the color. 1 bit
for the flashing, 3 bits
for the background 0-7 and 4 bits
for the foreground 0-15, packed in this way: foreground + 16 * background + 128 if you want flashing
)
*v='e';
Writes at row 0
, column 1
the character e
getch();
Waits for a key
Now a link about the CGA Text Mode Format, for those that FEEL the need of knowing how the "old generation" did it, before "Windows" came (and even before all that "Linux" came :-) ). Ah... and another link (a wiki this time) for those that still don't know what REAL-MODE is.
回答2:
He's writing directly to the video buffer which is usually sitting at that address.
Also, this is seriously old school graphics manipulation.
回答3:
The reason it's displayed is because 0xB8000000
is the address where video memory starts.
回答4:
You didn't specify what platform it is, and it's apparently not one that would crash this nasty code.
0xb8000000
on the legacy DOS platform was the video memory buffer, so in text mode, you could write characters there directly. See here: http://wiki.answers.com/Q/What_is_0xB8000000
回答5:
First, it gets the address of the beginning of the video buffer. It then clears the screen, and starts adding text to the buffer.
回答6:
This is the beginning of the video memory address space. What is written to memory here will be displayed on the screen.
来源:https://stackoverflow.com/questions/5096620/writing-to-0xb8000000-yields-output-on-screen-without-any-print-statements-such