我尝试使用 asm 从 CMOS 读取时间,但收到此错误:
/tmp/ccyx8l5L.s:1236: Error: too many memory references for 'mov'
/tmp/ccyx8l5L.s:1240: Error: too many memory references for 'out'
/tmp/ccyx8l5L.s:1244: Error: too many memory references for 'in'
/tmp/ccyx8l5L.s:1252: Error: too many memory references for 'mov'
这是代码:
for (index = 0; index < 128; index++) {
asm("cli");
asm("mov al, index"); /* Move index address*/
asm("out 0x70,al"); /* Copy address to CMOS register*/
/* some kind of real delay here is probably best */
asm("in al,0x71"); /* Fetch 1 byte to al*/
asm("sti"); /* Enable interrupts*/
asm("mov tvalue,al");
array[index] = tvalue;
}
我使用gcc来编译
最佳答案
正如 Janycz 指出的,您的代码使用 intel 语法进行汇编,而 gcc(默认情况下)需要 at&t。如果您使用的是 gcc,那么像这样的东西怎么样:
int main()
{
unsigned char array[128];
for (unsigned char index = 0; index < 128; index++) {
asm("cli\n\t"
"out %%al,$0x70\n\t"
/* some kind of real delay here is probably best */
"in $0x71, %%al\n\t" /* Fetch 1 byte to al*/
"sti" /* Enable interrupts*/
: "=a" (array[index]) : "a" (index) );
}
}
这可以最大限度地减少您必须编写的 asm 数量(4 行与 6 行),并允许编译器执行更多优化。请参阅 gcc 的文档 inline asm了解这一切是如何运作的。
我还没有尝试运行它,因为它不能在保护模式操作系统(如 Windows)上运行。 In
和 Out
是 protected 指令,不能由用户模式应用程序使用。我假设你在 DOS 或其他东西上运行它?
关于c - gcc asm 内存引用过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33056232/