Author Topic: Nasm64: Return multiple 64 bit registers to C++ in Windows10  (Read 256 times)

Offline prdas31

  • Jr. Member
  • *
  • Posts: 6
Nasm64: Return multiple 64 bit registers to C++ in Windows10
« on: March 22, 2018, 02:57:40 AM »
Hi,

What I want to know are the following two things:

1) Under fastcall, how can I return multiple values to C++ using registers? (as under fastcall C++ can officially take only RAX I think. And I do not wish to use a struct or class).

In the following trial code I used std::pair just to make my point clearer.

Code: [Select]
#include <iostream>
using namespace std;

extern "C" std::pair<string, string> testCall(char* a, char* b, char**);

int main() {
    char* somebuf =(char*) malloc(256);

    std::pair<string, string>res = testCall( "Hello", "World", &somebuf );

    cout << res.first <<rec.second<<endl;
    cout<<somebuf <<endl;
    return 0;
}

2) If I pass a third argument in my function to get some output value from the assembler, how to load that value/pointer in the assembly routine? (like the somebuf  vars in my example ).

A very simple asm code I tried:

Code: [Select]
segment .text

    global testCall

testCall:
    pushfq
   
    mov rax, rdx

    mov rdx, rcx ; How to get the value of RDX in C++?
   
    mov r8, rdx ; How to send back the R8 register/pointer to &somebuf?

   
    popfq
    ret

Appreciate your help.
Thanks.

Offline prdas31

  • Jr. Member
  • *
  • Posts: 6
Re: Nasm64: Return multiple 64 bit registers to C++ in Windows10
« Reply #1 on: March 23, 2018, 05:30:41 PM »
I have figured out both of these issues.

Thanks, and request to ignore it.

Offline PatrickDes

  • New Member
  • Posts: 1
Re: Nasm64: Return multiple 64 bit registers to C++ in Windows10
« Reply #2 on: April 01, 2018, 02:15:05 PM »
Can you share the solutions with us, Prdas31? I'm really curious and it might help someone else as well.