2012-02-02 87 views
2

我正在试图在字符串数组中找到一个字符串的索引。我知道数组的基址,现在我想要做的是如下这样的:如何在C++内联汇编代码中使用字符串?

  • 点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。

回答

5

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 
    } 
} 

它没有得到任何比这更容易,海事组织。您将获得所有的速度,而无需试图找出变量存储在哪里的麻烦。