0

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?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
  • What operating system and toolchain are you programming fro? – fuz Jan 21 '21 at 23:03
  • @gcc linux x86_64 <-- just updated the tags in the question. – samuelbrody1249 Jan 21 '21 at 23:03
  • 1
    Please [edit] your question and add this detail. That said, it is sufficient to (a) make the function label global using a `.globl` directive (b) set the label's type to `function` (c) declare the label as an external function where you want to call it and of course (d) assemble the assembly file and (e) link it into the final binary. – fuz Jan 21 '21 at 23:04
  • @fuz sure, I've updated the question. Would you want to post an answer with how that would all be done? – samuelbrody1249 Jan 21 '21 at 23:06
  • @fuz: `.type foo, @function` is totally optional. It doesn't really have any effect on linking and running. I would have left that out of a list of minimal steps. – Peter Cordes Jan 21 '21 at 23:08
  • [calling assembly function from c](https://stackoverflow.com/q/13901261) has an x86-64 answer with full source and gcc command. For the C caller, it's exactly like calling a function in another `.c` file, because `.c` and `.s` sources are both equivalent ways to produce a `.o`. [How to call Assembly Functions from C on x86 architecture?](https://stackoverflow.com/q/59176320) is a NASM version of the question. – Peter Cordes Jan 21 '21 at 23:10
  • @PeterCordes It may be required under certain circumstances for correct behaviour when dynamic linking is involved. I'd rather not stress my luck when it's so easy to just have that directive in the file. – fuz Jan 21 '21 at 23:11
  • @fuz: If you want to write an x86-64 canonical answer here, we can reopen this. Many of the duplicates already have the basics correct in the question, and the answer is just a fix to the actual asm. – Peter Cordes Jan 21 '21 at 23:25
  • @PeterCordes Sorry, no answer right now. Maybe another day. However, having a canonical answer might be a good idea. But that should go into more detail and cover all relevant cases. – fuz Jan 21 '21 at 23:32
  • @fuz: No worries, IMO it's covered sufficiently by other Q&As, and there are many examples of how to do the basics. (Including looking at `gcc -S` output from one .c file, for a working program with two C sources.) – Peter Cordes Jan 22 '21 at 00:03
  • @PeterCordes I can post a working example/answer from what Fuz has said if you want to re-open the question. – samuelbrody1249 Jan 22 '21 at 03:55
  • 1
    On 2nd look, posting on [calling assembly function from c](https://stackoverflow.com/q/13901261) would work fine. If you want to take extra time to explain why the different parts are needed, that can work there as an answer to the question title. It already has one x86-64 example, but the function is `void print(void)`, so an example that takes args could demonstrate the calling convention as well. – Peter Cordes Jan 22 '21 at 04:44
  • @PeterCordes thanks for the suggestion. I've posted an answer here to the best of my abilities: https://stackoverflow.com/questions/13901261/calling-assembly-function-from-c/65839661#65839661. Does that look reasonable? – samuelbrody1249 Jan 22 '21 at 05:15

0 Answers0