Skip to content

Instantly share code, notes, and snippets.

Last active Apr 11, 2022
What would you like to do?
Calling c function from x86 assembly code (cdecl)
#include <stdio.h>
int add_print(int a, int b) {
int c = a + b;
printf("%d + %d = %d\n", a, b, c);
return c;
global main
extern add_print
section .text
; int eax = add_print(1, 2); // => 3
push dword 2
push dword 1
call add_print
add esp, 8
; add_print(2, eax); // => 5
push dword eax
push dword 1
call add_print
add esp, 8
; return 0;
mov eax, 0
OBJECTS = func.o main.o
CC = gcc
CFLAGS = -std=c11 -m32 -Wall -Wextra -Werror -c
AS = nasm
ASFLAGS = -f elf
all: $(OBJECTS)
gcc -m32 $(OBJECTS) -o main
run: all
%.o: %.c
$(CC) $(CFLAGS) $< -o $@
%.o: %.s
$(AS) $(ASFLAGS) $< -o $@
rm -rf $(OBJECTS) main
Copy link

AnySharps commented Dec 9, 2018

Bash command?(

Copy link

udexon commented Dec 12, 2018

@JazzHoneyPerson make

Copy link

nipun5 commented Jan 4, 2020

how to Calling x86 assembly function from c code ?

Copy link

KilroyWasHere-cs-j commented Apr 11, 2022

It should be noted that if you're using an x64 change "-m32" in the Makefile to "-m64". It didn't work for me without doing that.

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