Reputation: 668
Working on calling a C function from my asm project.
I'm trying to push the integer value into the c function.
My code
mov rdi, [input]
push rdi ;push rdi into stack for c function to work with
call dtoch
pop rdi ;to keep stack balanced
' am I moving the input into the wrong register ?
Running on linux Ubuntu OS, not getting an error, it's just not printing out the correct value.
When I run the function in a C environement it works fine, but with my nasm project it prints out wrong numbers....?
c function:
void dtoch(int d )
{
int n , r[10], i = 0, number, j = 0;
while (number > 0)
{
r[i] = number%16;
number = number/16;
i++;
j++;
}
printf ("It's hexadecimal equivalent is: ");
for (i = j -1; i > = 0; i--)
{
if (r[i] == 10)
printf("A");
else if (r[i] == 11)
printf("B");
else if (r[i] == 12)
printf("C");
else if (r[i] == 13)
printf("D");
else if (r[i] == 14)
printf("E");
else if (r[i] == 15)
printf("F");
else
printf("%d", r[i]);
}
}
Upvotes: 2
Views: 1711
Reputation: 26171
x64 uses __fastcall
only, for Linux, the AMD64 ABI is used, see Agner fog's optimization manuals for the differences or this nice summery table on wikipedia (on windows, see this (Microsoft ABI)).
For your example running under Linux , you want it in RDI
, no PUSH
s(and the paired POP
's) are needed. You can review the full AMD64 ABI here, it includes a few coding smaples (on windows you'd want your single arg to go into RCX
, again, no PUSH
's needed.).
A a simple tip: you can compile your c calls with assembly output and see what code the compiler generates, the options -Wa,-adhln -g
on GCC should do the trick.
Upvotes: 3