Calling assembly functions from C

note, Jun 17, 2024, on Mitja Felicijan's blog

This is using the portable GNU assembler and TinyCC compiler but GCC or Clang can be used as well.

First lets define a simple function in assembly.

# sum.s
.intel_syntax noprefix

.global sum

.text

sum:
    add rdi, rsi
    mov rax, rdi
    ret

Lets compile this with GNU assembler as sum.s -o sum.o.

Now we need a C program that calls this function.

// main.c
#include <stdio.h>

// We need to define the signature of the function.
int sum(int a, int b);

int main() {
    for(int i=0; i<10; ++i) {
        printf("SUM of 5+%d is %d\n", i, sum(5, i));
    }
    return 0;
}

Now lets compile and link into final program with tcc main.c sum.o -o main.



Other notes