views:

171

answers:

3

I am messing around with assembly for the first time, and can't seem to change the index values of an array. Here's the method I am working on

int ascending_sort( char arrayOfLetters[], int arraySize )
 {
   char temp;

__asm
    {

   //???
      }
}

And these are what I tried

mov temp, 'X'
mov al, temp
mov arrayOfLetters[0], al

And this gave me an error C2415: improper operand type

so I tried

mov temp, 'X'
mov al, temp
mov BYTE PTR arrayOfLetters[0], al

This complied, but it didn't change the array...

A: 

This SO question deals with reading the elements of an array instead of modifying them, but I suspect that the underlying explanation will be largely the same (namely that arrayOfLetters needs to be treated as a pointer):

Michael Burr
A: 

The arrayOfLetters value is equivalent to a pointer. So, your assembly code might need to be:

mov temp, 'X' 
mov al, temp 
mov edx, arrayOfLetters
mov [edx], al 

In the above code, edx is loaded with the address of arrayOfLetters. Then the last instruction stores the al byte into the address pointed to by edx.

Greg Hewgill
+1  A: 

When you have a parameter or varaible that is an array, it is actually a pointer to the first element of the array. You have to deference that pointer in order to change the data that it points to. For example:

__asm
{
mov eax, arrayOfLetter
mov [eax], 0x58
}

Or, more generically:

__asm
{
mov eax, arrayOfLetter
mov [eax+index], 0x58
}
Remy Lebeau - TeamB