2011-11-28 141 views
0

:96: Error: `(%rax,%edx,4)' is not a valid base/index expressionasm错误信息:`(%rax,%edx,4)'不是有效的基数/索引表达式

line97: Error: `-4(%rax,%edx,4)' is not a valid base/index expression

line101: Error: `(%rax,%edx,4)' is not a valid base/index expression

line102: Error: `-4(%rax,%edx,4)' is not a valid base/index expression

我收到这些错误消息,不知道如何解决。

这是我的代码:

__asm__ (

      "loop:  \n\t" 
      "movl  $1,%3\n\t" 
      "movl  $0, %6\n" 

      "start:  \n\t" 

      "movl  (%1,%3,4),%4\n\t"  
      "movl  -4(%1, %3, 4), %5\n\t" 

      "cmpl  %4, %5\n\t"   
      "jle   next\n\t" 

      "xchgl  %4, %5\n\t"    
      "movl  %4, (%1, %3, 4)\n\t"   
      "movl  %5, -4(%1, %3, 4)\n\t"   
      "movl  $1, %6\n\t" 

      "next:  \n\t" 
      "incl  %3 \n\t"   

      "cmpl  %3, %2\n\t" 
      "jge  start\n\t"   

      "cmpl  $0, %6\n\t" 
      "je  end\n\t" 

      "jmp  loop\n\t"   
      "end:  \n\t" 

一些帮助解释如何解决这些错误信息,请。 我正在尝试在ASM中进行冒泡排序。

+0

为什么不重新格式化你的代码? – Beginner

+0

ppl说它不清楚,所以我试图清除它? – user1055916

+1

[asm编译问题]的可能重复(http://stackoverflow.com/questions/8290865/asm-compile-issue) – hirschhornsalz

回答

6

你没有说你想要的是什么处理器,但它看起来是x64。在x64上,(%rax, %edx, 4)不是合法的组合。请参阅处理器手册以获取有效寻址模式的列表。我的猜测是你的意思是(%rax, %rdx, 4)

3

最可能的原因是在%3操作数中使用了明确的32位整数类型。您没有显示内联程序集的约束列表。但上面的,如果你这样做时:

int main(int argc, char **argv) 
{ 
    int result, foobaridx; 

    foobaridx = foobar[4]; 

    __asm__ (
     "  dec %2\n\t" 
     "  movl (%1, %2, 4), %0\n\t" 
    : "=r"(result) : "r"(foobar), "r"(foobaridx) : "memory", "cc"); 

    return result; 
} 

在32位模式编译这个工程好吗:

$ gcc -O8 -m32 -c tt.c 
$ objdump -d tt.o 

tt.o:  file format elf32-i386 

00000000 : 
    0: 55      push %ebp 
    1: b8 00 00 00 00   mov $0x0,%eax 
    6: 89 e5     mov %esp,%ebp 
    8: 83 ec 08    sub $0x8,%esp 
    b: 8b 15 10 00 00 00  mov 0x10,%edx 
    11: 83 e4 f0    and $0xfffffff0,%esp 
    14: 4a      dec %edx 
    15: 8b 04 90    mov (%eax,%edx,4),%eax 
    18: 83 ec 10    sub $0x10,%esp 
    1b: c9      leave 
    1c: c3      ret

但在64位模式下,编译/汇编程序不喜欢它:

$ gcc -O8 -c tt.c 
/tmp/cckylXxC.s: Assembler messages: 
/tmp/cckylXxC.s:12: Error: `(%rax,%edx,4)' is not a valid base/index expression

解决这个问题的方法是使用#include <stdint.h>和最终将用于寻址(作为基址或索引寄存器)到uintptr_t(这是一个整型数据类型无论你使用的是32位还是64位,都可以保证与指针大小兼容)。随着这种变化,64位编译成功,并创建以下的输出:

$ gcc -O8 -c tt.c 
$ objdump -d tt.o 

tt.o:  file format elf64-x86-64 

Disassembly of section .text: 

0000000000000000 : 
    0: 48 63 15 00 00 00 00 movslq 0(%rip),%rdx  # 7 
    7: b8 00 00 00 00   mov $0x0,%eax 
    c: 48 ff ca    dec %rdx 
    f: 8b 04 90    mov (%rax,%rdx,4),%eax 
    12: c3      retq

好运气让你的内联汇编“32位/ 64位无关”!

相关问题