DWORD PTR用法混乱 [英] dword ptr usage confusion

查看:339
本文介绍了DWORD PTR用法混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在汇编语言中,如果我们用

  MOV EAX,DWORD PTR [EBX]

那么就意味着通过复制EBX指向的值(EBX包含地址值,而不是实际值,这个指令将地址的实际值)?

如果我们用

  MOV EAX,DWORD PTR [some_variable]

那么就意味着复制变量的值some_variable本身EAX,不能照搬通过指出变量的值some_variable?

我的理解是否正确?

如果是的,我很困惑为什么同样的汇编指令有两个不同的meansings - 在第一种情况有一个间接的水平,但在第二次有间接的其他级别

有何评论?

编辑:

并非每个[]不采取任何效果,例如,指令XCHG将采取的方向上的电平,它加载由EDX指出值

全部源$ C ​​$ C能从中找到

<一个href=\"http://www.$c$cproject.com/KB/threads/spinlocks.aspx\">http://www.$c$cproject.com/KB/threads/spinlocks.aspx

 的#ifdef WIN32
内联INT CPP_SpinLock ::检查并设置为(int *的targetAddress,整数n值)
{
    __asm​​ {
        MOV EDX,DWORD PTR [pTargetAddress]
        MOV EAX,n值
        锁定XCHG EAX,DWORD PTR [EDX]
    }
}
#ENDIF // WIN32


解决方案

在你问的处理器值从指定地址移到这两种情况。它是一个间接层。在第一种情况下,你问它从一个指定的寄存器取地址。在第二种情况下,你指定一个直接偏移。

x86处理器不支持双级间接,因此它不可能要求从内存中某个指定的地址值加载 - 你要的地址加载到寄存器

在一些组装机(MASM和内置VC ++汇编程序为例),你可以和刚写

  MOV EAX,DWORD PTR some_variable

没有括号,这将意味着相同的。

您可以写

 动EAX,DWORD PTR [变量] [EBX]

这会指示采取变量的地址,再加入EBX价值和使用的总和从中加载值的地址。这通常用于通过索引访问数组元素

在所有这些情况下,处理器会做同样的 - 从指定的地址装载值。这是每次一个间接层。

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

解决方案

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

move 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.

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.

这篇关于DWORD PTR用法混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆