How to call a function in an external assembly file [duplicate]

試著忘記壹切 提交于 2021-01-29 05:38:07

问题


I am trying to go from a C function to a function with inline-asm to a standalone function in an assembly file. Here is what I have so far:

#include <stdio.h>

int add_five(int n)
{
    n = n + 5;
    return n;
}

int add_five_inline(int n)
{
    asm("lea 5(%1), %0" : "=r" (n) : "r" (n));
    return n;
}

int main(void)
{
    int a=add_five(7);
    int b=add_five_inline(8);
    // int c=add_five_asm(9); // <-- here
    printf("7+5=%d | 8+5=%d\n", a, b);

}

7+5=12 | 8+5=13

What is the process to take something like the following asm and call it from a C function?

# file.s
add_five_asm:
    lea 5(%rdi), %rax
    ret

How would I call and compile so that I can call that asm function 'as-if' it were a normal C function?

来源:https://stackoverflow.com/questions/65837016/how-to-call-a-function-in-an-external-assembly-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!