Stack stack and segment register SS, SP (learning Assembly)

Source: Internet
Author: User

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
  1. MoV ax, 1000 h
  2. MoV SS, ax
  3. MoV sp, 0010 H
  4. MoV ax, 001ah
  5. MoV BX, 001bh
  6. PUSH AX
  7. Push BX
  8. Pop ax
  9. 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
  1. MoV ax, 1000 h
  2. MoV ds, ax
  3. MoV ax, 2266 H
  4. MoV [0], ax

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

[CPP]View plaincopy
  1. MoV ax, 1000 h
  2. MoV SS, ax
  3. MoV sp, 2
  4. MoV ax, 2266 H

 

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.