dword ptr 使用混乱
在汇编语言中如果我们使用
mov eax, dword ptr[ebx]
then就意味着复制ebx指向的值(ebx包含地址值,而不是实际值,该指令复制地址中的实际值)?
如果我们使用
mov eax, dword ptr[some_variable]
则意味着将变量“some_variable”本身的值复制到eax,而不是复制变量“some_variable”指向的值?
我的理解正确吗?
如果是,我很困惑为什么同一条汇编指令有两种不同的含义 - 在第一种情况下有一个间接级别,但在第二种情况下没有额外的间接级别。
任何意见?
编辑:
并非每个 [] 都不会产生任何影响,例如,指令 xchg 将采取一定程度的间接,加载 edx 指向的值。
找到
完整的源代码可以从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 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这两种情况下,您都要求处理器将值从指定地址移动。 这是一个间接级别。 在第一种情况下,您要求它从指定的寄存器中获取地址。 在第二种情况下,您直接指定偏移量。
x86 处理器不支持双层间接寻址,因此不可能请求从内存中某处指定的地址加载值 - 您必须将该地址加载到寄存器中。
在许多汇编器(例如 MASM 和内置于 VC++ 汇编器)下,您也可以只写
不带括号,这意味着相同的。
您可以
这样编写,指示获取“变量”的地址,然后添加 ebx 的值并使用总和作为从中加载值的地址。 这通常用于通过索引访问数组元素。 (支持语法的变体,例如常用的 mov eax, dword ptr variable[ebx] 和 mov eax, dword ptr [variable + ebx] 也很常见。 )
在所有这些情况下,处理器都会执行相同的操作 - 从指定地址加载值。 每次都是一层间接。
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
without brackets, it would mean the same.
You could write
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 andmov 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.
我的理解,汇编中的括号(
[]
)是指针
,所以这条指令:mov eax, dword ptr[some_variable]
它从<复制值code>some_variable 指向
eax
,对于此示例:mov eax, dword ptr some_variable
相同...但是? 我猜它受到
dword
的影响。 如果您感兴趣,您可以从头开始编写,例如使用 NASM 语法:mov eax, dword [some_variable]
My understanding, brackets (
[]
) in assembly ispointer
so this instruction :mov eax, dword ptr[some_variable]
It copy value from
some_variable
pointed toeax
and for this example :mov eax, dword ptr some_variable
It same... but ? I guess it affected by
dword
. If you interested, you can write from scratch this is for example using NASM syntax :mov eax, dword [some_variable]