使用字符串、字符数组、c++和汇编.向程序集传递字符串时出错

Working with strings, char array, c++ and assembly. Issue passing string to assembly

本文关键字:字符串 程序集 出错 字符 数组 c++ 汇编      更新时间:2023-10-16

我正在开发C++和外部asm代码。我们需要使用外部ASM库。问题是,我很难将字符串从c++端传递到asm。我确信我在访问asm端的字符串时犯了一些错误。

我基本上是逐字逐句地阅读文本文件。然后,我想将每个单词传递到ASM端,并对其进行处理以获得一些统计信息。

假设我从文件中检索到一个单词,并将其存储在中

string wordFromFile = "America";
processWord(wordFromFile, wordFromFile.size()) //processFromWord is the ASM side function
;;ASM SIDE
;;The doubt I have (first of all all) is how do I declare the arguments on the ASM SIDE
ProcessWordUpperCase PROC C, wordFile :BYTE, len :DWORD
OR
ProcessWordUpperCase PROC C, buffer :DWORD, len :DWORD  

我该怎么办?还有一件事,在函数中,我将访问每个字母的字符串。你有什么建议?

这里有一个骨架,它只计算字符串的长度,您可以将代码放在其中。

toupper.cpp

extern "C"
{
    int ProcessWordUpperCase(const char *wordFile, int arraySize);
};
int main(int argc, char*arg[])
{
    char t[100];
    int res;
    res = ProcessWordUpperCase(t, sizeof(t));
    std::string s = "myvalue";
    res = ProcessWordUpperCase(s.c_str(), s.length());
    return 0;
}

toupper.asm

.486
.model flat, C
option casemap :none
.code
ProcessWordUpperCase PROC, wordFile:PTR BYTE, arrayLen:DWORD
    LOCAL X:DWORD
    push        ebx  
    push        esi  
    push        edi  
    mov         edi, wordFile
    xor         eax, eax
    dec         edi
    dec         eax
@@StringLoop:
    inc         edi
    inc         eax
    cmp         byte ptr [edi], 0
    jne         @@StringLoop
    ; return length of string
    pop         edi  
    pop         esi  
    pop         ebx  
    ret  
ProcessWordUpperCase ENDP
END

获取函数骨架的一个简单技巧是告诉VS输出ASM文件。

如何做到:

用一个具有所需原型的空函数创建一个新的源文件例如int foo(const char* s, int bar) { return *s + bar; }

右键单击源文件,然后选择"属性"->"C/C++"->"输出文件"->"汇编程序输出"。选择适合您的值。构建并查看生成的ASM文件。生成的asm文件包括一些安全检查,可以通过播放该文件的编译标志来禁用这些检查。

从C端:我会这样做:将字符串作为数组传递,同时传递数组中的多个字段。该程序会将指向数组最开始的指针写入堆栈,因此,如果您想检索字符串中的第一个字符,只需将其称为

mov eax, [adress of the pointer on stack]