Accessing an item in an array with an assembly
Suppose the following function:
void asmFunction(const int *first, ...) {
__asm {
xor eax, eax
add eax, [first][0]
; ...
}
}
It's called like this:
int first[] = { 0, 0, 0, 0, 0, 0, 0, 5, 7, 6, 2, 4, 3, 5 };
asmFunction2(first, ...);
As far as I understand, the second assembly line should add an 0
array element first
to eax
. However, a random value is added. When debugging first[0]
is 0, as it should be. What's the problem with the code?
I am code in Visual Studio 2013 on a 64 bit machine.
source to share
It's weird syntax and probably doesn't do what you want. If you break down the generated code, you will most likely see something like add eax, [ebp+8]
. The random appended value must be a value first
that is a pointer. You really did eax += first
. To get the items, you need a level of indirection, that is eax += *first
. For example, this might work:
mov edx, [first]
add eax, [edx]
source to share