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

前端 未结 6 763
时光取名叫无心
时光取名叫无心 2020-12-15 12:35
#include 
#include 

void main()
{
  char far *v = (char far*)0xb8000000;
  clrscr();

  *v = \'w\';
  v += 2;
  *v = \'e\';

  getch()         


        
6条回答
  •  再見小時候
    2020-12-15 13:08

    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.

提交回复
热议问题