2013-10-30 47 views
1

我试图把汇编代码放到我的C函数中。 该功能的目的是存储在SRC地址值复制到DST地址:汇编代码C

void copy(int *dst, int *src);

我的实际问题的代码:

void copy(int *dst, int *src) 
{ 
    asm("mov %1, %2" 
     : /* no output */ 
     : "i" (dst), 
      "i" (src) 
     : "" 
    ); 
} 

给我的错误:

test.c: In function ‘copy’:
test.c:29: error: unknown register name ‘’ in ‘asm’
test.c:29: warning: asm operand 0 probably doesn’t match constraints
test.c:29: warning: asm operand 1 probably doesn’t match constraints

第29行是这一行:
asm("mov %1, %2"

编辑:

asm("mov %0, %1" 
    : "=m" (dst) 
    : "m" (dst), 
     "m" (src) 
    : "" 
    ); 

现在给我:
error: unknown register name ‘’ in ‘asm’ 我不知道做什么用的最后一节做。

EDIT2

我读过,我不能MOV内存 - >内存,我需要使用的寄存器。而且我还需要使用AT & T语法,以便像“mov src,dest”一样。下面的代码编译,但不幸的是,由dst指向的地址中的值是0,而不是我放入由src指向的地址中的值。

asm("movl %1, %%eax \n\t" 
    "movl %%eax, %0 \n\t" 
    : "=m" (dst) 
    : "m" (dst), 
     "m" (src) 
    ); 

EDIT3

我就是这么做的(改变参数),它现在的作品:

void copy(int *dst, int *src, int n) 
{ 
    int a = *src; 
    int b = *dst; 
    asm("movl %1, %%eax\n" 
     "movl %%eax, %0\n" 
     : "=m" (b) 
     : "m" (a) 
     ); 
    *src = a; 
    *dst = b;  
} 
+1

,你有一个输入和一个输出。而且他们都不是我的中间值。一个''m''emory对象应该适用于两者。 – glglgl

+2

这是什么架构? –

+3

你使用什么编译器? – geoffspear

回答

0

您可以使用:

void copy(int *dst, int *src) 
{ 
    __asm__ __volatile__(
    "movl (%0), %%eax\n" 
    "movl %%eax, (%1)\n" 
    : 
    : "r"(src), "r"(dst) 
    : "%eax" 
); 
} 

这就相当于:如果您将值

mov (%edx), %eax ; eax = *src 
mov %eax, (%ecx) ; *dst = eax 
+0

你的回答指导我最正确的答案。 – archibaldis

2

您的撞节空项。你不需要那个。

试试这个:

asm("mov %0, %1" 
    : "=m" (dst) 
    : "m" (dst), 
     "m" (src) 
    /* no clobbers */ 
    ); 

该代码是完全等效于这样的:

*dst = *src 

,所以我想这只是很小的例子吗?

代码编译,但给:

t.c: Assembler messages: 
t.c:2: Error: too many memory references for `mov' 

因此,我认为你的汇编指令需要工作,但是编译器语法确定。

+0

谢谢你的回答。是的,这只是一个小例子。 我试过你的代码,但是编译器告诉我:“错误:在''''令牌'之前的预期字符串字面量”。所以看起来我不会将此部分留空。 – archibaldis

+1

对不起,我忘了删除冒号了;纠正。它现在编译,尽管这仍然不是有效的汇编指令。 – ams

+0

谢谢。我编辑了主要职位。它现在编译,但给出了错误的结果。 – archibaldis