本文介绍了x86的"ret"是什么?指令相当于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我正在用x86汇编语言编写一个例程,例如"add",它将两个作为参数传递的数字相加.

Say I'm writing a routine in x86 assembly, like, "add" which adds two numbers passed as arguments.

在大多数情况下,这是一个非常简单的方法:

For the most part this is a very simple method:

push ebp
mov ebp, esp
mov eax, [ebp+8]
add eax, [ebp+12]
mov esp, ebp
pop ebp
ret

但是,有什么办法可以重写此方法,以避免使用"ret"指令,而仍然可以产生完全相同的结果?

But, is there any way I could rewrite this method to avoid the use of the "ret" instruction and still have it produce the exact same result?

推荐答案

这不需要任何可用寄存器来模拟ret,但是它需要4个字节的内存(一个双字).使用间接jmp. 编辑:正如Ira Baxter所述,此代码不可重入.在单线程代码中工作正常.如果在多线程代码中使用,将会崩溃.

This does not need any free registers to simulate ret, but it needs 4 bytes of memory (a dword). Uses indirect jmp. As noted by Ira Baxter, this code is not reentrant. Works fine in single-threaded code. Will crash if used in multithreaded code.


push ebp
mov  ebp, esp
mov  eax, [ebp+8]
add  eax, [ebp+12]
mov  ebp, [ebp+4]
mov  [return_address], ebp
pop  ebp

add  esp,4
jmp  [return_address]

.data
return_address dd 0

仅替换ret指令,而不更改其余代码.不可重入.不要在多线程代码中使用. 修复了以下代码中的错误.

To replace only the ret instruction, without changing the rest of the code. Not reentrant. Do not use in multithreaded code. fixed bug in below code.


push ebp
mov  ebp, esp
mov  ebp, [ebp+4]
mov  [return_address], ebp
pop  ebp

add  esp,4
jmp  [return_address]

.data
return_address dd 0

这篇关于x86的"ret"是什么?指令相当于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:36