汇编练习理解 push 命令会发生什么

Assembly practise understanding what happens with push command

我们在课程中有这样一个使用汇编的练习。
这段代码后EAX寄存器保存了什么?

mov eax, 10
push eax
push 20
mov ecx, eax
pop eax

据我了解,第一行意味着 10 被放入 EAX,第二行 EAX 被压入堆栈。

但这是什么意思? eax 的内容是否被删除?
如果20也入栈栈是10和20?

代码后的 EAX 会注册为 10、10、20 或其他内容吗?它的格式如何?

让我们来看看这个step-by-step:

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。这代表 Last-in-First-out.

因此 EAX(=10) 的值首先被压入堆栈。在下一步中,值 20 被压入堆栈。 mov ecx, eax 指令对于这个任务没有意义,可以忽略。现在,随着pop eax,被压入堆栈的last项(记住后进先出)被取回并放入寄存器EAX。

因此最终答案是EAX=20。