dword ptr 使用混乱

发布于 2024-07-15 07:48:57 字数 875 浏览 5 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丑疤怪 2024-07-22 07:48:57

在这两种情况下,您都要求处理器将值从指定地址移动。 这是一个间接级别。 在第一种情况下,您要求它从指定的寄存器中获取地址。 在第二种情况下,您直接指定偏移量。

x86 处理器不支持双层间接寻址,因此不可能请求从内存中某处指定的地址加载值 - 您必须将该地址加载到寄存器中。

在许多汇编器(例如 MASM 和内置于 VC++ 汇编器)下,您也可以只写

mov eax, dword ptr some_variable

不带括号,这意味着相同的。

您可以

mov eax, dword ptr [variable][ebx]

这样编写,指示获取“变量”的地址,然后添加 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

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.

明天过后 2024-07-22 07:48:57

我的理解,汇编中的括号([])是指针,所以这条指令:

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 is pointer so this instruction :

mov eax, dword ptr[some_variable]

It copy value from some_variable pointed to eax 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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文