Skip to content

Instantly share code, notes, and snippets.

@hibariya

hibariya/Makefile

Last active Jun 14, 2020
Embed
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
main:
; 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
ret
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
./main
%.o: %.c
$(CC) $(CFLAGS) $< -o $@
%.o: %.s
$(AS) $(ASFLAGS) $< -o $@
clean:
rm -rf $(OBJECTS) main
@JazzHoneyPerson

This comment has been minimized.

Copy link

@JazzHoneyPerson JazzHoneyPerson commented Dec 9, 2018

Bash command?(

@udexon

This comment has been minimized.

Copy link

@udexon udexon commented Dec 12, 2018

@nipun5

This comment has been minimized.

Copy link

@nipun5 nipun5 commented Jan 4, 2020

how to Calling x86 assembly function from c code ?

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.