1
votes

I am working on this for class, and as per the instructors guidelines we have to do the program using inline c++. The purpose of the program is to take a string of any length and reverse it. The error I'm getting is an operand size conflict and from what I can tell it's in the first line of the __asm block, there could be other issues but the only one that shows up in visual studio is the conflict. Here is my asm block

int _tmain(int argc, _TCHAR* argv[])
{
   char string[] = "Hi There!";
   __asm
   {    // reverse a string of any length
    lea ecx, string
    lea eax, string
    mov esi, eax  // esi points to start of string
    add eax, ecx
    mov edi, eax
    dec edi       // edi points to end of string
    shr ecx, 1    // ecx is count (length/2)
    jz done       // if string is 0 or 1 characters long, done
    reverseLoop:
    mov al, [esi] // load characters
    mov bl, [edi]
    mov [esi], bl // and swap
    mov [edi], al
    inc esi       // adjust pointers
    dec edi
    dec ecx       // and loop
    jnz reverseLoop
    done:
   }

   printf(string);

   return 0;
  }

I made the changes now I am getting this: Unhandled exception at 0x00e71416 in String Reverse.exe: 0xC0000005: Access violation reading location 0x0087ef6f. Based on other suggestions I have tried I have still not be able to get it to run properly. I think the issue might be in the registers I'm referencing or the add eax line, but I'm not really sure.

1

1 Answers

2
votes
  mov ecx, [string]

"string" is an array of char, you are trying to move 8 bits into a 32-bit register. If is was a global variable you'd use the offset keyword. But it is not, it is stored on the stack. Which requires you to use the LEA instruction (load effective address), like this:

  lea ecx,string

which the compiler automatically translates into something like:

  lea ecx,[ebp-20] 

with the -20 adjustment depending on where it is located on the stack. The ECX register now points to the first char in the string.