can someone please explain the difference among the two set of instructions (1) and (2) which I expected to be equivalent?

char* name = "john";

__asm MOV ESI,name


As said I though (1) and (2) would produce the same result but this is not so. Aren't name and ESI pointing to the same memory address?? Why then the different result?

Posted on 2003-09-02 15:41:45 by yaa
Nope. Are you using Microsoft Visual C++? It uses MASM syntax. mov esi,name is the same as mov esi,. Thus, esi is loaded with the content of your variable "name". But the other instruction will add the address of "name" to eax and take the byte at that address, which is probably not what you want.
Posted on 2003-09-02 16:13:46 by Sephiroth3
Sephiroth3 you are probably right ... but why then does the use of the square brake operator work with registers as in the following example?

__asm MOV EAX,EBX <- copies what is in EBX
__asm MOV EAX, <- copies the 4 bytes pointed to by what is in EBX

And yes, I'm using Visual C++.

Posted on 2003-09-02 16:29:50 by yaa
I got to understand what is happening although I still don't understand why it is happening:

char* name = "john";

__asm MOV ESI,name

is copying in EBX the byte pointed to by the address resulting from the sum ESI+EAX


is instead copying in EBX the first byte of the address resulting from the sum name+EAX

So in the first case I'm correctly copying what is pointed ... in the second I'm copying a byte of the address resulting from the sum.

Posted on 2003-09-02 16:59:43 by yaa
So can anyone confirm that in MASM mode it is not possible to use the [] operator on a variable to get what is pointed by that variable but that it can be done once the variable address has been loaded into a register????

char* name = "hello world";

__asm MOV EAX, name
__asm MOX EBX, <- loads in EBX the first 4 bytes of the string name


__asm MOX EBX, <- loads in EBX the address in memory of the string name

Is all this correct???

Posted on 2003-09-02 17:07:10 by yaa
The contents of the variable "name" isn't being used, it's address is.

To the compiler name is an alias of "ebp + 10" or "0x40010" or something similar (it depends on whether it's a global, or local / fn argument, which of the two it'll be).

"mov ESI, name" == "mov ESI, " == "mov ESI, " == "copy what name points to, to the register ESI".

So here you copy what name points to into ESI, then add EAX to what name pointed to.

"movsx EBX, " == "movsx EBX, " == "copy the data that is EAX bytes further on from where name is pointing into EBX".

Hope this explanation helps a bit.

Posted on 2003-09-02 17:50:06 by Mirno
Not of great help Mirno and this for the reason that the word "points to" can be misinterpreted ...

So summarizing supposing that the variable "name" is at 0x40010 and that the first 4 bytes at that address are 00 04 02 00 (let's not talk of the big endian and little endian issue)

mov ESI, name <- now ESI = 00040200
mov EBX, <- now in EBX we copy the 4 bytes STARTING from address 00040200

while the following two lines do the SAME EXACT thing and that is to copy in EBX 00 04 02 00 just as we did before with ESI

mov EBX, <- now EBX = 00040200
mov EBX, name <- now EBX = 00040200

Posted on 2003-09-02 18:37:02 by yaa
try MOV EBX, DWORD PTR for the data at address name - similar to MOV EBX, .
or MOV EBX, OFFSET NAME for address of name - similar to MOV EBX, ESI.

brackets are optional in either case!
this forces MASM to give you what you want.
Posted on 2003-09-02 21:03:00 by bitRAKE