简体   繁体   中英

Getting parameter in ASM from C call

I call an asm function from a C program, then I try to call another C function and give new parameters, but the second function received the parameters of asm. Here are the 3 files.

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;
}

File 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;
 }

File 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]

When I execute, I get this output:

In aff(115,9)
From pusher : 'v = 115 - 9'

But I was expecting

In aff(123,321)
From pusher : 'v = 123 - 321'

How do I pass my own new parameters? The final goal is to take in _pusher a char ** and call aff like this:

_aff(char *param,...) and use VA_LIST inside so pusher should be call like this
_pusher(int count,char **tbl) 

The OP noticed that the parameters he was passing to his assembly-language function weren't being processed.

@Olaf noted that the OP wasn't using the correct ABI (Application Binary Interface - the definition of how to call functions on a platform) for his environment - he was using a (much) older one described in Wikipedia.

@Michael Petch clarified by stating that for x64 development, instead of PUSH ing the values onto the stack (an x86 convention), he should load them into the first two expected registers: RDI and RSI

I just realise I forgot to post the working code, after 6 month oops. so the asm function will receive a char ** and will formate it to send to redis.

hope this code will be usefull for others.

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

then _pusher will call 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM