从 nasm x86-64 调用 c 函数
致力于从我的 asm 项目中调用 C 函数。
我正在尝试将整数值推入 c 函数中。
我的代码
mov rdi, [input]
push rdi ;push rdi into stack for c function to work with
call dtoch
pop rdi ;to keep stack balanced
' 我是否将输入移动到错误的寄存器中?
在 Linux Ubuntu 操作系统上运行,没有收到错误,只是没有打印出正确的值。
当我在 C 环境中运行该函数时,它工作正常,但在我的 nasm 项目中,它打印出错误的数字....?
函数:
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]);
}
}
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]);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
x64 使用
__fastcall
仅,对于 Linux,使用 AMD64 ABI,请参阅 Agner folg 的 优化手册了解差异或这个漂亮的总结表维基百科(在 Windows 上,请参阅 此 (Microsoft ABI))。对于在 Linux 下运行的示例,您希望它在
RDI
中,不需要PUSH
(以及配对的POP
) 。您可以在此处查看完整的 AMD64 ABI,其中包括一些编码示例(在 Windows 上,您希望单个参数进入RCX
,同样,不需要PUSH
。)。一个简单的提示:您可以使用汇编输出编译 c 调用,并查看编译器生成的代码,GCC 上的选项
-Wa,-adhln -g
应该可以解决问题。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
, noPUSH
s(and the pairedPOP
'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 intoRCX
, again, noPUSH
'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.