【发布时间】:2017-03-28 15:26:09
【问题描述】:
我正在为我的 OSDev 操作系统制作键盘驱动程序,但我的 kbd.c 有问题:
kbd.c: In function 'scancoderec':
kbd.c:56:2: error: variable-sized object may not be initialized
register int (ScanCode[strlen(ValEAX)-8]) = 0x00; /* Remove last 8 bits from the value we gathered from EAX to get AH and make that the scancode. */
这是包含失败代码行的函数:
int scancoderec() {
__asm__ volatile( "movl $0, %eax" ); /* Moving 00 to EAX. */
__asm__ volatile( "int $0x16 "); /*int 0x16 */
register int ValEAX asm("eax"); /* Let's get eax */
register int (ScanCode[strlen(ValEAX)-8]) = 0x00; /* Remove last 8 bits from the value we gathered from EAX to get AH and make that the scancode. */
}
为什么会这样?
编辑:我仍然认为“ax”是未定义的,这一次,在另一个函数中。
kbd.c:65:27: error: 'ax' undeclared (first use in this function)
register int Key = kbdus[ax];
scancode函数和getkey函数代码:
unsigned short scancodeget()
{
unsigned char ax = 0; /* int 0x16, AH=0 is get keystroke */
__asm__ __volatile__ ("int $0x16\n\t"
: "+a"(ax));
ax = ax >> 8; /* Shift top 8 bits of ax to lower 8 bits */
/* ax now is the scancode returned by BIOS */
return (unsigned short)ax; /* Return the lower 8 bits */
}
int getkey() { /*This could be used as a keyboard driver. */
scancoderec(); /*Get our scancode! */
int Key = kbdus[ax]; /*Use our kbdus array which i copied from a website since i seriously don't want to make an gigantic array */
}
【问题讨论】:
-
编译器认为您正在声明一个名为
ScanCode的数组。坦率地说,我不知道你想在该代码中做什么。 -
评论说“从值中删除最后 8 位...” --> 看起来像“删除最后 8 个 字节 ...”
-
嗯,如果
strlen(ValEAX) <= 8,代码肯定有问题。 -
ax(在我的回答中)是在scancodeget()中定义的局部变量的名称,因此仅在该函数中可用。您可以执行类似uint8_t scancode = scancodeget();然后int Key = kbdus[scancode];之类的操作。我猜你是 C 语言的新手? -
你把类型弄混了。它应该是
unsigned short ax = 0;(一个 16 位变量)。返回的扫描码仅为 8 位值,因此您可以将unsigned short scancodeget()更改为unsigned char scancodeget()和return (unsigned short)ax;更改为return (unsigned char)ax;
标签: gcc x86 keyboard-events inline-assembly osdev