什么是 x86 “ret"?指令相当于? [英] What is the x86 "ret" instruction equivalent to?

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

问题描述

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

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

推送ebpmov ebp, espmov eax, [ebp+8]添加 eax, [ebp+12]mov esp, ebp流行音乐ret

但是,我有什么办法可以重写这个方法来避免使用ret"指令并且仍然让它产生完全相同的结果?

解决方案

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

<上一页>推送ebpmov ebp, espmov eax, [ebp+8]添加 eax, [ebp+12]移动 ebp, [ebp+4]mov [return_address], ebp流行音乐添加 esp,4jmp [返回地址].数据return_address dd 0

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

<上一页>推送ebpmov ebp, esp移动 ebp, [ebp+4]mov [return_address], ebp流行音乐添加 esp,4jmp [返回地址].数据return_address dd 0

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"?指令相当于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆