将一个字符串从Assembly x64传递给c ++

时间:2016-09-27 19:13:51

标签: c++ pointers visual-c++ assembly x86-64

我有这个c ++程序:

#include <iostream>
#include <conio.h>

using namespace std;

extern "C" int GetAsmX64();

extern "C" void PrintInt(int a) {
    cout << "[ASM]: " << a << endl;
}

extern "C" void PrintString(void* str) {
    char* string = (char*)str;
    cout << str << endl;
}

int main() {
    cout << "x64 says: " << GetAsmX64() << endl;
    _getch();   //stop screen from closing
    return 0;
}

使用此asm文件:

extrn PrintInt:proc     ;PrintInt func
extrn PrintString:proc  ;PrintString func

.data

string db 'Hallo string from asm here'

.code

_print_int macro value
    sub rsp, 20h        ;reserve shadow space on the stack
    mov ecx, value      ;first and only parameter
    call PrintInt       ;call function
    add rsp, 20h        ;restore stack
endm

GetAsmX64 proc
    _print_int 111

    sub rsp, 20h        ;reserve shadow space on the stack
    mov cl, string      ;first and only parameter
    call PrintString    ;call function
    add rsp, 20h        ;restore stack

    mov eax, 0          ;return 0 to c++
    ret
GetAsmX64 endp
end

所以主要开始并调用asm过程。 这很好用。 我有一个PrintInt函数,可以从asm调用以在控制台中打印一个整数。这也有效。我在asm文件中创建了一个宏,以便于调用该函数。现在我想从asm传递一个字符串到c ++函数来打印它,就像我可以打印整数。 然而,这会引起问题。首先我试过:

extern "C" void PrintString(char* str) {
    cout << str << endl;
}

但这会引发大量错误。 有一个void * str确实有效:

extern "C" void PrintString(void* str) {
    cout << str << endl;
}

它会打印一个地址(字符串?????) 虽然施法会产生更多错误:

extern "C" void PrintString(void* str) {
    char* string = (char*)str;
    cout << str << endl;
}

所以问题是,如何将字符串从asm传递到c ++方法作为char *以使用cout打印它?

提前致谢,

科迪

0 个答案:

没有答案