3

Assume the following function:

void asmFunction(const int *first, ...) {
    __asm {
        xor eax, eax
        add eax, [first][0]
        ; ...
    }
}

It is being called this way:

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 must add 0 element of array first to eax. However, a random value is being added. When debugging, first[0] equals 0, as it must be. What is the problem with the code?

I code in Visual Studio 2013 on 64-bit machine.

2
  • I'm not an expert, but shouldn't it be add eax, [first + 0]? With [] you dereference the first pointer/address. And for element 1: add eax, [first + 4], etc. Commented Dec 3, 2014 at 12:33
  • @meaning-matters As far as I know, [first][0] means the same as [first + 0], but I’m not sure. Commented Dec 3, 2014 at 13:26

1 Answer 1

1

That is a strange syntax and probably doesn't do what you want. If you disassemble the generated code you will most likely see something like add eax, [ebp+8]. The random value added should be the value of first, which is a pointer. You have effectively done eax += first. To get at the elements you need a level of indirection, that is eax += *first. For example this could work:

mov edx, [first]
add eax, [edx]
Sign up to request clarification or add additional context in comments.

2 Comments

Interesting. Unfortunately, currently I’m unable to test it. Is the add eax, [[first]] syntax acceptable or wrong?
x86 doesn't support double indirection, so even if some assembler accepts that form, it's not going to work either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.