ch2_2 access registers and memory

1. Storage of words in memory

1.1 Storage of words in memory

Factual scene:
对8086CPU,16位作为一个字, 表明一个包含两个字节,对应的便需要两个存储单元来存放;

So what is the specific storage order of the two bytes in the memory unit?

Question
16-bit word is stored in a 16-bit register, how to store it?
Answer:
The high 8 bits put the high byte, and the low 8 bits put the low byte;

Question:
A 16-bit word needs to be stored in 2 consecutive bytes in memory, how to store it?

低位字节存在低地址单元,高位字节存在高地址单元

insert image description here

Note that number 0 is the low address unit and number 1 is the high address unit

1.2 word unit

  • Word unit: It consists of two memory units with continuous addresses, storing a word data (16 bits)

  • Principle: In a word unit, the low address unit stores the low byte, and the high address unit stores the high byte;

In the unit whose starting address is 0, it stores 4E20H;
in the unit whose starting address is 2, it stores 0012H

insert image description here

Problem detection:
(1) The byte type data stored in the 0 address unit is (20H)
(2) The word type data stored in the 0 address word unit is (4E20H)
(3) The byte type data stored in the 2 address unit Yes (12H)
(4) The font data stored in the 2-address word unit is (0012H)

2. Use DS and [address] to realize the transmission of words

2.1 The CPU reads data from the memory unit

Requirements:
When the CPU wants to read a memory unit, it must first give the address of the memory unit;

Principle
In 8086PC, the memory address is composed of segment address and offset address
(segment address: offset address)

Solution: DS and [address] cooperate

  1. Use the DS register to store the segment address of the data to be accessed
  2. The offset address is given directly in the form of […]

注意,当直接使用[..] 的表示形式,则本身的含义代表的是使用的是 DS:[..], 以数据段 DS寄存器中的内容,作为段地址,偏移地址是[..]

  • The data in the memory unit is written to the register of the CPU;

Read the data in 10000H (1000:0) into al,

mov bx,1000H
mov ds,bx
mov al, [0]

  • Write the data in the cpu register to the memory unit,
    and write the data in al to 10000H (1000:0);

mov bx,1000H
mov ds,bx
mov [0],a

insert image description here

8086CPU does not support sending data directly into segment registers
(hardware design problem)
: routine: data --> general registers --> segment registers

注意到, 当使用DS数据段寄存器时, 则表明此时,从内存单元中取出的内容, cpu将会把该内容当做数据来处理, 因为DS数据段寄存器,表明的便是该内存单元中的内容是作为数据来处理的

2.2 Word transmission

8086CPU can transmit one word (16-bit data) at a time

mov bx, 1000H
mov ds, bx
mov ax, [0] ; the font data at 1000:0 is sent to ax
mov [0], cx ; the 16-bit data in cx is sent to 1000:0

insert image description here
insert image description here

2.3 Case

insert image description here

指令
mov ax, 1000H
mov ds, ax
mov ax, [0]
mov bx, [2]
mov cx, [1]
add bx, [1]
add cx, [2]

insert image description here

insert image description here

指令
mov ax, 1000H
mov ds, ax
mov ax, 2C31
mov [0], ax
mov bx, [0]
sub bx, [2]
mov [2], bx

3. DS and data segment

3.1 Access to data in the memory unit

For 8086PC, a group of memory units can be defined as a segment according to the needs;

Physical address = segment address × 16 + offset address;

Data segment:将一组长度为N(N≤64K)、地址连续、起始地址为16的倍数的内存单元当作专门存储数据的内存空间,从而定义了一个数据段。

Use the space of 123B0H~123B9H to store data
Segment address: 123BH Start offset address: 0000H Length: 10 bytes
Segment address: 1230H Start offset address: 00B0H Length: 10 bytes
...

insert image description here

Processing method: DS:[address]
Use DS to store the segment address of the data segment,
use related instructions to access specific units in the data segment, and the unit address is pointed out by [address]

3.2 The memory unit is defined as a data segment

Define the memory unit of 123B0H~123BAH as a data segment;

Accumulate the data in the first 3 cells in the data segment,

mov ax, 123BH
mov ds, ax
mov al, 0
add al, [0]
add al, [1]
add al, [2]

insert image description here

这里,需要注意到字型数据和 字节型数据之间的区别

That is, one word data occupies two byte data, and each byte data occupies one storage unit.

Accumulate the first 3 font data in the data segment

mov ax, 123BH
mov ds, ax
mov ax, 0
add ax, [0]
add ax, [2]
add ax, [4]

3.3 Practice

Given the data of 00000H-0001F, please write the execution result of the following code:
insert image description here
preset data, preset code, register value, execution code;

insert image description here

3.4 Manipulate data with mov instruction

command form illustration
mov register, data mov ax, 8
mov register, register mov ax, bx
mov register, memory location mov ax, [0]
mov memory location, register mov [0], ax
mov segment register, register mov ds, ax

Known: mov segment register, register
guess 1 mov register, segment register
insert image description here

: known: mov memory unit, register
speculation 2 mov memory unit, segment register
speculation 3 mov segment register, memory unit
insert image description here

Known: mov register, data
Guess 4 mov segment register, data

insert image description here

3.5 Addition add and subtraction sub instructions

add command form illustration
add register, data add ax, 8
add register, register add ax, bx
add register, memory unit add ax, [0]
add memory unit, register add [0], ax

insert image description here

Sub command form illustration
sub register, data sub ax, 8
sub register, register sub ax, bx
sub register, memory unit sub ax, [0]
sub memory unit, register sub [0], ax

3.6 Summary of methods for accessing data segments in memory in the form of DS and [address]

mov ax, 1000H
mov ds, ax
mov ax, 11316
mov [0], ax
mov bx, [0]
sub bx, [2]
mov [2], bx

Combining Experience and Taste:

(1)字在内存中存储时 ,要用两个地址连续的内存单元来存放,字的低位字节存放在低地址单元中,高位字节存放再高地址单元中。

(2)用 mov 指令要访问内存单元,可以在mov指令中只给出单元的偏 移地址,此时,段地址默认在DS寄存器中。

(3) [address] indicates a memory unit whose offset address is address.

(4)在内存和寄存器之间传送字型数据时,高地址单元和高8位寄存器、低地址单元和低8位寄存器相对应。

(5) mov, add, and sub are instructions with two operation objects, accessing the data segment in memory (contrast: jmp is an instruction with one operation object, corresponding to the code segment in memory).

(6) You can experiment with the new format of instructions in Debug according to your own speculation.

4. Implementation of stack and stack operations

A stack is a data structure that can only be inserted or deleted at one end.

insert image description here

4.1 Stack structure

The stack has two basic operations: push and pop.

Push: Put a new element on the top of the stack;
Pop: Take an element from the top of the stack.

  • The element at the top of the stack is always pushed into the stack last, and when it needs to be popped, it is taken out of the stack first.
    Stack operation rules: LIFO (Last In First Out, Last In First Out)

The stack mechanism provided by the CPU

Today's CPU has a stack design.
The 8086CPU provides related instructions and supports accessing the memory space by means of a stack.
Based on 8086CPU programming, a section of memory can be used as a stack

PUSH (into the stack) and POP (out of the stack) instructions

push ax: send the data in ax to the stack
pop ax: take the data from the top of the stack and send it to ax
ax是16位通用寄存器,表明(以字为单位对栈进行操作)

4.2 Memory is used as a stack

Suppose the 10000H~1000FH memory is used as a stack,

mov ax,0123H
push ax
mov bx,2266H
push bx
mov cx,1122H
push cx
pop ax
pop bx
pop cx

insert image description here

Questions:
1. How does the CPU know that a section of memory space is used as a stack?
2. When executing push and pop, how do you know which unit is the top unit of the stack?

Answer:
In the 8086cpu, there are two registers related to the stack:

栈段寄存器ss, 用于存放栈顶的段地址,

栈顶指针寄存器sp, 用于存放栈顶的偏移地址,

At any moment, ss:sp points to the top element of the stack;

4.3 Stack operation

mov ax, 1000H
mov ss, ax
mov sp, 0010H
mov ax, 001AH
mov bx, 001BH
push ax
push bx
pop ax
pop b

insert image description here

4.4 Execution process of push instruction and pop instruction

push ax: push the stack, 默认的是先将sp栈顶指针代表的偏移地址减去2, 然后将ax寄存器中的内容输入到 ss:sp 指向的内存单元中;

The reason for adding and subtracting 2 here is that the ax register is 16 bits, which represents the content of two bytes, so it is adding and subtracting two;

pop ax: out of the stack,默认的是先将ss:sp 指向的内存单元中的内容输入到寄存器 ax中,然后将栈顶指针sp+2;

Note that during the process of pushing the stack, the top of the stack is gradually moved up, moving from high address to low address;

In the process of popping out of the stack, the top of the stack gradually moves down, from low address to high address.

push ax
(1) SP=SP–2;
(2) Send the content in ax to the memory unit pointed to by SS:SP
, and SS:SP points to the top of the new stack at this time.
:pop ax
(1) Send the data at the memory unit pointed to by SS:SP into ax;
(2) SP = SP+2, SS:SP points to the unit below the current stack top, take the unit below the current stack top as The new top of the stack.

mov ax, 1000H
mov ss, ax
mov sp, 0010H
mov ax, 001AH
mov bx, 001BH
push ax
push bx
pop ax
pop bx

insert image description here

: The problem of the top of the stack being out of bounds
How to ensure that the top of the stack will not exceed the stack space when pushing or popping the stack?

4.5 When performing a push, the top of the stack exceeds the stack space

insert image description here

4.6 When performing a pop, the top of the stack exceeds the stack space

insert image description here

insert image description here

The solution to the stack top out of bounds problem

:8086CPU does not guarantee that the operation on the stack will not exceed the bounds.

The 8086CPU only knows where the top of the stack is (indicated by SS:SP), and does not know how much stack space the program arranges.

When programming, we have to worry about the problem of the top of the stack being out of bounds. We must arrange the size of the stack according to the maximum stack space that may be used to
prevent over-bounding caused by too much data being pushed into the stack;
to prevent the stack from being empty when popping out the stack. The out-of-bounds caused by continuing to pop the stack

4.7 Summary of the stack

Push and pop are essentially memory transfer instructions, which can transfer data between registers and memory.

与mov指令不同的是, push和pop指令访问的内存单元的地址不是在指令中给出的,而是由SS:SP指出的。

When the push and pop instructions are executed, the content in SP changes automatically.

The stack operation mechanism provided by 8086CPU:

The segment address and offset address defined by the stack are stored in ss, sp, and the push and pop instructions access the memory unit according to the address indicated by ss:sp according to the stack.

The execution steps of the push command:

  1. sp = sp - 2:
  2. Input data into the memory unit pointed to by ss:sp;

The execution steps of the pop instruction:

  1. Read data from the memory unit pointed to by ss:sp,
  2. sp = sp +2

insert image description here

5. Summary of paragraphs

Base

Physical address = segment address × 16 + offset address

  • practice
  1. When programming, a group of memory cells can be defined as a segment as needed.
  1. A group of memory units whose starting address is a multiple of 16 and whose length is N (N ≤ 64K) can be defined as a segment.
  1. 将一段内存定义为一个段,用一个段地址指示段,用偏移地址访问段内的单元——在程序中可以完全由程序员安排

5.1 Three types of segments

  1. Data segment DS
    . Put the segment address in DS
    . When using mov, add, sub, etc. to access memory unit instructions,
    CPU将我们定义的数据段中的内容当作数据段来访问;

  2. Code segment CS
    . Put the segment address in CS and the offset address of the first instruction in the segment in IP.
    . CPU将我们定义的代码段中的内容当做指令来执行;

  3. Stack segment
    . Put the segment address in SS, and put the offset of the top unit in SP
    .CPU在需要进行栈操作(push、pop)时,就将我们定义的栈段当作栈空间来用。

5.2 Set the segment and execute the code as required

insert image description here

mov bx, 1000H
mov ds, bx
mov bx, 1001H
mov ss, bx
mov sp, 10H
mov ax, [0]
mov bx, [2]
push ax
push bx
pop ax
pop bx
mov [0], ax
mov [2], bx

5.3 The addresses of the three segments can be the same!

insert image description here

mov bx, 1000H
mov ds, bx
mov ss, bx
mov sp, 20H
mov ax, [0]
mov bx, [2]
push ax
push bx
pop ax
pop bx
mov [0], ax
mov [2], bx

Guess you like

Origin blog.csdn.net/chumingqian/article/details/131615346