c - gcc asm 内存引用过多

标签 c for-loop gcc memory reference

我尝试使用 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)上运行。 InOut 是 protected 指令,不能由用户模式应用程序使用。我假设你在 DOS 或其他东西上运行它?

关于c - gcc asm 内存引用过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33056232/

相关文章:

c - 尝试打印结构元素,但结果为空白。 - C

batch-file - 批处理文件-搜索任何包含文本的文件夹,如果找不到任何文件夹,则继续

linux - 在管道 awk 输出上打印循环输入文件名

c++ - 什么时候会在编译过程中删除琐碎的(没有效果的代码)代码?

C:inet_ntop返回值位置?

c - 使用 execve() 执行反向 shell

linux - gcc 可以在不交叉编译的情况下为 Arm 生成二进制文件吗

c - 相同的程序 GCC 崩溃但 msvc 可以工作

c++ - 推送 Lua 表

batch-file - 如何从 FOR 循环中批量为全局变量赋值