我正在研究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
我该怎么办?还有一件事,在函数中我将通过每个字母访问字符串。你在这里提出什么建议?
答案 0 :(得分:2)
这是一个骨架,只计算你可以放置你的代码的字符串的长度。
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
答案 1 :(得分:1)
获取函数框架的一个简单方法是告诉VS输出ASM文件。
怎么做:
Crete一个新的源文件,其中包含一个具有所需原型的空函数
例如int foo(const char* s, int bar) { return *s + bar; }
右键单击源文件,然后选择属性 - > C / C ++ - >输出文件 - >汇编程序输出。 选择一个适合您的值。 构建并查看生成的ASM文件。 生成的asm文件包含一些安全检查,可以通过播放此文件的编译标志来禁用。
答案 2 :(得分:-1)
从C方面: 我会这样做:将字符串作为数组传递,并与数组中的许多字段一起传递。程序会将指向数组最开头的指针写入堆栈,因此如果要检索字符串中的第一个字符,请将其称为
mov eax, [adress of the pointer on stack]