Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

c - Writing to 0xb8000000 yields output on screen without any print statements such as `printf`

#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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...