从C调用获取ASM中的参数

时间:2016-05-26 17:45:33

标签: c nasm

我从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) 

2 个答案:

答案 0 :(得分:1)

OP注意到他传递给汇编语言函数的参数没有被处理。

@Olaf注意到OP没有使用正确的ABI(应用程序二进制接口 - 如何在平台上调用函数的定义)用于他的环境 - 他正在使用维基百科中描述的(更多)旧版本

@Michael Petch通过声明对于x64开发而不是PUSH将值放入堆栈(x86约定)而澄清,他应该将它们加载到前两个预期的寄存器中:RDIRSI

答案 1 :(得分:0)

我刚刚意识到我忘了发布工作代码,经过6个月的哎呀。 所以asm函数将收到一个char **并将其合成以发送给redis。

希望这段代码对其他人有用。

redisReply * _pusher(redisContext *c, int count, char **args); 

然后_pusher将调用redisCommand(c,“%s%s ...”,args [0],...,args [count-1]):

[CPU x64]                                                             
[BITS                                                          
extern redisCommand
global _pusher   

[SECTION.text]                                                                
_pusher:
push rbp
mov rbp,rsp

mov rax,rsi
cmp rax,1  
je arg_1

cmp rax,2
je arg_2 

cmp rax,3
je arg_3 

cmp rax,4
je arg_4

cmp rax,5
jge arg_more 

arg_1:                                                         
sub rsp,0x3
mov byte [rbp - 0x3],0x25
mov byte [rbp - 0x2],0x73                                     
mov byte [rbp - 0x1],0x0                                     

mov rax,rdi                                                 
mov rdi,rdx                                                 
mov rdx,[rdi]                                                 
lea rsi,[rbp - 0x3]                                      
mov rdi,rax                                                 
jmp debut_appel                                                 

arg_2:                                                         
sub rsp,0x6                                                 
mov byte [rbp - 0x6],0x25                                     
mov byte [rbp - 0x5],0x73                                     
mov byte [rbp - 0x4],0x20                                     
mov byte [rbp - 0x3],0x25                                     
mov byte [rbp - 0x2],0x73                                     
mov byte [rbp - 0x1],0x0                                     

mov rax,rdi                                                 
mov rdi,rdx                                                 
mov rdx,[rdi]                                                 
mov rcx,[rdi + 0x8]                                      
lea rsi,[rbp - 0x6]                                      
mov rdi,rax                                                 
jmp debut_appel 

arg_3:
sub rsp,0x9
mov byte [rbp - 0x9],0x25
mov byte [rbp - 0x8],0x73
mov byte [rbp - 0x7],0x20
mov byte [rbp - 0x6],0x25
mov byte [rbp - 0x5],0x73
mov byte [rbp - 0x4],0x20
mov byte [rbp - 0x3],0x25
mov byte [rbp - 0x2],0x73
mov byte [rbp - 0x1],0x0

mov rax,rdi
mov rdi,rdx
mov rdx,[rdi]
mov rcx,[rdi + 0x8]
mov r8, [rdi + 0x10]
lea rsi,[rbp - 0x9]
mov rdi,rax
jmp debut_appel

arg_4:
sub rsp,0xc
mov byte [rbp - 0xc],0x25
mov byte [rbp - 0xb],0x73
mov byte [rbp - 0xa],0x20
mov byte [rbp - 0x9],0x25
mov byte [rbp - 0x8],0x73
mov byte [rbp - 0x7],0x20
mov byte [rbp - 0x6],0x25
mov byte [rbp - 0x5],0x73
mov byte [rbp - 0x4],0x20
mov byte [rbp - 0x3],0x25
mov byte [rbp - 0x2],0x73
mov byte [rbp - 0x1],0x0

mov rax,rdi
mov rdi,rdx
mov rdx,[rdi]
mov rcx,[rdi + 0x8]
mov r8, [rdi + 0x10]
mov r9, [rdi + 0x18]
lea rsi,[rbp - 0xc]
mov rdi,rax
jmp debut_appel

arg_more:
mov r8,rdi                                         
mov rdi,rdx
mov rcx,rax
sub rcx,5
lea rax, [rcx * 3]
add rax, 0xf

sub rsp,rax
mov rdx,rbp
sub rdx,rax
lea rsi,[rdx]

mov byte [rbp - 0xc],0x25
mov byte [rbp - 0xb],0x73
mov byte [rbp - 0xa],0x20
mov byte [rbp - 0x9],0x25
mov byte [rbp - 0x8],0x73
mov byte [rbp - 0x7],0x20
mov byte [rbp - 0x6],0x25
mov byte [rbp - 0x5],0x73
mov byte [rbp - 0x4],0x20
mov byte [rbp - 0x3],0x25
mov byte [rbp - 0x2],0x73
mov byte [rbp - 0x1],0x0


;debut de boucle
loop_start:
    mov byte [rdx],0x25
    mov byte [rdx + 1],0x73
    mov byte [rdx + 2],0x20
    add rdx,0x3

    lea rax , [rcx * 0x8]
    mov rbx,[rdi + rax + 0x20]
    push rbx