汇编语言程序设计笔记

The microprocessor and its architecture (Chap. 1-2)

图 1 架构对比

运算资源:寄存器

图 2 寄存器
名称作用备注
RAX(64bit)累加寄存器有单独的ADD代码,代码密度更高
RBX(64bit)基址寄存器无特殊功能
RCX(64bit)计数寄存器用于loop等需要计数的指令中
RDX(64bit)数据寄存器乘除法指令中使用
RBP(64bit)基指针寄存器指向当前栈帧的基地址
RSI(64bit)源变址寄存器字符串操作指令中使用
RDI(64bit)目的变址寄存器字符串操作指令中使用
R8 - R15扩展寄存器64bit架构下新增的8个通用寄存器
数量bits内容
168-bit low-byte registersAL, BL, CL, DL, SIL, DIL, BPL, SPL, R8B-R15B
48-bit high-byte registersAH, BH, CH, DH, addressable only when no REX prefix is used
1616-bit registersAX, BX, CX, DX, DI, SI, BP, SP, R8W-R15W
1632-bit registersEAX, EBX, ECX, EDX, EDI, ESI, EBP, ESP, R8D-R15D
1664-bit registersRAX, RBX, RCX, RDX, RDI, RSI, RBP, RSP, R8-R15

注意

x86架构下,只有64bit mode支持8bit的BP,SP,SI,DI寄存器(即BPL,SPL,SIL,DIL),而在32位下,它们是不开放的,最小就到16bits。

嵌套寄存器的影响规则

MOV RAX, 1111111111111111H     ; RAX = 1111111111111111H
MOV EAX, 22222222H             ; RAX = 0000000022222222H
MOV AX, 3333H                  ; RAX = 0000000022223333H
MOV AL, 44H                    ; RAX = 0000000022223344H

并行

特殊用途的寄存器

名称作用备注
RIP指令指针寄存器指向下一条将要执行的指令地址
RFLAGS标志寄存器存储算术运算结果的状态标志位,以及控制位
RSP栈指针寄存器指向当前栈顶地址
图 3 FLAG寄存器

status flags

名称作用
C (bit 0):Carry flag holds the carry after addition or borrow after subtraction.
Z (bit 6):Zero flag shows that the result of an arithmetic or logic operation is zero.
S (bit 7):Sign flag holds the arithmetic sign of the result after an arithmetic or logic instruction executes.
O (bit 11):Overflow flag occurs when signed numbers are added or subtracted. An overflow indicates the result has exceeded the capacity of the machine.
P (bit 2) 校验位Parity flag is set if the least-significant byte of the result contains an even number of 1 bits; cleared otherwise, 用于串行通讯.
A (bit 4) 辅助进位标志Auxiliary carry holds the carry (half-carry) after addition or the borrow after BCD operations between bit positions 3 and 4 of the result.

DF flags

名称作用
Dselects increment or decrement mode for the DI and/or SI registers.

system flags (控制作用)

名称作用
Tenables trapping through an on-chip debugging feature
IDallows the use of the CPUID instruction to identify processor features

Segment registers

名称作用
CS (code)code segment selector
DS (data)data segment selector
SS (stack)stack segment selector
ES (extra)extra segment selector
FS (general purpose)general purpose segment selector
GS (general purpose)general purpose segment selector

注意

在64位中,不实用segment管理,但是为了兼容,仍然保留了段寄存器,但是DS,ES等都为0;CS保留了部分功能,FS,GS在基地址中仍然有用。

System registers

不同运行模式与寄存器

启动过程:

运算资源:内存

基本要求

实模式

保护模式

The selector is located in the segment register and is used to locate the descriptor in the descriptor table, which describes the base address, limit, and access rights of the memory segment. It selects one of 8192 descriptors from one of two tables of descriptors. A segment size maximum is 64KB, a descriptor size is 8B, therefore a table can store up to 8K descriptors. Each program needs at least 2 segments (code and data), therefore a table supports up to 4K programs.

描述符 (descriptors)

描述符的结构:

描述符的G位 (期末必考题)

The G or granularity bit determines the scaling of the segment limit field.

Problem 1

For a descriptor with a base address of 10000000H, a limit of 001FFH, and G=0, what is the starting and ending locations?

Problem 2

For a descriptor with a base address of 10000000H, a limit of 001FFH, and G=1, what is the starting and ending locations?

权限位:

选择子(selector):

权限的管理:

访问数据段和访问栈

描述符表的位置

保护模式下内存访问的过程

如何寻找局部描述符(DS的TI位为1时):

LDTR的更新机制:

内存模式:

分页机制

地址

page

多级分页

下面的讨论摘自OS笔记:

我们将使用以下的参数进行讨论:

首先,我们使用如下的Paging配置:

virtual address space size64 bits
entry size4 Bytes
page size4 KB
Physical memory4 GB

接着使用以上的配置考虑一个极端情况:

使用Page存储Page table:

Hierarchical Paging:

在有大量无效PTE的情况下,Hierarchical Paging能够节省大量内存空间。

经典题目

Problem: Consider a system using multi-level paging scheme. The page size is 4 KB. The physical memory is 16 TB ( bytes) and virtual address is 32 bits long. The page table entry size is 4 bytes.

Find:

Solution:

页表项

多级页表中的权限保护: PDE (Page-Directory Entry), PTE (Page-Table Entry)

相关寄存器

缺页异常

分页模型

分页模型的扩展

,

由于需要支持36bit的物理地址,page number达到了36-12=24bits,于是entry的大小扩展到了8bytes。

PAE模式下的PSE (2MB):

优先级:PAE打开,自动启用PSE

TLB

Addressing modes (Chap. 3)

Data movement instructions (Chap. 4)

Arithmetic and logic instructions (Chap. 5)

Program control instructions (Chap. 6)

Using assembly language with c/c++ (Chap. 7)

Basic I/O interface (Chap. 11)