指向视频内存的指针有些奇怪(0xB8000) [英] Something strange with pointer to video memory (0xB8000)

查看:26
本文介绍了指向视频内存的指针有些奇怪(0xB8000)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个操作系统.我在 RAM (0xb8000) 中创建了指向视频区域的指针.但是我在写入屏幕时遇到了一些问题.

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.

为了使它工作(只是写字母)我必须以 1 的偏移量写入内存(例如 mem[1] = char, mem[2] = colour>).这工作得很好.但是当我需要实现滚动时,我必须从内存的一部分复制到另一部分.在这里我遇到了问题.我无法从内存中获取字符.此外,这个偏移量看起来很奇怪,但没有它就无法工作.

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;
}

当我在没有行 mem[4] = mem[2]; 的情况下启动它时,它可以正常工作.但是有了这条线,我得到了非常奇怪的结果,没有所有上一个字母(X 和 Z)

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:

这就是我自己打印 X 时发生的情况.它似乎有效:

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

mem[2] = 'Z' 导致 X 的颜色不同.并进一步修改(如 mem[4] = 'Z')从屏幕中删除所有这些字符

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

推荐答案

这篇文章中所述, 文本模式内存为屏幕上的每个字符占用两个字节.第一个是ASCII码字节,另一个是属性字节.

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.

如果您尝试在屏幕上打印XZZ",您的代码应如下所示:

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
}

当然,要使其正常工作,您需要确保以 32 位编译,如评论中建议的@@MichaelPetch.

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

这篇关于指向视频内存的指针有些奇怪(0xB8000)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆