Chapter 6: Program Control Instructions
6-1 Jump Group
- 无条件和有条件跳转
- 数值计算改变 flag,有条件跳转指令检测 flag 决定是否跳转
- LOOP 也是一种跳转
JMP
无条件跳转
- short jump - +127 and –128 bytes
- intrasegment jumps
- near jump - 3byte,±32K bytes
- intersegment jumps
- far jump - 5-byte,any memory location within the real memory system
- FAR PTR or far label
- JA, JB, JAE, JBE, JE, and JNE instructions - above and below refer to unsigned numbers
- JG, JL, JGE, JLE, JE, and JNE - greater than and less than refer to signed numbers
6–2 CONTROLLING THE FLOW OF THE PROGRAM
6-4 INTRO TO INTERRUPTS
exception handlers and interrupt handlers - interrupt service procedure (ISP)
- 一个异常或中断有 vector number (interrupt vector)
- 以这个 number 为 index 在 interrupt vector table (IVT) or interrupt descriptor table (IDT) 找 ISP
- IVT is used in real mode
- IDT is used in protected mode and long mode
中断 - 主动请求
- 有内部终中断和外部中断
- 外部中断 maskable or nonmaskable(需不需要满足 FLAGS.IF=1 才能触发)
异常 - 意外发生(可以带错误码)
- Program-Error Exceptions
- Software-Generated Exceptions - INTO, INT1, INT3, and BOUND instructions permit exceptions
- Machine-Check Exceptions
E.g.
#GP(0)
#
- pound sign
精确异常和非精确异常
- 精确异常 - 可重启
- 在异常指令的前面或者后面报错
- 非精确异常 - 不可重启
- 例:机器自检发出异常
异常分类
前两种是精确,后一种是非精确异常
- faults - 异常指令之前报,比如缺页异常
- traps - 异常指令之后报(重启时需要回退一步)
- abort
Interrupt Vectors
- 特定中断对应相应的中断向量号
- 中断向量被 trap 程序使用,跳转到对应的 service routine
- 256 个,每个包含一个 ISP 的地址
- 实模式下
- 4-byte number stored in the first 1024 bytes of memory (00000H–003FFH)
- Each vector contains a value for IP and CS that forms the address of the ISP
- the first 2 bytes contain IP; the last 2 bytes CS
- 保护模式 - IDT,8B 描述符
Intel: 保留开始的 32 个 vector for predefined exception and interrupt conditions
剩下的 32-255 可被用户使用(只能自定义中断)
实模式下
double fault exception
- 特定组合、嵌套异常会报双故障异常
- triple fault 会导致系统重启
Error Code
中断优先级
- 相同优先级时,可能会嵌套/排队
- 不同优先级,嵌套
Interrupt Instructions
INT N, INT1, INT3 and INTO
- INT 10H instruction calls the interrupt service procedure whose address is stored beginning at memory location 40H (10H 4)
6–5 MACHINE CONTROL AND MISCELLANEOUS INSTRUCTIONS
多字节 NOP
LOCK Prefix
- 在一些读写指令前加上 LOCK 前缀,保证操作的原子性
- 多核、共享内存的情况下保证独占性
- 只有当指令 modify memory,才能使用 LOCK(否则报错 undefined opcode exception(#UD)
; MOV 不是可以 LOCK 的指令
LOCK MOV [EAX], EBX ; #UD occurs
LOCK ADD EAX, [EBX] ; #UD occurs
LOCK ADD DWORD PTR [EAX], 1 ; correct
ENTER and LEAVE
- create & release stack frames for called procedure
- stack frame contains the local variables and the arguments passed by its caller
ENTER stack space, nesting levels
- nested functions using enter and leave