【问题标题】:How to use a String in C++ Inline Assembly code?如何在 C++ 内联汇编代码中使用字符串?
【发布时间】:2012-02-25 04:50:25
【问题描述】:

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

  • 将 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 进行编程。

【问题讨论】:

    标签: c++ assembly


    【解决方案1】:

    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。您可以获得所有的速度,而无需尝试找出变量的存储位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-21
      • 2021-01-19
      • 2015-03-17
      • 2016-05-08
      • 1970-01-01
      相关资源
      最近更新 更多