从汇编函数返回浮点数

时间:2019-05-25 11:48:37

标签: c assembly x86 floating-point nasm

我需要获取一个由c代码调用的汇编函数计算的浮点值。

这是我的c代码:

extern float test_distance(VECTOR x1, VECTOR x2, int d);

void testIndex(){
    float x[5] = {1, 2, 3, 8, 7};
    float y[5] = {9, 8, 7, 6, 1};
    printf("Distance Assembly: %f\n", test_distance(x, y, 5) );

}

现在我需要获取任何值,然后我将计算两个向量之间的距离。

以下是验证码:

section .data           

section .bss            

section .text           

global test_distance      ; makes function visible from extern

    x1   equ   8     ; pointer a float x1, needs 32 bit (4 bytes)
    x2   equ   12    ; puntatore a float x2, needs 32 bit (4 bytes)
    d    equ   16    ; int 32 bit, dimension of vectors

test_distance:

    push    ebp       ; save Base Pointer
    mov     ebp, esp  ; Base Pointer point to Activation Record 
    push    ebx       ; save register
    push    esi
    push    edi
    ;
    ; put funcion here
    ;

    pop   edi       ; restores registers
    pop   esi
    pop   ebx
    mov   esp, ebp  ; restores Stack Pointer
    pop   ebp       ; restores Base Pointer
    ret             

此汇编代码是由我的老师提供给我的,因此我想使用此结构。 我唯一需要做的就是获取在c中使用的float值,但我不知道该怎么做。

目前使用的体系结构是32位。我必须将它们都写成32位和64位。

我尝试过:

movaps xmm0, [ebp+x1]  ; reads x1
movaps xmm1, [ebp+x2]  ; reads x2
addps xmm0, xmm1       ; adds for now the registers values by values
movsd [ebp+d], xmm0    ; saves values computes to stack

当我使用它时,我得到'-nan'值。

我也尝试过

fld num1   ; This pushes num1 onto the stack at the address of st(0)
fld num2   ; This pushes num2 onto the stack at the address of st(1)
faddp      ; This multiplies st(1) and st(0), stores the result at st(0),            and then pops the variable stored at st(1) off the stack

谢谢

0 个答案:

没有答案