【发布时间】:2015-05-30 22:56:51
【问题描述】:
我正在从头开始制作一个 C 内核,实际上我只是从网站上复制了这段代码,因为我的代码不起作用,所以我很困惑。
void kmain(void)
{
const char *str = "my first kernel";
char *vidptr = (char*)0xb8000; //video mem begins here.
unsigned int i = 0;
unsigned int j = 0;
/* this loops clears the screen
* there are 25 lines each of 80 columns; each element takes 2 bytes */
while(j < 80 * 25 * 2) {
/* blank character */
vidptr[j] = ' ';
/* attribute-byte - light grey on black screen */
vidptr[j+1] = 0x07;
j = j + 2;
}
j = 0;
/* this loop writes the string to video memory */
while(str[j] != '\0') {
/* the character's ascii */
vidptr[i] = str[j];
/* attribute-byte: give character black bg and light grey fg */
vidptr[i+1] = 0x07;
++j;
i = i + 2;
}
return;
}
当我运行我的内核时,它会在屏幕上打印一个 S 而没有别的。我知道我的内核正在启动,因为如果我这样做了
vidptr[0] = 'h';
vidptr[2] = 'e';
vidptr[4] = 'l';
vidptr[6] = 'l';
vidptr[8] = 'o';
它按预期工作。发生了什么?
编辑:可能是我的代码加载了内核(可能没有设置一些寄存器),所以我只会研究 grub 和其他东西。
【问题讨论】:
-
什么是 C 内核? C 只是一种编程语言
-
这就是为什么你不会为了学习而复制代码。
-
@StefanFalk 对不起,我不清楚,我一直在研究我的内核,当我尝试制作打印功能时,我无法让它工作,所以我去了一个网站来尝试代码,看看它是否只是我的代码。
-
它不可能使用您存储在数组
'0' != '\0'中的值。 -
@iharob 是的,我也是这么想的。如果我只是做 vidptr[0] = str[0];它不起作用。