Created
September 7, 2018 04:59
-
-
Save bmcculley/ad41cb557f7325a2a4c4c3e8122ab6d6 to your computer and use it in GitHub Desktop.
Calling a c function from assembly example.
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
int hello() { | |
return 123; | |
} |
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
extern hello | |
extern printf | |
extern exit | |
global _start | |
section .data | |
fmt: db "%d", 10, 0 | |
section .text | |
_start: | |
call hello ; hello() | |
mov rsi,rax | |
mov rdi,fmt | |
call printf ; printf(format, rsi) | |
push 0 | |
call exit ; exit(0) |
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
all: | |
gcc -Wall -c hello.c | |
nasm -f elf64 main.asm | |
ld main.o hello.o -lc -I /lib64/ld-linux-x86-64.so.2 | |
clean: | |
rm a.out hello.o main.o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment