如何在C++内联汇编代码中使用字符串
How to use a String in C++ Inline Assembly code?
我正在尝试在字符串数组中查找字符串的索引。我知道数组的基本地址,现在我想做的事情如下所示:
- 将ESI指向数组中的条目
- 将EDI指向我们在数组中搜索的字符串
- cmps字节ptr ds:[esi],字节ptr es:[edi],用于在esi和edi时一次比较一个字节
然而,我很困惑如何将EDI寄存器指向我正在搜索的字符串?
int main(int argc, char *argv[])
{
char entry[]="apple";
__asm
{
mov esi, entry
mov edi, [ebx] //ebx has base address of the array
等等
那么,将我的esi寄存器指向我正在搜索的字符串的正确方法是什么呢?
我正在WinXPSP3上使用Visual Studio C++速成版2010进行编程。
Visual C++编译器允许您直接在汇编代码中使用变量。此处的示例:http://msdn.microsoft.com/en-us/library/y8b57x4b(v=vs.80).aspx
// InlineAssembler_Calling_C_Functions_in_Inline_Assembly.cpp
// processor: x86
#include <stdio.h>
char format[] = "%s %sn";
char hello[] = "Hello";
char world[] = "world";
int main( void )
{
__asm
{
mov eax, offset world
push eax
mov eax, offset hello
push eax
mov eax, offset format
push eax
call printf
//clean up the stack so that main can exit cleanly
//use the unused register ebx to do the cleanup
pop ebx
pop ebx
pop ebx
}
}
没有比这更容易的了,IMO。你可以获得所有的速度,而不必费力地找出变量存储在哪里。
相关文章:
- 编译包含字符串的代码时遇到问题
- 为什么这个 c++ 代码打印出长度 5,当我打印出字符串时,程序会自动终止?
- 我应该如何修改此代码以使用给定字符串中的字母打印菱形图案
- 我无法在 qt 的C++代码中定义字符串
- 从 C 样式字符串中删除子字符串 "in place" 在C++代码中
- 有人可以解释一下这段代码如何能够反转字符串
- 如何在 c++ 非托管代码中反序列化 byte[] 的 json 字符串?
- 我遇到了这个代码片段,不明白. 它递归检查 C++ 字符串中是否存在大写字符
- 如何用符合C++核心准则的代码替换C风格的字符串解析
- C++/CLI访问字符串::来自非CLI代码的格式
- MSVC UTF8字符串编码使用了不正确的代码点
- C++指向对象的指针堆数组中,如何将字符串传递给特定对象?有一些代码请看一下:
- 将浮点数转换为字符串时如何加快此代码的速度?
- C++代码,用于在用户插入的字符串的每个元音之后插入一对字符
- 如何获取包含 NULL 字符的外壳代码字符串的长度
- 为什么 for 循环只接受这个简单代码中的最后一个字符串?
- 返回所有代码 - 字符串
- 我能做些什么来加速这个代码(字符串相似性)
- 不知道该为函数的最后一个代码字符串写什么
- 可以将代码字符串传递到c++宏中吗?