指令集架构
字数: 0
Instruction Set Architecture / ISA,定义一种抽象的接口规范,用于连接硬件和软件

指令集

CISC

Complex Instruction Set Computer,复杂指令集 ,常见的架构包括 Intel x86 和 AMD64。
CISC 中常用的指令集只占源代码的 20%,剩余的 80% 不常用,因此操作比较复杂耗时。
设计目标是为了提供更丰富的指令集,便于编写高级语言的编译器生成更紧凑的代码。

RICS

Reduced Instruction Set Computer,精简指令集 ,常见的架构包括 ARM 和 PowerPC。
RISC 设计的目标是通过简化指令执行过程来提高指令的执行速度和效率,通常具有固定长度、简单的指令格式和较少的指令操作码。

x86 指令集

数据传输指令

Data Transfer,通常用于加载(load)数据或程序到寄存器或内存中
  • mov

算术指令

  • add cx,bx:cx=cx+bx
  • sub al,10:al=al-10
  • inc cx:cx++
  • dec cx:cx--
  • mul bx:ax=ax*bx
  • div B :ax=ax/b
    • 被除数 A 默认存放在 ax 中
    • 结果是两个整数,商和余数
    • 如果除数是 8 位 (1 字节),则 al 存储商,ah 存储余数
    • 如果除数是 16 位,则 ax 存储商,dx 存储余数
  • cmp dl,"q":相当于减法指令,只是不改变 dl 的值。可以查看 zf 的值,比较数值大小。
  • adc ax 10h:(add with carry,进位加法)add 16 to ax if cf=0, add 17 to if cf=1
  • neg ax:将操作数的值取反(负数化)

跳转指令

jmp (unconditional jump instruction)
  • 用于设置CS、IP的值,形如 jmp 段地址: 偏移地址
    • jmp 2AE3:3,执行后:CS=2AE3H, IP =0003H,CPU 从 2AE33H 处读取指令
  • 仅修改 IP 的内容,形如 jmp 某一寄存器
    • jmp ax,执行前:ax=1000H, CS=2000H, IP=0003H; 指令执行后: ax=1000H, CS=2000H, IP=I000H
    • 类似于 mov IP,ax

  • ja:(无符号)大于 (above) 跳转(jumo if cf=0 and zf =0)
  • jae:大于等于
  • jb:(无符号)小于 (below) 跳转
  • jbe :小于等于
  • jc:jump if cf=1 (carry)
  • jnc:jump if cf=0
  • je: 与 jz 等效
  • jz:jump if zero flag set (zf=1)
  • jnz:jump if not zero

循环指令

loop 本质属于短转移 (jump short):在对应的机器码中包含转移的位移,而不是目的地址。对 IP 的修改范围为 -128~127。
CPU 执行该指令的时候, 进行两步操作
  • cx = cx -1
  • 判断 cx 的值, 不为零则转至标号处执行程序, 如果为零则向下执行。
  • If CX is not equal to zero and (ZF=0), else IP=IP+offset

  • loopz: Loop while zero (ZF=1) and CX not zero
  • loopnz: Loop while not equal (ZF=0) and CX not zero

Using loops as delays
Lecture 4
使机器延时 ,需要利用多少空循环?
  • : the desired time delay in clock cycle
  • : overhead time
    • In computer science, overhead is any combination of excess or indirect computation time, memory, bandwidth, or other resources that are required to perform a specific task.
  • : loop back time
  • : the number of times to go around the loop

    • On a MHz machine (Time for one Clock Cycle)
       
      ➡️ mov cx,109 or mov cx,6Ch

标志操作

  • clc: cf=0 (clear carry flag)
  • stc: cf=1 (set carry flag)
  • cmc: cf 取反 (complement carry flag)
  • cld: df=0 (clear direction flag)
  • std: df=1
  • cli: if=0, 关闭中断 (clear interrupt endable flag)
  • sti: if=1, 打开中断 (set interrupt endable flag)

位操作

经常用于提取特定位或数据的循环处理等
notion image
  • and ax,bx
  • or ax,bx
  • xor ax,bx
  • shl bx,1:逻辑左移 1 位
  • shr bx,1 :逻辑右移
  • shld: 双精度左移(shift left double)
  • shrd
  • rol ax,1循环左移(Rotate Left)
    • ax 中的数据向左移动一位,将最高位的数值同时插到最低位和放进 CF
  • rcl ax,1带进位循环左移(rotate through carry left)
    • ax 中的数据向左移动一位,将 CF 的值插入到最低位,然后把移出的最高位放进 CF
  • ror
  • rcr

💡 例题
Use cx to count 4 bits in each nibble
Clear ax and carry for shifting nibbles
Move from high nibble from bx to low nibble of ax

串操作

String manipulation.
  • movsb (Move string byte)
    • Copies memory pointed to by DS:SI to the address specified in ES:DI
    • If DF=0 , SI,DI are incremented
    • If DF=1 , SI,DI are decreased.
  • lea SI,label (Load effective address in to SI)
  • cmpsb

x87 指令集

也可以视为 x86 指令集的一部分

数据传输指令

  • FLD:用于将浮点数值加载到浮点寄存器 ST(0)
  • FSTP:将浮点数值从浮点寄存器弹出并存储到内存或寄存器中(Store and Pop)

算术指令

  • FADD / /source/destination, source
    • 源操作数 (source) 可以是寄存器、内存或常数
    • notion image
  • FSUB:ST(0) - 源操作数
    • ST 指代栈顶
  • FSUBR:源操作数 - ST(0)
  • FMUL
  • FDIV
  • FSQRT

控制指令

  • FINIT:将浮点处理器设置为默认初始状态
  • FLDCW:加载控制字寄存器的值到浮点处理器中
  • FSTSW:将状态字寄存器的值存储到通用寄存器中

寻址方式

Addressing Modes

x86 寻址

notion image
mov 的操作码通常是 B8 到 BF 之间,不同的操作码表示不同的寻址方式

寄存器间接寻址

Register Indirect Addressing: accessing memory in a similar manner to arrays in higher level languages
  • mov dx,[bx] is similar to dx=array[bx]
  • sub ax,[bX] Subtract the number pointed to by bx from ax. Result is put back in AX.

寄存器寻址

Register Addressing: the operand is contained in one of the general registers of the CPU
  • mov ax,bx is equivalent to ax=bx

直接寻址

Direct Addressing: the location in memory that contains the operand is given
存储单元的有效地址 EA 直接由指令给出
  • mov bx,Count is equivalent to bx=[Count]

立即寻址

立即数指汇编指令中直接指定的数字或常量
Immediate Addressing: the operand (data) appears in the instruction.
An example of an immediate instruction is one that moves a constant into an internal register.
  • mov ah,10 is equivalent to ah=10
  • mov bx, OFFSET msg1 is equivalent to bx=&[msg1]
 
2023 - 2026