linux - Passing argument from C to Assembly -


i'm trying make program in c uses function assembly. below can see code:

sum_c.c

#include <stdio.h>  extern int _assemblysum(int x, int y);  int main(int argc, char *argv[]){     int total;      total = _assemblysum(4, 2);     printf("%d\n", total);     return 0; } 

assembly_sum.asm

section .data  section .text     global _assemblysum  _assemblysum:     push rbp     mov rbp, rsp     mov rax, [rbp+16]     mov rbx, [rbp+24]     add rax, rbx     pop rbp     ret 

compile

nasm -f elf64 assembly_sum.asm -o assembly_sum.o gcc c_sum.c assembly_sum.o -o sum ./sum 

when run program random numbers -1214984584 or 2046906200. know need use registers rdi, rsi, rdx , rcx because 64bit gnu/linux compiler uses them (passing arguments c 64bit linux assembly). how can that?

you may have confused calling convention being used.

linux uses 64-bit system v calling convention. under convention, registers preferred on stack passing of integer type parameters. registers integer passing used in following order:

  • %rdi
  • %rsi
  • %rdx
  • %rcx
  • %r8
  • %r9

if additional integer parameters used, passed on stack.

you can find detailed information in system v abi specification.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo