VV.
VV.

Reputation: 83

Change an array's value in x86 assembly (embedded in C++)

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

Upvotes: 3

Views: 11971

Answers (3)

Remy Lebeau
Remy Lebeau

Reputation: 596256

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
}

Upvotes: 3

Greg Hewgill
Greg Hewgill

Reputation: 993095

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.

Upvotes: 2

Michael Burr
Michael Burr

Reputation: 340208

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):

Upvotes: 0

Related Questions