Created
June 19, 2016 06:17
-
-
Save hibariya/3bd642fc983cab1421c43a847b59ea31 to your computer and use it in GitHub Desktop.
Calling c function from x86-64 assembly code (System V AMD64 ABI)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
it's good thanks for help me