开发者

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 do开发者_JAVA百科n't get how the output is getting printed without any printf or other print statements.


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.


He's writing directly to the video buffer which is usually sitting at that address.

Also, this is seriously old school graphics manipulation.


The reason it's displayed is because 0xB8000000 is the address where video memory starts.


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


First, it gets the address of the beginning of the video buffer. It then clears the screen, and starts adding text to the buffer.


This is the beginning of the video memory address space. What is written to memory here will be displayed on the screen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜