Cainiao compilation 005 stack and segment register SS and SP

Source: Internet
Author: User

1. There are two basic operations on the stack: Inbound and Outbound

In the stack, a new element is placed at the top of the stack. In the stack, an element is taken from the top of the stack;

 

2. The elements at the top of the stack always last in the stack and first out of the stack. This operation rule of the stack is called LIFO (last in first out) and then in first 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, 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.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:

This can be illustrated.

 

First, the low address unit is at the top of the stack, and the high address unit is at the bottom of the stack.
. As shown in.

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 used as the stack, and the initial state stack is empty.

(2) set the ax register = 001ah, And the Bx register = 001bh.

(3) import data in the ax and BX registers to the stack.

(4) Clear the ax and BX registers.

(5) restore the original contents of the ax and BX registers from the stack.

 

MoV ax, 1000 h <br/> mov SS, ax <br/> mov sp, 0010 H <br/> mov ax, 001ah <br/> mov BX, 001bh <br/> push ax <br/> push BX <br/> mov ax, 0; sub ax, ax <br/> mov BX, 0; sub BX, BX <br/> is also available; sub ax, the machine code of ax is 2 bytes <br/>; MoV ax, the machine code of 0 is 3 bytes <br/> pop BX <br/> pop ax

 

13. 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.

MoV ax, 1000 h <br/> mov SS, ax <br/> mov sp, 0010 H <br/> mov ax, 001ah <br/> mov BX, 001bh <br/> push ax <br/> push BX <br/> pop ax <br/> pop BX

 

14. Programming

If you want to write 2266 h of font data into 10000h, you can use the following code:

MoV ax, 1000 h <br/> mov ds, ax <br/> mov ax, 2266 H <br/> mov [0], ax

 

Requirement: you cannot use commands such as mov memory units and registers to complete the above functions.

 

MoV ax, 1000 h <br/> mov SS, ax <br/> mov sp, 2 <br/> mov ax, 2266 H <br/> PUSH AX



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.