Ext.: http://www.cnblogs.com/xkfz007/archive/2012/06/22/2558935.html
Stack growth and big-endian/small-end issues are two CPU-related issues.
In memory management, the stack corresponds to the heap. For the heap, the direction of growth is upward, that is, the direction of the memory address increase, for the stack, it is growing in a downward direction, is to reduce the memory address of the way to grow. In memory, "heap" and "stack" share all the free space, but their starting address and growth direction are different, they do not have a fixed limit, if at run time, "heap" and "stack" grow to overlap with each other, known as "stack heap conflict", the system must collapse.
In the common x86, the growth direction of the in-memory stack is to grow from a high address to a low address.
We can use some code to determine the direction of the stack growth:
#include <stdio.h>
static int stack_dir;
static void Find_stack_direction (void) {
static char *addr = NULL; /* Address of first
' Dummy ', once known */
char dummy; /* To get stack address */
if (addr = = NULL)
{/* Initial Entry */
addr = &dummy;
Find_stack_direction (); /* recurse once */
}
else/* Second entry */
if (&dummy > Addr)
Stack_dir = 1; /* Stack grew upward */
Else
Stack_dir =-1; /* Stack grew downward */
}
int main (void)
{
Find_stack_direction ();
if (stack_dir==1)
Puts ("stack grew upward");
Else
Puts ("stack grew downward");
return 0;
}
The method of using function recursion for find_stack_direction function
The first entry, because addr is null, assigns the address of the character variable dummy to the static variable addr
The second entry, because the static variable addr has been assigned a value, so enter "Second entry."
Next, compare the dummy address of the second entry to the dummy address of the first entry
If the value is positive, the stack grows to a high address, otherwise the stack grows to a low address
Ext.: http://blog.csdn.net/woxiaozhi/article/details/6891356
Big-endian/small-end is Big-endian/little-endian problem
Big-endian format:
In this format, the high byte of the character data is stored in the low address, while the low byte of the word data is placed in the high address.
Small End Format:
In contrast to the big-endian storage format, in the small-end storage format, the low-level address holds the low byte of the character data, and the high address holds the high byte of the character data.
Please write a C function, if the processor is Big_endian, then return 0, if Little_endian, then return 1
Answer:
int Checkcpu ()
{
{
Union W
{
int A;
Char b;
C
C.A = 1;
Return (c.b ==1);
}
}
Analysis:
Embedded system developers should be well aware of the Little-endian and Big-endian models. For example, the 16bit wide number of 0x1234 in Little-endian mode CPU memory (assuming that it is stored from the address 0x4000) is as follows:
Memory address |
0x4000 |
0x4001 |
Store content |
0x34 |
0x12 |
In Big-endian mode, the CPU memory is stored in the following way:
Memory address |
0x4000 |
0x4001 |
Store content |
0x12 |
0x34 |
The 32bit wide number of 0x12345678 in Little-endian mode CPU memory (assuming that it is stored from the address 0x4000) is as follows:
Memory address |
0x4000 |
0x4001 |
0x4002 |
0x4003 |
Store content |
0x78 |
0x56 |
0x34 |
0x12 |
In Big-endian mode, the CPU memory is stored in the following way:
Memory address |
0x4000 |
0x4001 |
0x4002 |
0x4003 |
Store content |
0x12 |
0x34 |
0x56 |
0x78 |
The Union union is stored in the order that all members are stored from the low address.
Here's another piece of code, excerpted from an open source project
int Big_endian (void)
{
union{
Long L;
Char c[sizeof (long)];
}u;
U.L = 1;
Return (u.c[sizeof (long)-1] = = 1);
}
Sometimes, you need to know whether to write programs in C is big-endian or small-end mode. The so-called big-endian mode is that the low data is kept in the high address of the memory, and the data is kept in the low address of the memory, and the so-called small-end mode means that the low-level data is kept in the lower address of the memory, while the high position of the data is kept in the higher address of memory. Why do you have the size and end mode of the points? This is because in the computer system, we are in bytes, each address unit corresponds to a byte, one byte is 8bit. But in the C language in addition to 8bit of Char, there are 16bit short type, 32bit long (to see the specific compiler), in addition, for the number of bits greater than 8 bits of the processor, such as 16-bit or 32-bit processor, because the register width is greater than one byte, Then there must be a problem if multiple bytes are scheduled. The result is a big-endian storage mode and a small-end storage mode. For example, a 16bit short x, where the value of address 0x0010,x in memory is 0x1122, then 0x11 is a high byte and 0x22 is a low byte. For big-endian mode, put 0x11 in the low address, that is, 0x0010, 0x22 placed in the high address, that is, 0x0011. Small-end mode, just the opposite. The X86 structure we commonly use is the small-end mode, while the Keil C51 is the big-endian mode. Many of the arm,dsp are in the small-end mode. Some arm processors can also be hardware to choose between big-endian or small-end mode.
The following code can be used to test whether your compiler is big-endian or small-end mode: short int x;
Char x0,x1;
x=0x1122;
X0= ((char*) &x) [0]; Low address unit
X1= ((char*) &x) [1]; High address Unit
If x0=0x11, it is big-endian; If x0=0x22, it is the small end
Stack growth direction and big-endian/small-end issues