Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dword ptr usage confusion

In assembly language if we use

mov eax, dword ptr[ebx]

then it means copy the value pointed by ebx (ebx contains the address value, not the actual value, this instruction copies the actual value in the address)?

If we use

mov eax, dword ptr[some_variable]

then it means copy the value of variable "some_variable" itself to eax, not copy the value pointed by variable "some_variable"?

Is my understanding correct?

If yes, I'm confused why the same assembly instruction has two different meansings - in the first case there is a level of indirection, but in the second there is no additional level of indirection.

Any comment?

EDIT:

Not every [] does not taking any effect, for example, instruction xchg will take a level of in-direction, which loads value pointed by edx.

Whole source code could be found from,

http://www.codeproject.com/KB/threads/spinlocks.aspx

#ifdef WIN32
inline int CPP_SpinLock::TestAndSet(int* targetAddress, int nValue)
{
    __asm {
        mov edx, dword ptr [pTargetAddress]
        mov eax, nValue
        lock xchg eax, dword ptr [edx]
    }
}
#endif // WIN32
like image 526
George2 Avatar asked Mar 27 '09 07:03

George2


People also ask

What does dword ptr mean?

Basically, it means "the size of the target operand is 32 bits", so this will bitwise-AND the 32-bit value at the address computed by taking the contents of the ebp register and subtracting four with 0. Follow this answer to receive notifications.

What is dword in ASM?

DWORD defines 'size' of the memory location used for move operation. In you example, you'd be moving 0000000Ah (4 bytes) into memory location ESP+18h. As 0Ah is immediate value its size cannot be determined without using DWORD , WORD , BYTE or other similar qualifier.


1 Answers

In both cases you ask the processor to move the value from a specified address. It's one level of indirection. In the first case you ask it to take the address from a specified register. In the second case you specify an offset directly.

x86 processors don't support dual level indirection, so it's not possible to request to load a value from an address specified somewhere in memory - you have to load the address onto a register.

Under a number of assemblers (MASM and built into VC++ assembler for example) you could as well write just

mov eax, dword ptr some_variable

without brackets, it would mean the same.

You could write

mov eax, dword ptr [variable][ebx]

this would instruct to take the address of "variable", then add value of ebx and use the sum as an address from which to load a value. This is often used for accessing array elements by index. (Variations on the syntax are supported, like mov eax, dword ptr variable[ebx] being commonly used and mov eax, dword ptr [variable + ebx] also being common.)

In all these cases the processor would do the same - load a value from a specified address. It's one level of indirection each time.

like image 103
sharptooth Avatar answered Sep 27 '22 20:09

sharptooth