我是个汇编新手,我正在使用MASM。我看到这些代码行,想知道它们之间的区别是什么
a) push myVar
b) push [myVar]
c) push OFFSET myVar我怎么知道他们是在推送myVar的值还是地址,谢谢!
致以最良好的问候,谢谢
发布于 2013-05-17 16:27:05
push myVar只是将您的var推入堆栈。
push [myVar]正在取消引用您的变量。如果myVar是一个指针,则此代码将把该地址的值推入堆栈。
我不确定最后一个,但它似乎是相反的,push OFFSET myVar正在将myVar的地址压入堆栈。
发布于 2013-05-17 16:48:58
这取决于您使用的汇编程序。对于MASM/TASM,前两个变体执行相同的操作(将myVar的值压入堆栈),而第三个变体将myVar的地址推入堆栈(在分段模式下,这将是当前段内的偏移量)。
换句话说,MASM/TASM假设您希望取消引用变量的有效地址,即使您编写该变量时没有使用括号。如果使用立即值作为地址/操作数,则会有所不同:
pushd 0 ; push the dword value 0 onto the stack
push dword ptr [0] ; push the dword at address 0 onto the stack
; will likely crash your program与寄存器操作数类似:
push eax ; push the value of eax onto the stack
push dword ptr [eax] ; push the value at the address that eax points to使用NASM,您可以将前两个变体编写为
push dword [myVar] ; assuming a dword variable和第三个as
push myVar发布于 2014-02-08 20:10:34
假设我们像这样声明了我们的data段:
.data
myVar DWORD 1, 5, 9不带的
现在,让我们将其放入code部分:
.code
mov eax, myVar如果我们现在看一下寄存器(例如,通过调试器),我们可以看到如下所示:
EAX = 00000001使用的
我们可以使用括号作为解引用运算符,就像在C中使用*一样,所以如果我们现在将行更改为:
mov eax, [myVar]我们得到:
EAX = 00000001hmm...the也一样。
但是现在我们可以想,哈!如果把一个地址偏移量放到数组中,我应该从数组“myVar”中获得下一个值:
mov eax, [myVar + 4] ; +4, because we have a DWORD (4 Bytes)结果是:
EAX = 00000005它起作用了!但是如果我在没有括号的情况下做同样的事情呢?
mov eax, myVar + 4
EAX = 00000005嗯,同样的事情!因此,在使用这两个符号时必须没有区别。所以它们是完全等同的。
https://stackoverflow.com/questions/16604558
复制相似问题