问题描述
假设我正在用 x86 程序集编写一个例程,例如添加",它将作为参数传递的两个数字相加.
在大多数情况下,这是一个非常简单的方法:
push ebpmov ebp, espmov eax, [ebp+8]添加 eax, [ebp+12]mov esp, ebp流行音乐退
但是,有没有什么办法可以重写这个方法来避免使用ret"指令,并且仍然产生完全相同的结果?
这不需要任何空闲寄存器来模拟 ret
,但它需要 4 个字节的内存(一个 dword).使用间接 jmp
. 正如 Ira Baxter 所指出的,此代码不可重入.在单线程代码中工作正常.如果在多线程代码中使用会崩溃.
仅替换ret
指令,而不更改其余代码.不可重入.不要在多线程代码中使用.修复了以下代码中的错误.
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
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?
This does not need any free registers to simulate ret
, but it needs 4 bytes of memory (a dword). Uses indirect jmp
. Edit: 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
To replace only the ret
instruction, without changing the rest of the code. Not reentrant. Do not use in multithreaded code. Edit: 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"?指令相当于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!