0
votes

I'm working in Visual Studio 2012 Professional, i have this code:

 char* array = (char*) malloc(100);
 array[0] = 'H'; array[1] = 'e'; array[2] = 'l'; array[3] = 'l';array[4] = 'o';

 __asm {
    // Missing code here
    }

How can i access, for example, the chars in the array using inline assembly? I tried this but it works only for the first character in the array:

 mov eax, array[0]
 mov al, [eax] // Now al contains the 'H'

Buf if i change the code to "mov eax, array[1]" it doesn't work anymore (access violation). How can i loop trough the array?

EDIT, SOLVED (i can't answer my own question for 8 hours) : I found what was wrong with the code:

mov eax, array[0]
mov al, [eax]

actually works, but the eax value gets changed because i modify the al register that's part of the eax register, so when i put it in a loop the eax value is no longer valid. Changing eax to ebx, for example, makes it work without any problem.

1
You need to load effective address. lea eax, array then increment eax whenever you want just as a pointer. Then access as mov al,[eax] but this may only valid for 32 bit system. - huseyin tugrul buyukisik
@huseyin This unfortunately doesn't work, the final value in the al register is not any of the characters i put in the array. - denidare

1 Answers

0
votes

I found what was wrong with the code:

mov eax, array[0]
mov al, [eax]

actually works, but the eax value gets changed because i modify the al register that's part of the eax register, so when i put it in a loop the eax value is no longer valid. Changing eax to ebx, for example, makes it work without any problem.