x86-64 Linux example
There is already an answer here which shows how to call a void func(void), but here is an x86-64 Linux example that accepts parameters and has a return value, which was what was asked in the question. (The question and some other answers are using 32-bit code, which has a different calling convention).
To start off, let's simplify the assembly function:
# Need to make it global so it can be accessed in another file with extern .globl integrate # Cannot hurt to define it as a function type, sometimes useful for dynamic linking, see comments in: https://stackoverflow.com/questions/65837016/how-to-call-a-function-in-an-external-assembly-file#comment116408928_65837016 .type integrate, @function integrate: # int integrate(int from /*EDI*/, int to /*ESI*/) # INPUT: # the first parameter `from` is contained in %edi, the int-sized low half of %rdi # the second parameter `to` is contained in %esi # OUTPUT: # return is passed in %eax; # you can leave garbage in the high half of RAX if convenient lea 123(%rdi, %rsi), %ecx # from + to + 123 just for example # (main work of function done) mov %ecx, %eax # it seems your return value is in %ecx # but we need it in %eax for the return value to C # or just use EAX instead of ECX in the first place to avoid this instruction ret
This is using the System V calling convention, where the function return value is passed back in rax and the parameters that the function receives are passed in rdi, rsi, rdx, rcx, r8, r9, then the stack in reverse order. (What are the calling conventions for UNIX & Linux system calls (and user-space functions) on i386 and x86-64). For example:
long add_four_nums(int first, long second, short third, unsigned fourth);
The function declared with this prototype would receive first in %edi, second in %rsi, third in %dx, and fourth in %ecx. It would return its result in %rax.
Now that we have the assembly written (though the function is mainly a stub to show how to accept arguments and return a value), you can use that function in your C file like you currently have:
#include <stdio.h> extern int integrate(int from,int to); int main() { printf("%d\n", integrate(1,10)); }
It can be compiled and linked with gcc, then run, as:
$ gcc -o combined -Wall main.c integrate.s && ./combined