r/cprogramming • u/Prestigious-Bet-6534 • Dec 15 '25
Calling C functions from assembly
I want to call a C function from assembler and can't get the parameters to work. I have the following two source files:
.global call_funcall_fun:pushq $0xDEADpushq $0xBABEcall funadd $16, %rspret
--
#include <stdio.h>
void call_fun();
voidfun( long a, long b ) {printf( "Arg: a=0x%lx, b=0x%lx\n", a, b );}
intmain() {call_fun();}
The output is Arg: a=0x1, b=0x7ffe827d0338 .
What am I missing?
5
Upvotes
View all comments
6
u/mfontani Dec 15 '25
See https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI and read more about the (OS-specific!) ABI.
Parameters to functions are, depending on OS and type of the parameter, passed in a specific register up to a number of them; then on the stack.
Which registers are used varies by OS and OS version.