Search code examples
cfunctionassemblynasmyasm

C calling ASM (YASM x86)


I want to call a ASM function in a c code, How do I pass the parameters to the ASM code?

#include <stdio.h>

extern int * asm_mod_array(int *ptr,int size);
 int main()
 {
int fren[5]={1,2,3,4,5};

/*Call ASM func*/
int a=asm_mod_array(fren,5);
printf(u,a);
return 0;   
 }

now, i want to use this parameters in my ASM function.

;asm_mod_array(int ptr,int size)


global asm_mod_array

asm_mod_array:
push r12
mov rdi, 0
mov rsi, 0

mov r12,0
mov rax,0

sumLoop:

add rax, [rdi+r12]
inc r12
cmp r12, rsi
jl sumLoop

mov [rdx], rax
pop r12
ret

NOTE: in the ASM code the 0, have to be changed with the parameters passed by c.


Solution

  • You can access the arguments according to the AA64 calling conventions for your platform. On most systems, except Windows, this is defined by the System V AMD64 ABI.

    By these calling conventions, ptr will be in rdi and size will be in rsi. The return value is placed in rax.

    See X86 calling conventions.