【发布时间】:2016-05-26 17:45:32
【问题描述】:
我从一个 C 程序调用一个 asm 函数,然后我尝试调用另一个 C 函数并提供新参数,但第二个函数接收到 asm 的参数。这是 3 个文件。
main.c
#include <stdio.h>
#include <stdlib.h>
#include "ac.h"
int main (int argc,char **argv)
{
char * s = _pusher(115,9);
fprintf(stdout,"From pusher '%s'\n",s);
free(s);
return EXIT_SUCESS;
}
文件 ac.c
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include "ac.h"
char * _aff(int a,int b)
{
char * s;
fprintf(stdout,"In aff(%d,%d)\n",a,b);
asprintf(&s,"v = %d - %d",a,b);
return s;
}
文件 aa.asm
[CPU x64]
[BITS 64]
extern _aff
global _pusher
[SECTION .text]
_pusher:
push rbp
mov rbp,rsp
push 123 ;seems no effect
push 321 ;seems no effect
call _aff
add rsp,24
mov rsp,rbp
pop rbp
ret
[SECTION .data]
[SECTION .bss]
当我执行时,我得到这个输出:
In aff(115,9)
From pusher : 'v = 115 - 9'
但我期待
In aff(123,321)
From pusher : 'v = 123 - 321'
如何传递我自己的新参数?最终目标是接收_pusher a char ** 并像这样调用aff:
_aff(char *param,...) and use VA_LIST inside so pusher should be call like this
_pusher(int count,char **tbl)
【问题讨论】:
-
读取您平台的 ABI 并按预期传递参数。
-
奇怪的是那些从 main 发送的参数会直接发送到 _aff。我以为 mov rbp,rsp 做了我的新堆栈
-
不要只是重复你在问题中已经阐明的内容。如果您不理解我的评论,请搜索“ABI”。
-
我不理解您的评论,而不是因为 ABI。我正在使用 Fedora 23 x64,我读了这个来写代码en.wikipedia.org/wiki/X86_calling_conventions
-
64-bit System V Linux calling convention 在寄存器(而不是堆栈)中传递前 6 个参数(可以用 8 个字节表示)。 RDI 和 RSI 是前两个参数。