Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calling c function from x86-64 assembly code (System V AMD64 ABI)
#include <stdio.h>
void take_many(int a, int b, int c, int d, int e, int f, int g, int h) {
printf("%d, %d, %d, %d, %d, %d, %d, %d", a, b, c, d, e, f, g, h);
}
global main
extern take_many
bits 64
section .text
main:
; take_many(1, 2, 3, 4, 5, 6, 7, 8);
push 8
push 7
mov r9, 6
mov r8, 5
mov rcx, 4
mov rdx, 3
mov rsi, 2
mov rdi, 1
call take_many
add rsp, 16
; return 0;
mov rax, 0
ret
OBJECTS = func.o main.o
CC = gcc
CFLAGS = -std=c11 -m64 -Wall -Wextra -Werror -c
AS = nasm
ASFLAGS = -f elf64
all: $(OBJECTS)
gcc -m64 $(OBJECTS) -o main
run: all
./main
%.o: %.c
$(CC) $(CFLAGS) $< -o $@
%.o: %.s
$(AS) $(ASFLAGS) $< -o $@
clean:
rm -rf $(OBJECTS) main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.