Assembly (3) registers (memory access)
1. Memory storage
1. Storage of words in memory
Start storing 20000 (4E20H) at address 0:
- Unit 0 is the low address unit, unit 1 is the high address unit
- Any two memory units with consecutive addresses, the N unit and the N+1 unit, can be regarded as the low byte unit and the high byte unit in a word unit with an address of N
2. DS and [address]
There is a DS register in the 8086CPU , which is usually used to store the segment address of the data to be accessed
- The AS register is to store the address of the instruction currently to be read by the CPU
- The DS register is the segment address that stores the data to be accessed
Example 1: Read the content of 10000H unit:
- mov bx,1000H
- mov ds,bx
- mov al, [0]
The above three instructions read the data in 10000H (1000:0) into al (al refers to the low register of ax)
Note: 8086CPU does not support the operation of sending data directly to the segment register, ds is a segment register.
Error: mov ds,1000H
Example 2: Send the data in al to the memory unit 10000H:
- mov bx,1000H
- mov ds,bx
- mov [0], al
3. Transmission of words
8086CPU is a 16-bit structure with 16 data lines, so 16-bit data can be transmitted at a time, that is, a word can be transmitted at a time
For example:
a memory unit is 8-bit, after executing the above instruction, the bit of unit 0 stores the low register data in cx, and the bit of unit 1 stores the high register data in cx.
Four. mov, add, sub
Several forms of the mov instruction:
mov register, data
mov register, register
mov register, memory unit
mov memory unit, register
mov segment register, register
mov register, segment register
The add and sub instructions, like mov, have two operation objects
5. Data segment
A group of memory units with a length of N (N≤64K), continuous addresses, and a multiple of 16 as the memory space for storing data, defines a data segment
Example 1: We use the space 123B0H~123B9H to store data:
Segment address: 123BH
length: 10 bytes
Example 2: Define the memory unit of 123B0H~123BAH as the data segment, we need to accumulate the data in the first 3 units in this data segment, the code is as follows:
Example 3: Accumulate the first 3 units in the data segment字型数据
Note: One word data occupies two units, so the offset address is 0, 2, 4
Six. Stack
The stack is a storage space with a special access method. Its particularity is that the 先进后出,后进先出
stack has two basic operations: push and pop
- Push: Put a new element on the top of the stack
- Pop: Take out an element from the top of the stack
The 8086CPU provides the two most basic stack and pop instructions:
- PUSH (push into the stack) example:
push ax
send the data in the register ax into the stack - POP (pop out of the stack) example:
pop ax
take data from the top of the stack and send it to ax
The stacking and popping operations of the 8086CPU are carried out in units of words
. The segment address and offset address of the current instruction are stored in the register CS and IP.
In 8086CPU, there are two registers:
- The segment register SS stores the segment address at the top of the stack
- Register SP stores the offset address of the top of the stack
At any time, SS:SP points to the top element of
the stack. When the stack is empty, SP points to the next element on the top of the stack
6.1 push and pop instructions
Push and pop instructions can also transfer data between registers and memory
format:
- push register: push the data in a register onto the stack
例:push ax
Segment register:
- pop register: pop out of the stack, use a register to receive the popped data
例:pop ax
- push segment register: push the data in a segment register onto the stack
例:push ds
- Pop segment register: pop out of the stack, use a segment register to receive the popped data
例:pop es
Memory unit:
- Push memory unit: Push the word at a memory unit onto the stack (stack operations are all in word units)
例:push [0]
- pop memory unit: pop out of the stack, use a memory word unit to receive the popped data
例:pop [2]
When the instruction is executed, the CPU needs to know the address of the memory unit, and the offset address of the memory unit can be given in the push and pop instructions. The segment address is obtained from the ds when the instruction is executed.
6.2 Execution process of push instruction
- SP=SP–2;
- Send the content in ax to the memory unit pointed to by SS:SP, and SS:SP now points to the top of the new stack
6.3 The execution process of the pop instruction
- Send the data at the memory unit pointed to by SS:SP into ax;
- SP = SP+2, SS:SP points to the unit below the current stack top, and the unit below the current stack top is the new stack top
- After being popped from the stack, SS:SP points to the new stack top 1000EH, the stack top element before the pop operation, 2266H at 1000CH still exists, but it is no longer in the stack.
- When the push instruction is executed again, SS:SP is moved to 1000CH, and new data is written in it, it will be overwritten
6.4 The problem of stack top out of bounds
SS and SP only record the address of the top of the stack, relying on SS and SP to ensure that the top of the stack is found when the stack is pushed and popped, but when the stack is full, the push instruction is used to push the stack, and the pop instruction is used when the stack is empty. Stack, the problem of out of bounds on the top of the stack will occur.
Regarding the problem of out-of-bounds, there are no good preventive measures at the cpu level. We can only worry about the problem of out-of-bounds on the top of the stack when writing programs. We must arrange the size of the stack according to the maximum stack space that may be used to prevent Out of bounds caused by too much data into the stack
6.5 Stack Segment
For 8086PC, when programming, we can define a group of memory units as a segment according to the needs, and we can define a group of memory units with a length of N (N ≤64K) with continuous addresses and a starting address that is a multiple of 16. Used as a stack, thus defining a stack segment.
For example: Use the 16-byte memory space from 10010H to 1001FH as a stack, so that it can be accessed in a stack, and this space can become a stack segment. The segment address is 1000H and the size is 16 bytes. . Then point SS:SP to the stack segment defined by us, then we can support stack operation instruction access such as push and pop.