1. Registers and data storage
A register is a unit used to store data or instructions inside the cpu.
1.1 Composition of CPU
-
The computing unit performs information processing;
-
registers for information storage;
-
The controller coordinates various devices to work;
-
The internal bus realizes the connection between various devices in the CPU;
1.2 The register is the information storage unit inside the CPU
The 8086 CPU has 14 registers:
- General registers: AX, BX, CX, DX;
- Index register: SI, DI
- Pointer registers: SP, BP
- Instruction Pointer Register: IP
- Segment registers: CS, SS, DS, ES
- Flag Register: PSW
commonality
8086CPU所有的寄存器都是16位的
, 可以存放两个字节
;
1.3 General Registers
A 16-bit register stores a 16-bit data,
-
In the assembler, the default is expressed in decimal, if there is H after the data, it means hexadecimal;
-
. In the debug environment, the default is hexadecimal representation;
maximum?
2 16 − 1 2^{16}-1216−1
Store 18D in AX:
— 12H
— 10010B
Another example: store 20000D in AX
20000D
— 4E20H
— 0100111000100000B
1.4 Register Compatibility
The registers in the previous generation CPU of 8086 are all 8 bits, how to ensure the compatibility of the program?
- General-purpose registers can be divided into two independent 8-bit registers.
Refinement
; AX can be divided into AH and AL
; BX can be divided into BH and BL
; CX can be divided into CH and CL
; DX can be divided into DH and DL
1.5 Storage of "words" in registers
8086 is a 16-bit CPU
; 8086的字长(word size)为16bit
;
A word can be stored in a 16-bit register:
; The high-order byte of this word is stored in the high-order 8-bit register of this register
; the low-order byte of this word is stored in the low-order 8-bit register of this register
而在内存中存储的时候,低位字节的数据存放在低地址内存单元中,高位字节存放在高地址内存单元中
2. mov, add instruction
Note: Assembly instructions are not case sensitive
2.1 Learning assembly instructions - using middle school
2.2 Write the result of assembly instruction execution (1)
Assume that the values in the original AX and BX are both 0000H,
注意, 最后一步中加的结果,导致结果溢出
,
2.3 Write the result of the execution of the assembly instruction (2)
注意, 最后一步中加的结果,导致结果溢出
,
when using 8-bit registers, when adding, the overflow carry generated cannot be saved to the high-order 8-bit register;
Because 8-bit registers are used when adding, when there is a carry overflow, only registers that cannot be carried to the upper 8 bits can be discarded.
只有当使用的16位寄存器相加时,低8位产生的溢出,才可以进位到高8位的寄存器中
。
That is, if add ax, 93H is executed, the result of AX is 0158H
3. Method to determine the physical address
3.1 Physical address
-
When the CPU accesses the memory unit, it needs to give the address of the memory unit.
-
The storage space formed by all memory units is a one-dimensional linear space.
-
Each memory unit has a unique address in this space, and this unique address is called a physical address.
- fact
- The 8086 has a 20-bit address bus, can transmit 20-bit addresses, and has an addressing capability of 1M.
- 8086 is a CPU with 16-bit structure;
- The arithmetic unit can process up to 16-bit data at a time, and the maximum width of the register is 16 bits.
- The addresses processed, transmitted, and temporarily stored in the 8086 are also 16 bits, and the addressing capability is only 64KB!
地址总线的宽度和cpu中寄存器的宽度不一样
,
Question: How does 8086 deal with this contradiction in addressing space? !
3.2 The method of 8086CPU giving the physical address
Solution for 8086CPU
用两个16位地址(段地址、偏移地址) ,合成一个20位的物理地址
。
A method for synthesizing a physical address by an address adder;
物理地址=段地址×16+偏移地址
And multiplied by 16, represents a hexadecimal number, shifted one bit to the left;
8086CPU accesses the memory unit whose address is 123C8H:
3.3 The essential meaning of "segment address × 16 + offset address = physical address"
Problem to be solved:
用两个16位的地址(段地址、偏移地址),相加得到一个20位的物理地址.
Essential meaning:
When the CPU accesses memory, it adds a base address (segment address × 16) and an offset address relative to the base address to give the physical address of the memory unit.
4. Segmentation representation of memory
4.1 Manage memory in a segmented way
The 8086CPU gives the physical address of the memory unit in the form of "(segment address × 16) + offset address = physical address".
The addresses of all hardware devices will be mapped to the memory, and different hardware device addresses correspond to different physical addresses in the memory unit;
- The memory is not segmented, the segment division comes from the CPU! ! !
4.2 The same segment of memory, multiple segmentation schemes
(1) The segment address × 16 must be a multiple of 16, so the starting address of a segment must also be a multiple of 16;
(2) The offset address is 16 bits, and the addressing capability of the 16-bit address is 64K, so the maximum length of a segment is 64K.
In the figure on the left, the starting address (base address) is 10000H;
the segment address is 1000H, and the size is 100H;
In the figure on the right: the starting address (base address) is 10000H and 10080H,
the segment address is 1000H and 1008H, and the size is 80H
4.3 Use different segment addresses and offset addresses to form the same physical address
偏移地址16位,变化范围为0~FFFFH,用偏移地址最多寻址64KB.
Given a segment address of 2000H, the range addressed by the offset address is: 20000H~2FFFFFH, a total of 64K; the
expression method of the storage unit address in the 8086PC is:
The data is in the 21F60H memory unit, the segment address is 2000H, statement
(a) the data is stored in the memory unit 2000:1F60;
(b) the data is stored in the 1F60H unit in the 2000H segment of the memory
段地址: 使用4种专用寄存器来存放,四种不同类型的段地址
4 types of registers:
cs: code segment register;
DS: data segment register;
SS: stack segment register;
ES: extended register;
The offset address can be provided in a variety of ways - 8086's rich addressing methods.
5. Use of Debug
5.1 What is Debug
Debug is a well-known debugging program in the DOS system, and it can also run in the real mode of the windows system.
使用Debug程序,可以查看CPU各种寄存器中的内容、内存的情况,并且在机器指令级跟踪程序的运行!
Debug is legendary!
5.2 What can Debug do?
- Use the R command to view and change the contents of the CPU registers;
: use the D command to view the contents of the memory
: use the E command to change the contents of the memory
: use the U command to translate the machine instructions in the memory into assembly instructions
: use the A command to assemble instructions Write machine instructions in memory in the format
: Execute machine instructions with the T command
Start Debug, enter the command at the DOS prompt: debug
5.2.1 Use the R command to view and change the contents of the CPU registers
- R - view register contents
- R register name - change the contents of the specified register
5.2.2 Use the D command to view the contents of the memory
D List the contents of 128 bytes at the preset address memory;
D segment address: offset address, lists the content at the specified address in the memory;
D segment address: offset address, end offset address - list the contents of the specified address range in memory;
5.2.3 Use the E command to change the contents of the memory
E-segment address: offset address data 1 data 2 ...
E-segment address: offset address
; Inquiry-style modification one by one
; space-accept, continue
; carriage return-end
5.2.4 Use the U command to translate machine instructions in memory into assembly instructions
There are assembly instructions
mov ax, 0123H
mov bx, 0003H
mov ax, bx
add ax, bx
The corresponding machine code is
B8 23 01
BB 03 00
89 D8
01 D8
e address data - write
d address - view
u address - view code
5.2.5 Use the A command to write machine instructions in the memory in the format of assembly instructions
There are assembly instructions
mov ax, 0123H
mov bx, 0003H
mov ax, bx
add ax, bx
The corresponding machine code is
B8 23 01
BB 03 00
89 D8
01 D8
a address - write assembly instructions
d address - view data
u address - view code
5.2.6 Executing machine instructions with the T command
t - Execute the instruction at CS:IP
mov ax, 0123H
mov bx, 0003H
mov ax, bx
add ax, bx
5.2.7 Exit Debug with Q command
q - exit Debug
6. cs, ip and code segment
内存单元中的内容, 究竟用作数据,还是用作指令
:
depends on how the cpu is used,
If it is
CS:ip
, it means that the content in the pointed memory unit is used as an instruction,
becausecs:
represents the code segmentation code segment,
And if it is
ds:
, it means that the data segmentation data segment indicates the content in the memory unit, which is used as data.
6.1 Two key registers
指令的执行,是通过 cs 和ip来找到内存单元中需要执行的指令;
Special Register Name | effect |
---|---|
CS | code segment register |
IP | instruction pointer register |
CS:IP:
CPU将内存中CS:IP指向的内容当作指令执行
6.2 Code Execution under CS and IP Instructions
8086CPU current state: the content in CS is 2000H, and the content in IP is 0000H
The executable machine code is stored in memory 20000H~20009H
6.3 Demonstration of 8086PC reading and executing instructions
A brief description of the working process of 8086PC:
(1) Read instructions from CS:IP to the memory unit, and the read instructions enter the instruction buffer;
(2) IP = IP + the length of the read instruction, thus pointing to the next instruction;
(3) Execution of instructions. Go to step (1) and repeat the process
6.4 Empirical Demonstration of Instruction Reading and Execution-Debug
Use the debug program to execute the following code
mov ax, 0123H
mov bx, 0003H
mov ax, bx
add ax, bx
a address - write assembly instructions
u address - view code
t - execute code at CS:IP
Question: There is data B8 23 01 BB 03 00 89 D8 01 D8 in the memory,
is it used as general data or as an instruction?
Answer: The CPU regards the content in the memory unit pointed to by CS:IP as an instruction
7. jmp instruction
7.1 Modify CS, IP commands
Fact: Where to execute the instruction depends on CS:IP;
- Application: You can control the target instructions to be executed by the CPU by changing the contents of CS and IP
Question: How to change the value of CS and IP?
Method 1: The R command in Debug can change the value of the register—rcs, rip,
but Debug is a debugging method, not a program method!
Method 2: Modify with instructions
对于cpu中的专用寄存器而言(四种段地址寄存器和ip寄存器),不可以使用立即数 赋值的形式
cs等专用寄存器,虽然可以通过其他通用寄存器来赋值,但是不属于常规操作,不常用
ip的数值,只能是cpu自身改变,不可以通过赋值改变
Note: 8086CPU does not provide instructions to modify CS and IP through assignment!
Method 3: Transfer instruction jmp
7.2 Transfer instruction jmp
- Modify the content of CS and IP at the same time
jmp segment address: offset address
jmp 2AE3:3
jmp 3:0B16
Function:用指令中给出的段地址修改CS,偏移地址修改IP
- Only modify the content of the IP
jmp a legal register
jmp ax (similar to mov IP, ax)
jmp bx
Function:用寄存器中的值修改IP。
7.3 Problem Analysis
Starting from 20000H, the executed sequence is:
(1) mov ax,6622
(2) jmp 1000:3
(3) mov ax,0000
(4) mov bx,ax
(5) jmp bx
(6) mov ax,0123H
( 7) Go to step (3) to execute
这里注意到,两字节的数据在存放时,低字节的数据存放在内存单元中的低地址, 高字节的数据存放在内存单元中的高地址
Corresponding book chapter
Corresponding chapter content: