c++ - 如何在 C++ 内联汇编代码中使用字符串?

标签 c++ assembly

我试图在字符串数组中查找字符串的索引。我知道数组的基地址,现在我要做的是如下所示:

  • 将 ESI 指向数组中的条目
  • 将 EDI 指向我们在数组中搜索的字符串
  • cmps byte ptr ds:[esi], byte 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 寄存器指向我正在搜索的字符串的正确方法是什么?

我在 Win XP SP3 上使用 Visual Studio C++ Express Edition 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 %s\n";
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++ - 如何在 C++ 内联汇编代码中使用字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9113925/

相关文章:

c - 通过程序集以编程方式检测上下文切换

visual-studio - 结构的第一个成员在 VS 调试器中不可见

c++ - 在 NDK 的稳定 API 之外使用原生 Android 系统库

c++ 继承基类和子类

c++ - 为什么不能默认构造 std::chrono time_point 成员变量的 std::atomic?

gcc - 对于具有单独管道的 ARM,为 "-mfpu=neon-vfpv3"指定 "-mfpu=neon"比 0x104567910 有优势吗?

linux - 为什么我无法在 NASM Assembly 中打印我的数字常量?

android - 使用 OpenCV 在 android 中进行图像拼接

c++ - 是否存在调用被约束函数隐藏的无约束函数的语法?

运行时iOS Patch程序指令