在x64汇编中的jmp指令期间,指令指针中将存储什么? [英] What will be stored in the instruction pointer during a jmp instruction in x64 assembly?
问题描述
在汇编中考虑一条简单的跳转指令(jmp),其中destination是一个预定义的标签.
Consider a simple jump instruction (jmp) in assembly, where destination is a pre-defined label.
jmp destination
根据Kip Irvine的"x86处理器的汇编语言",当CPU执行无条件传输时,目标的偏移量将移动到指令指针中.
According to Kip Irvine's "Assembly Language for x86 Processors" when the CPU executes an unconditional transfer, the offset of destination is moved into the instruction pointer.
有人可以解释一下,因为我认为我们要跳转的地址必须移到指令指针中吗?
Could someone explain this because I thought the address to which we want to jump must moved into the instruction pointer?
推荐答案
我找到了您正在谈论的段落:
I found the passage you're talking about:
4.5.1 JMP指令
4.5.1 JMP Instruction
JMP指令导致无条件地传输到目标,该目标由由汇编器转换为偏移量的代码标签标识.语法是
The JMP instruction causes an unconditional transfer to a destination, identified by a code label that is translated by the assembler into an offset. The syntax is
JMP目标
当CPU执行无条件传输时,目标位置的偏移被移到指令指针中,导致执行在新位置继续.
When the CPU executes an unconditional transfer, the offset of destination is moved into the instruction pointer, causing execution to continue at the new location.
您的困惑是可以理解的;这很难解释.
Your confusion is understandable; this is poorly explained.
首先,如果一条指令说jmp destination
,那么它将设置指令指针等于destination
.你是对的.
First of all, if an instruction says jmp destination
, then it will set the instruction pointer equal to destination
. You're right about that.
但是指令行为与指令 encoding 混淆了.
But the instruction behavior is being confused with the instruction encoding.
格式为jmp address
的指令在x86中使用相对偏移量进行编码.偏移量是相对于
Instructions of the form jmp address
are encoded using relative offsets in x86. The offsets are relative to the address immediately following the jmp
instruction.
可以将其编码为EB
,然后是带符号的字节偏移量,或者是E9
,然后是带符号的dword偏移量. (在x86中,整数为 little endian )
This can be encoded either as an EB
followed by a signed byte offset or an E9
followed by a signed dword offset. (Integers are little endian in x86)
例如,
00010000: EB 01 CC 90
反汇编为
loc_10000:
jmp loc_10003 ; EB 01
int3 ; CC
loc_10003:
nop ; 90
还有
00010000: E9 01 00 00 00 CC 90
反汇编为
loc_10000:
jmp loc_10006 ; E9 01 00 00 00
int3 ; CC
loc_10006:
nop ; 90
请注意,这意味着以相同方式编写的指令位于不同的地址时可能具有不同的编码.例如,
Note that this means instructions written the same way may have different encodings when located at different addresses. For example,
00010000: EB 02 EB 00 CC EB FD EB FB
反汇编为
loc_10000:
jmp loc_10004 ; EB 02
jmp loc_10004 ; EB 00
loc_10004:
int3 ; CC
jmp loc_10004 ; EB FD (FD == -3)
jmp loc_10004 ; EB FB (FB == -5)
侧面说明:jmp
指令有几种不同形式,但是您要说的类型只能用相对偏移量编码.
Side note: There are several different forms of the jmp
instruction, but the type you are speaking of can only be encoded with a relative offset.
无论如何,作者的意思是,为了使汇编器为诸如jmp destination
之类的指令生成机器代码,它必须将destination
转换为相对于jmp
指令末尾的字节偏移.但是,大多数情况下,您无需担心此过程.您只需在程序集中定义一个标签并编写jmp my_label
,汇编程序就会为您处理所有事情.
Anyway, what the author is saying is that, for an assembler to generate machine code for an instruction like jmp destination
, it must convert destination
to a byte offset relative to the end of the jmp
instruction. Most of the time, you don't need to worry about this process, however. You can just define a label in your assembly and write jmp my_label
, and the assembler will take care of everything for you.
这篇关于在x64汇编中的jmp指令期间,指令指针中将存储什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!