问题
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