Jmp指令的格式

本文解析了汇编语言中的跳转指令及其编码方式,包括相对地址和绝对地址两种表示法,并通过实例展示了汇编代码如何被编译为机器码。

jmp就是跳到另一个地方那个去执行

 

There are several different encodings for jumps, but some of the most commonly used ones are PC-relative.
1. That is, they encode the difference between the address of the target instruction and the address of the
instruction immediately following the jump. These offsets can be encoded using one, two, or four bytes.

相对地址表示法,这个相对是指相对于jmp下面一条指令的距离

2. A second encoding method is to give an “absolute” address, using four bytes to directly specify the target. The
assembler and linker select the appropriate encodings of the jump destinations.

绝对地址表示法

 

例子 

 

汇编代码

1      jle .L4                                                If <, goto dest2
2      .p2align 4,,7                                      Aligns next instruction to multiple of 8
3 .L5:                                                      dest1 :
4       movl %edx,%eax
5       sarl $1,%eax
6       subl %eax,%edx
7       testl %edx,%edx
8       jg .L5                                              If >, goto dest1
9 .L4:                                                     dest2 :
10      movl %edx,%eax

 

 

obj的机器码

1   8: 7e 11                                     jle 1b <silly+0x1b>               Target = dest2
2   a: 8d b6 00 00 00 00                lea 0x0(%esi),%esi               Added nops
3 10: 89 d0                                    mov %edx,%eax                  dest1 :
4 12: c1 f8 01                                sar $0x1,%eax
5 15: 29 c2                                    sub %eax,%edx
6 17: 85 d2                                    test %edx,%edx
7 19: 7f f5                                       jg 10 <silly+0x10>                Target = dest1
8 1b: 89 d0                                    mov %edx,%eax                   dest2 :

 

line 1中 11 是相对地址, 0x11 + 0x0a = 0x1b 这个地址就是dest2的相对的地址。

line 7中 f5 是相对地址, 是个负值 0xf5 + 0x1b = 0x10 这个地址就是dest1的相对地址。

 

link后的机器码

1 80483c8: 7e 11                                              jle 80483db <silly+0x1b>
2 80483ca: 8d b6 00 00 00 00                         lea 0x0(%esi),%esi
3 80483d0: 89 d0                                            mov %edx,%eax
4 80483d2: c1 f8 01                                         sar $0x1,%eax
5 80483d5: 29 c2                                             sub %eax,%edx
6 80483d7: 85 d2                                             test %edx,%edx
7 80483d9: 7f f5                                                jg 80483d0 <silly+0x10>
8 80483db: 89 d0                                              mov %edx,%eax

 

link后,伪代码的值变成了绝对值。 但是在机器码中仍然使用相对值,保持不变。

 

By using a PC-relative encoding of the jump targets, the instructions can be
compactly encoded (requiring just two bytes), and the object code can be shifted to different positions in
memory without alteration.

 

 

 

### jmp指令的工作原理 `jmp` 是一种控制流指令,主要用于实现程序中的无条件跳转功能。它通过修改程序计数器(PC)的值来改变程序执行的顺序[^1]。具体而言,当处理器遇到 `jmp` 指令时,会立即将 PC 的值设置为目标地址,从而使得下一条被执行的指令位于目标地址处。 #### 1. 跳转类型 `jmp` 指令可以分为两种主要形式: - **绝对跳转**:直接指定一个固定的内存地址作为目标地址。例如,在 x86 架构中,可以通过立即数或寄存器提供目标地址。 - **间接跳转**:目标地址存储在一个寄存器或内存单元中,CPU 需要先读取该地址再进行跳转。 以下是 x86 架构下的简单示例代码: ```asm ; 绝对跳转 jmp label ; 跳转到标签 'label' 处继续执行 ; 寄存器间接跳转 mov eax, target_address jmp eax ; 根据 EAX 中的目标地址进行跳转 ``` #### 2. 应用场景 `jmp` 指令广泛应用于各种编程需求中,包括但不限于以下几种情况: - 实现函数调用后的返回机制; - 控制循环结构的退出; - 支持分支语句(如 `if-else` 和 `switch-case`)的底层实现; - 执行状态机的状态切换逻辑。 值得注意的是,在某些高级语言编译过程中,可能会将复杂的条件判断转换成一系列基于比较结果的 `jmp` 操作序列[^3]。 #### 3. 流程表示方法的选择 为了更好地向不同受众群体解释涉及 `jmp` 指令的过程,可以选择适当的表现手法。如果面向非技术人员,则建议采用直观易懂的方式描绘基本动作链路;而针对开发者和技术专家,则需借助标准化图形符号精确刻画各个细节环节及其关联性[^2]。 --- ### 示例代码片段 下面给出一段伪汇编代码演示如何利用 `jmp` 完成简单的条件分支: ```asm section .data value db 5 ; 初始化变量值为5 section .text global _start _start: mov al, [value] ; 将'value'加载至AL寄存器 cmp al, 0 ; 对比 AL 是否等于零 je is_zero ; 如果相等则跳转至"is_zero" not_zero: ; 不为零时到达此处 inc al ; 增加 AL 的数值 jmp end_process ; 直接前往结束部分 is_zero: ; 当检测到零时进入此路径 dec al ; 减少 AL 的数值 end_process: ; 公共结尾段落 ... ``` 在此例子中可以看到,依据特定条件下产生的标志位变化决定下一步行动方向——这正是依靠了灵活运用 JMP 来达成目的的结果之一[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值