如何从另一个程序集文件中调用汇编函数?

时间:2016-05-20 00:52:47

标签: c ubuntu nasm

实际上,我正在使用NASM作为我的学者项目。我们的想法是为数学运算构建一个静态库。

我已经能够在不同的asm文件中构建单独的函数。例如:

  • add_vectorial

    • 有3个参数:
      • d,尺寸
      • v1,float of array
      • v2,float of array
      • rt,必须包含操作结果的float数组
  • sub_vectorial

    • 有3个参数:
      • d,尺寸
      • v1,float of array
      • v2,float of array
      • rt,必须包含操作结果的float数组

但是我有第三个asm文件,它必须调用2个函数: add_vectorial sub_vectorial 来进行一些微积分。

我已经读过我必须使用调用词来调用我的外部函数。但我不知道如何传递参数?

我的代码示例:

extern add_vectorial

global operation:    ;for linux

operation:
            ;Initialize a stack frame
            push    ebp
            mov     ebp, esp
            ;Loading the arguments values
            mov     ebx, [ebp+8]    ; ebx='d'
            mov     ecx, [ebp+12]   ; ecx='v1'
            mov     edx, [ebp+16]   ; edx='v2'
            mov     eax, [ebp+20]   ; eax = rt

            ;Initial the xmm4 registry with zero.
            xorps   xmm4, xmm4
.body:

            ;Here, How can i pass the parameters to my asm external function?
            call   add_vectorial

.done:
            ;Restore the call's stack frame pointer
            leave                   ; mov esp,ebp / pop ebp 
            ret                     ; return from function

3 个答案:

答案 0 :(得分:0)

您可以在执行call语句之前将参数传递到堆栈中,也可以先将它们插入寄存器中来传递参数。

答案 1 :(得分:0)

我已经创建了一个宏来调用我的外部函数,传递参数,如C。

%macro call_fun1  4
push %4
push %3
push %2
push %1
call sub_vectorial
add  ebp, 16   ;4p*4bytes
%endmacro

只有当我将此代码与我的主asm文件集成时,请给我以下错误:编程接收信号SIGSEGV,分段错误。

由于这个原因,我对我的功能进行了一次单元测试,但功能正常。

错误只是在我与其余代码集成时给出。

答案 2 :(得分:0)

谢谢你们的支持......最后我找到了解决问题的方法......我留下了解决方案......

%macro call_fun1  4
pushad             ;I put this
push %4
push %3
push %2
push %1
call sub_vectorial
add  esp, 16   ;4p*4bytes
popad             ;I put this
%endmacro