views:

213

answers:

3

Hey all

I have a method (C++) that returns a character and takes an array of characters as its parameters.

I'm messing with assembly for the first time and just trying to return the first character of the array in the dl register. Here's what I have so far:

char returnFirstChar(char arrayOfLetters[])
{
 char max;

 __asm
 {
  push eax
      push ebx
       push ecx
     push edx
  mov dl, 0

  mov eax, arrayOfLetters[0]
  xor edx, edx
  mov dl, al

  mov max, dl       
  pop edx
  pop ecx
  pop ebx
  pop eax

 }

 return max;
}

For some reason this method returns a ♀

Any idea whats going on? Thanks

+4  A: 

The line of assembly:

mov eax, arrayOfLetters[0]

is moving a pointer to the array of characters into eax (note, that's not what arrayOfLetters[0] would do in C, but assembly isn't C).

You'll need to add the following right after it to make your little bit of assembly work:

mov al, [eax]
Michael Burr
A: 

Oh, and just a note, it should return a 'c' in this case, instead of the female sign

Mark V.
In the future note that its best to edit your question rather than answer it to add more information.
Goz
+2  A: 

Well here is how I'd write that function:

char returnFirstChar( const char arrayOfLetters[] )
{
    char max;
    __asm
    {
         mov eax, arrayOfLetters ; Move the pointer value of arrayOfLetters into eax.
         mov dl, byte ptr [eax]  ; De-reference the pointer and move the byte into eax.
         mov max, dl             ; Move the value in dl into max.
    }
    return max;
}

That seems to work perfectly.

Notes:

1) As I said in my comment you don't need to push the registers on the stack, let MSVC handle that.
2) Don't bother clearing edx by X'oring it against it self or don't set dl to 0. Both will achieve the same thing. All in you don't even need to do that as you can just overwrite the value stored in dl with your value.

Goz
+1: very easy to understand.
dboarman