以下汇编代码到底做了什么?

时间:2016-03-13 12:21:58

标签: assembly x86-64 low-level

我不确定以下应该做什么,但这是我到目前为止所做的。

enter image description here

mov eax, 5       (move 5 into register eax)   
add eax, ebx     (add 5 from eax to 0 from ebx and store in eax)
nop              (no operation)
nop              (no operation)
push ebx         (push 0 onto the hardware stack)
nop              (no operation)
pop ebx          (pop the 0 from off the stack and store in ebx)
call [eax]       (get the 5 from eax)

2 个答案:

答案 0 :(得分:4)

片段A的更正说明是:

var self = this;

mov eax, 5 ; move 5 into register eax add eax, ebx ; add contents of ebx to eax, changing eax nop ; no operation nop ; no operation push ebx ; push contents of ebx onto the stack nop ; no operation pop ebx ; pop top of the stack into ebx call [eax] ; call the subroutine pointed to at location [eax] 后跟nop后跟push ebx后跟nop的{​​{1}}说明不会改变任何内容(除了保留pop ebx的前值之外在堆栈空间中的可用位置)。因此在功能上(尽管消耗的CPU周期数和代码空间减少),这相当于:

ebx

片段B是:

mov eax, 5       ; move 5 into register eax
add eax, ebx     ; add contents of ebx to eax, changing eax
call [eax]       ; call the subroutine pointed to at location [eax]

连续两次交换两个寄存器没有净效应,除了消耗CPU周期和代码空间。因此片段B在功能上归结为:

mov eax, 5       ; move 5 into register eax
push ecx         ; push contents of ecx onto the stack
pop ecx          ; pop top of the stack into ecx
add eax, ebx     ; add contents of ebx to eax, changing eax
swap eax, ebx    ; swap the contents of eax and ebx
swap ebx, eax    ; swap the contents of eax and ebx
call [eax]       ; call the subroutine pointed to at location [eax]
nop              ; no operation

功能与片段A相同。

答案 1 :(得分:1)

两个代码片段通过EAX进行间接调用。如果两个片段中的EBX值相同,则会调用相同的代码,因为两个片段都将ebx添加到eax。