blob: 41e50e8a06115b07bc13255b7e2defba368a7d28 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
---
title: "Calling assembly functions from C"
url: calling-assembly-functions-from-c.html
date: 2024-06-17T16:12:13+02:00
type: note
draft: false
---
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.
```asm
# 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.
```c
// 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`.
|