Skip to content

Instantly share code, notes, and snippets.

@hibariya
Created June 19, 2016 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hibariya/3bd642fc983cab1421c43a847b59ea31 to your computer and use it in GitHub Desktop.
Save hibariya/3bd642fc983cab1421c43a847b59ea31 to your computer and use it in GitHub Desktop.
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
@MafiaBoys
Copy link

it's good thanks for help me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment