1. There are two basic operations on the stack: Inbound and Outbound
2. The elements at the top of the stack always last in the stack, first out of the stack, and then in and out.
3. The 8086cpu provides the inbound and outbound commands. The two most basic commands are push (inbound) and POP (outbound)
Push ax refers to sending the contents of the ax register to the stack, and pop ax refers to pulling data from the top of the stack and sending it to the ax register.
The inbound and outbound operations of the 8086cpu are in word units.
4. In 8086cpu, segment register SS: stores the top segment address of the stack, and segment register SP: stores the offset address of the top of the stack.
5. Any time: SS: SP points to the top element of the stack.
6. The 8086cpu does not guarantee that our stack operations will be out of bounds.
7. The stack top is a low-address unit, and the stack bottom is a high-address unit.
8. Steps for executing the push command:
(1) sp = Sp-2 (offset address reduction, that is, offset to the lower address [stack top direction])
(2) send data to the word UNIT pointed to by SS: SP
9. Execution steps of pop commands:
(1) read data from the word UNIT pointed to by SS: SP
(2) sp = SP + 2 (the offset address is increased, that is, the offset to the high Address [stack low direction])
10. The 8086cpu only records the top of the stack, and the size of the stack space should be managed by ourselves.
11. Example:
If you change the value from 10000 h ~ The 1000fh space is used as the stack. The initial state stack is empty. At this time, Ss = 1000 h, SP =?
Answer:
First, the low address unit is at the top of the stack, and the high address unit is at the bottom of the stack.
Low address
10000 h
.
.
.
1000dh
1000eh
1000fh
-------------------
10010 H <------------- SS: SP points to the next unit of the maximum address unit of the stack space.
Assume that AX = 2266 H, because the inbound and outbound operations of the 8086cpu are in units of words. Therefore, the result is displayed.
10000 h
.
.
.
1000dh
1000eh 66 h (Al) <------------- SS: SP
1000fh 22 h (Ah)
--------------------------------------------
10010 H
Take 10000 h ~ The 1000fh space is stack space, Ss = 1000 h, and stack space is 16 bytes.
If the initial state stack is empty, SP = 0010 H.
12. Programming
(1) convert forward H ~ The 1000fh space is regarded as a stack, and the initial state stack is empty.
(2) set the ax register = 001ah, And the Bx register = 001bh.
(3) Use the stack to exchange data in the ax and BX registers.
[CPP]View plaincopy
- MoV ax, 1000 h
- MoV SS, ax
- MoV sp, 0010 H
- MoV ax, 001ah
- MoV BX, 001bh
- PUSH AX
- Push BX
- Pop ax
- Pop BX
13. Programming
If you want to write 2266 h of font data into 10000h, you can use the following code:
[CPP]View plaincopy
- MoV ax, 1000 h
- MoV ds, ax
- MoV ax, 2266 H
- MoV [0], ax
Requirement: you cannot use commands such as mov memory units and registers to complete the above functions.
[CPP]View plaincopy
- MoV ax, 1000 h
- MoV SS, ax
- MoV sp, 2
- MoV ax, 2266 H