Search code examples
assemblyx86inline-assembly

How to store in a register the address of an element of an array in asm?


#include <stdio.h> 
void main()

{
//Variables

char string[] = "This is a string";

_asm {
    XOR EAX, EAX
    XOR EBX, EBX
    XOR ECX, ECX

    MOV EAX, [string]

}   

What I'm trying to do is store the memory address of the first element of string in EAX but I get "Operand size conflict" . I'm guessing the syntax for dereference is wrong since both eax and the memory adress should be 32 bits, but I can't find anything about it on stack overflow

this is on visual studio done with _asm in C


Solution

  • MOV EAX, [string] is not going to work because char is 8 bit and eax is 32 bit register just use LEA load effective address:

    LEA EAX, [string]
    

    NOTE: what LEA does is converting var to offset so the compiler see it like this: LEA MOV, [offset point to address]