Neon Flash
Neon Flash

Reputation: 3233

How to use a String in C++ Inline Assembly code?

I am trying to find the index of a string in an array of Strings. I know the base address of the Array, now what I want to do is something like shown below:

However, I am confused about how to point EDI register to the string I am searching for?

int main(int argc, char *argv[])
{
char entry[]="apple";
__asm
{
mov esi, entry
mov edi, [ebx] //ebx has base address of the array

and so on.

So, what would be the right way to point my esi register to the string that I am searching for?

I am programming in Visual Studio C++ Express Edition 2010 on Win XP SP3.

Upvotes: 2

Views: 5282

Answers (1)

Timothy Baldridge
Timothy Baldridge

Reputation: 10653

The Visual C++ compiler allows you to use variables directly in assembly code. Example from here: https://learn.microsoft.com/en-us/cpp/assembler/inline/calling-c-functions-in-inline-assembly

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

It doesn't get any easier than this, IMO. You get all the speed, without all the hassle of trying to find out where variables are stored.

Upvotes: 6

Related Questions