使用字符串、字符数组、c++和汇编.向程序集传递字符串时出错
Working with strings, char array, c++ and assembly. Issue passing string to assembly
我正在开发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]
相关文章:
- 内联程序集printf将整数解释为地址
- 正在解码MSVC 32位版本的程序集(作业).没有手术做什么
- 具有外部"c"和程序集的未定义函数
- 用于将C++代码转换为 Web 程序集的脚本未终止
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- CoreCLR 中的检测探查器 - 将帮助程序程序集加载到 dotnet 进程的方法
- 不同于按值传递和常量引用传递的程序集
- 为什么在堆栈和堆上创建变量会产生相同的程序集代码?
- C++变量在调用 x64 程序集函数后重置为 0
- 如何将C++子例程链接到 x86 程序集程序?
- Qt 网页程序集缓存
- 测试操作系统时执行程序集"sti"时虚拟框崩溃
- 为什么从 constexpr 引用生成的程序集代码与从 constexpr 指针生成的程序集代码不同?
- 将内联程序集尾调用函数尾声替换为用于x86/x64 msvc的Intrinsics
- 解析 C# 中的C++程序集
- 64 位进程中的 AnyCPU C# DLL 无法引用 64 位C++ DLL(给出错误:无法加载文件或程序集)
- 使用CLANG内联程序集创建C++预增量操作
- 程序集字符串和 C++ 中的等效代码
- visualstudio内联程序集发出字符串宏
- 使用字符串、字符数组、c++和汇编.向程序集传递字符串时出错