实模式跳转到远地址 [英] Jumping to a far address in real mode

查看:28
本文介绍了实模式跳转到远地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我必须在实模式下跳转到远地址,我在 fs 寄存器中有段值,在 gs 寄存器中有偏移量,并且在跳转我必须保持确切的寄存器内容,我想出了一个想法如下,

I have a situation where I have to jump to a far address in real mode, I have the segment value in fs register and offset in gs register, and during the jump I have to maintain the exact register content, I have come up with one idea as following,

mov bp, fs
shl ebp, 16
mov bp, gs
jmp ebp

假设 bpfsgs 没有在被调用的目的地中读取,这是我刚刚在 实模式下的NASM远跳/远调用和ASM代码约定 我可以使用,

assuming bp, fs and gs is not read in the called destination, another way I just found in NASM far jump / far call in real mode and ASM code conventions and I can use,

push fs
push gs
retf

我想知道我应该使用哪种方法,或者是否还有其他方法可以实现?我在 x86 汇编方面没有太多技能,所以请原谅我的无知.

I am wondering which method I should use or if there is any other way to achieve this ? I don't have much skill in x86 assembly so please forgive my ignorance.

问候,

阿尔卡

推荐答案

如果性能很重要,不匹配的调用/返回对会导致 返回地址预测器,导致对这个 retf 和以后返回的分支错误预测.(如果 far call/far ret 甚至参与其中;他们可能不会,IDK.)否则这是显而易见的选择.

If performance matters, mismatched call/return pairs throw off the return-address predictor, leading to the equivalent of a branch mispredict on this retf and later returns. (If far call / far ret even participate in that; they might not, IDK.) Otherwise it's the obvious choice.

jmp ebp 是一个接近跳转(不改变 cs)所以不能工作.您将使用 seg:off 作为 32 位整数,将 EIP 设置为该值,而不是 CS:IP.

jmp ebp is a near jump (doesn't change cs) so that can't work. You'd be using the seg:off as a 32-bit integer, setting EIP to that value, not CS:IP.

您需要远跳(jmp ptr16:16jmp m16:16).ptr16:16 版本要求在指令中编码目标地址(因此它不是间接跳转).唯一可用的间接(可变目标)远跳转编码具有内存中的段:偏移对,而不是 (a) 寄存器

You need a far jump (jmp ptr16:16 or jmp m16:16). The ptr16:16 version requires the target address to be encoded in the instruction (so it's not an indirect jump). The only indirect (variable-destination) far jump encoding available has the segment:offset pair in memory, not (a) register(s)

mov [mem], fs
mov [mem+2], gs
jmp far [mem]

语法来自https://courses.engr.illinois.edu/ece390/archive/spr2002/books/labmanual/inst-ref-jmp.html

push/push/retf 会小很多,而且不需要单独的暂存空间,所以可能会更好.如果性能很重要,请同时衡量.

push/push/retf will be significantly smaller, and doesn't need a separate scratch space, so it's probably better. If performance matters, measure both ways.

mem 空间可以在栈上,也可以是静态存储.但是,如果您在到达目的地时需要特定的堆栈内容,您可能无法在堆栈上留下额外的东西,并且使用堆栈下方的空间是不安全的.(并且您只能使用诸如 [bp-4] 之类的寻址模式来寻址堆栈,而不是相对于 [sp],除非您使用的是 32 位的 386寻址模式,如 jmp far [esp+4] 或其他.)

The mem space can be on the stack, or static storage. But if you need specific stack contents when reaching the destination, you might not be able to leave extra stuff on the stack, and using space below the stack wouldn't be safe. (And you can only address the stack using addressing modes like [bp-4], not relative to [sp], unless you're on a 386 for 32-bit addressing modes like jmp far [esp+4] or whatever.)

这篇关于实模式跳转到远地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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