Why stacks and heaps grow in different directions __ memory

Source: Internet
Author: User

The growth direction of the stack
The 8051 stack is growing to a high address, and Intel's 8031, 8032, 8048, 8051 series use a stack that grows to a high address, but also Intel, which uses a stack that grows to a low address in the x86 series. In other companies, except for the ARM structure, which provides a stack option for high address growth, most are using stacks that grow to a low address.

Historical reasons
In the age of no MMU, in order to maximize the use of memory space, heaps and stacks are designed to grow from opposite ends. So which one is up and which one is down?
People are accustomed to data access to the upward, such as you in the heap new an array, is used to put low elements to low address, put the high address, so the heap upward growth is more in line with the habit. And the stack is not sensitive to the direction, the general operation of the stack only push and pop, does not matter upward downward, so put stacked in the low-end, put the stack on the high-end. MMU out of the matter, but there is no need to change.
51 This monolithic microcomputer, there is no heap, only the stack, so the stack is designed to be upward, conducive to expansion, such as 52 after 127 plus a 128 bytes, the program does not have to modify the direct transplant, if it is down to use this more than 128 bytes will modify the program, change the stack pointer.


The address between the data to see the CPU system, is generally fixed. The address in the data to see the CPU size end mode, sometimes can be changed.


ARM supports both types of growth, growing upwards and growing downward. For upward growth, the value of the stack pointer becomes larger after the data is written to the stack, called the increment stack. The reverse is the descending stack.


Another point to note is whether the storage unit that the stack pointer (SP) points to has been saved with data, and can be divided into two situations, namely, full stack and empty stack. This does not mean that the stack is full or empty, but that the current SP points to a cell that has valid data. If the SP points to a valid data item that is last pushed into the stack, called a full stack, the stack operation of the stacks should first be adjusted before the data is written to the SP. The other SP points to the next empty location to be pressed data (the SP points to a location that does not have valid data), called an empty stack, which writes the data before adjusting the SP.


51 SCM uses full stack of the situation, the SP always defaults to 07-bit, Phase 0 of the R7 register. When inserting data, you need to increment the SP before you can write data. Just push, 51 completes the operation on its own, but the SP does point to the location of the valid data. Instead, the POPs are first assigned and then the SP is self reduced.


ARM supports these 2 kinds of situations. Combined ARM supports 4 types of stack, full increment, empty increment, full decrement, empty descending.

Note: This article is sorted from the network, the original address: http://www.dzsc.com/dzbbs/20061126/200765184739359366.html

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.