问题描述
我们在课程中有一个使用汇编的实践,就像这样。
此代码后在EAX寄存器中保存了什么?
<a href="a.js" download>a.js</a>
因此,据我了解,第一行表示将10放入EAX,第二行将EAX推送至堆栈。
但这是什么意思? eax的内容会被擦除吗?
如果还将20推入堆栈,则堆栈为10和20?
代码之后,EAX会注册为10、10、20之类的东西吗?它如何格式化?
解决方法
让我们逐步进行以下操作:
mov eax,10 ; This assigns the value 10 to the register EAX
push eax ; This puts the value of EAX onto the stack (a LIFO structure)
push 20 ; This puts the DWORD value 20 onto the stack
mov ecx,eax ; This moves the value of the register EAX to the register ECX - the left hand side is the destination in x86 assembly with intel syntax
pop eax ; This puts the top value of the stack into EAX and decreases the stackpointer to now point to the preceding item
最后一条指令需要一些其他信息。我提到堆栈是LIFO data structure。这代表后进先出。
因此,EAX(= 10)的值首先被压入堆栈。在下一步中,将值20推入堆栈。 mov ecx,eax
指令对此任务没有意义,可以忽略。现在,使用pop eax
,检索已被压入堆栈的 last 项(记住LIFO)并将其放入寄存器EAX。
因此,最终答案是EAX = 20。