【问题标题】:gcc asm too many memory referencesgcc asm 内存引用过多
【发布时间】:2016-01-08 11:06:31
【问题描述】:

我正在尝试使用 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编译

【问题讨论】:

标签: c for-loop gcc memory reference


【解决方案1】:

正如 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 是受保护的指令,不能由用户模式应用程序使用。我假设你是在 DOS 上运行它吗?

【讨论】:

    【解决方案2】:

    gcc 使用 AT&T 语法。 用-masm=intel编译

    【讨论】:

      猜你喜欢
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 2011-02-01
      相关资源
      最近更新 更多