Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何将int从xmm寄存器移动到eax寄存器?_C_Assembly_X86_Nasm_Sse - Fatal编程技术网

C 如何将int从xmm寄存器移动到eax寄存器?

C 如何将int从xmm寄存器移动到eax寄存器?,c,assembly,x86,nasm,sse,C,Assembly,X86,Nasm,Sse,我必须调用一个汇编方法来计算C中两个整数的加法,使用sse 简单的C代码是: #include <stdio.h> #include <stdlib.h> extern int add(int a, int b); int main(){ int a=5; int b=2; int n = add(a,b); printf("%d\n",n); return 0; } 但这样我需要从存储在内存中的RISULTATO传递

我必须调用一个汇编方法来计算C中两个整数的加法,使用sse

简单的C代码是:

#include <stdio.h>
#include <stdlib.h>

extern int add(int a, int b);

int main(){

    int a=5;
    int b=2;
    int n = add(a,b);
    printf("%d\n",n);

    return 0;

}

但这样我需要从存储在内存中的RISULTATO传递。 有没有办法避免这种情况,直接将xmm0中存储的结果移动到eax

当然,我需要在eax中移动加法的结果以将其传递给我的C方法,因为返回值必须在eax寄存器中


谢谢你的帮助。:)

如果查看指令集参考,可以找到
MOVD
指令。如果您再看一看,您可能还会注意到
addss
添加浮点而不是整数。您需要
padd
。整个代码可以简化为:
movdxmm0[esp+4];MOVD xmm1,[esp+8];paddxmm0,xmm1;movdeax,xmm0;RET
。谢谢!MOVD似乎工作正常:)或
movq
一次加载两个值,然后
phadd
(水平添加)。但这只是有用的,因为数据移动比计算要多
phadd
padd
慢得多。
section .bss
    RISULTATO   resd    1

section .text

global add

add:
    ;-----------------------------------------------
    ;   start point of the function
    ;-----------------------------------------------

    push ebp    ; salva il Base Pointer
    mov ebp, esp    ; il Base Pointer punta al Record di Attivazione corrente
    push ebx    ; salva i registri da preservare
    push esi
    push edi

    ;-----------------------------------------------
    ;   add implementation
    ;-----------------------------------------------

    movss xmm0, [ebp+8]
    movss xmm1, [ebp+12]

    addss xmm1, xmm0
    movss [RISULTATO], xmm1

    mov eax, [RISULTATO]

    ;-----------------------------------------------
    ;   exit point of the function
    ;-----------------------------------------------

    pop edi     ; ripristina i registri da preservare
    pop esi
    pop ebx
    mov esp,ebp ; ripristina lo Stack Pointer
    pop ebp     ; ripristina il Base Pointer
    ret     ; ritorna alla funzione chiamante