Something strange with pointer to video memory (0xB8000) [duplicate]

冷暖自知 提交于 2019-12-11 10:56:07

问题


I am writing an OS. I created pointer to video area in a RAM (0xb8000). But I have some problems with writing to to the screen.

To make it works (just writing letters) I have to write to memory with offset by 1 (like mem[1] = char, mem[2] = colour). And that worked fine. But when i need to implement scroll i got to copy from one part of memory to another. And here i got problems. I could not get an char from memory. Also this offset seems very strange but it doesn't work without it.

void main() {
    volatile unsigned char* mem = 0xB8000;
    mem[0] = 'X';
    mem[1] = 0xf0; // black on white
    mem[2] = 'Z';
    mem[3] = 0xf0; // black on white
    mem[4] = mem[2]; // this line delete all prev letters from display (like shift them out of screen)
    mem[4] = 0xf0;
}

When I launch it without line mem[4] = mem[2]; it works as it should. But with this line i got very strange result without all prev letters (X and Z)

These are the kinds of results I am seeing when it doesn't work:

This is what happens when I print X by itself. It appears to work:

mem[2] = 'Z' causes different colour of X. And further modification (like mem[4] = 'Z') delete all these chars from screen


回答1:


As outlined in this article, text mode memory takes two bytes for every character on the screen. The first one is the ASCII code byte, the other the attribute byte.

If you're trying to print "XZZ" on the screen, your code should look like :

void main()
{
  volatile unsigned char* mem = 0xB8000;
  mem[0] = 'X';
  mem[1] = 0xf0; // black on white
  mem[2] = 'Z';
  mem[3] = 0xf0; // black on white
  mem[4] = mem[2];
  mem[5] = 0xf0; // black on white
}

Of course, for it to work, you need to make sure you're compiling in 32-bits, like @@MichaelPetch suggested in the comments.



来源:https://stackoverflow.com/questions/56892797/something-strange-with-pointer-to-video-memory-0xb8000

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!