In architectures where the call stack grows in memory from high memory address to low memory address, suppose the stack pointer is at a higher memory address Y initially, and after an integer argument is saved onto it, the stack pointer goes to a lower memory address X. Then would the first byte of the integer argument be at X(lower address), or at Y(higher address)?
-
1The endianness of the data is usually not depending on the stack growth direction. But no one would guarantee it unless it is standardized. But it is not. – Eugene Sh. Aug 30 '16 at 15:16
-
@EugeneSh. I agree, endianess is not relevant to it (edited my question). What I mean is, would the first byte be at the new position of the stack pointer (i.e. X), or at the earlier position of the stack pointer (i.e, Y)? – Meathead Aug 30 '16 at 15:19
-
What is "first byte of the integer"? – chux - Reinstate Monica Aug 30 '16 at 15:25
-
@chux What I mean is, would the integer start at Y (higher address) and end at X, or it would start at X (Lower address) and end at Y? Or put another way, would the first byte of the integer be at Y and 4th byte at X, or it would be the other way round? – Meathead Aug 30 '16 at 15:27
-
@chux: if I understand it correctly, it is the byte of the integer that has the lowest address. In other words, in big endian it is the top byte, in littel endian it is the bottom byte. He actually wants to know if the stack pointer is decremented before storing the integer or if it is decremented afterward. The answer is of course: it depends on the platform. – Rudy Velthuis Aug 30 '16 at 15:31
-
@RudyVelthuis You are very close. What I mean is, does the integer begin at the new position of the SP (from lower to higher address), or it ends at the new position of the SP (after starting from the older position of the SP at higher address) – Meathead Aug 30 '16 at 15:36
-
@Meathead: It either is at the address SP points to, or SP points **below** the entire integer. Which it is depends on the platform. – Rudy Velthuis Aug 30 '16 at 15:50
-
Stack frame pointers usually points to the last stacked data, but this is not a requirement. So in usual cases your int is stacked [SP, SP+sizeof(int)-1] and has address SP just after its allocation. – Jean-Baptiste Yunès Aug 30 '16 at 16:40
-
See https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/stack.html or http://www.cs.uwm.edu/classes/cs315/Bacon/Lecture/HTML/ch10s07.html – Jean-Baptiste Yunès Aug 30 '16 at 16:43
-
@Jean-BaptisteYunès That's a very helpful link. Incidentally I was looking at it some minutes back on my own as the Wikipedia article on call stack references that page. – Meathead Aug 30 '16 at 16:47
4 Answers
Endianness is completely irrelevant, why is that even being mentioned.
Why didnt you just try it and find out? Of course it is compiler/calling convention specific, but using the compiler you care about easy to figure out.
void more_fun ( unsigned int, unsigned int );
void fun
(
unsigned int a,
unsigned int b,
unsigned int c,
unsigned int d,
unsigned int e,
unsigned int f
)
{
more_fun(e,f);
}
00000000 <fun>:
0: 8fa50014 lw a1,20(sp)
4: 8fa40010 lw a0,16(sp)
8: 08000000 j 0 <fun>
c: 00000000 nop

- 69,149
- 8
- 89
- 168
Assume an integer and register are both 4 bytes in size (on a certain platform). Then there are two possible scenarios:
The integer is stored at the SP and then the SP is decremented:
+---+ | | + + | i | + n + the integer that was pushed | t | + + . | | X . +---+ . | u | . + n + . | u | . + s + unused (yet) . | e | v + d + SP -> | | Y +---+
The SP is decremented and then the integer is stored at the SP:
+---+ | | + + | ? | + ? + previous push | ? | + + . | | X . +---+ . | | . + + . | i | . + n + the integer that was pushed . | t | v + + SP -> | | Y +---+
Which of these possible ways is used depends on the platform on which the code runs.
Note that according to the standard, a stack is not necessary. It is just an implementation detail of most C (and other language) compilers.

- 28,387
- 5
- 46
- 94
Endianess and stack growth direction aren't related. If you store a 32 bit integer on the stack, it will be 4 bytes stored according to endianess. The stack/stack pointer doesn't know/care about what's stored there, nor does the number itself know/care about that it happens to be allocated on the stack.
So in case of big endian, the MS byte will always be stored at the lowest address. No matter where you allocate it in memory.

- 195,001
- 40
- 254
- 396
-
1Please look into my earlier comment. I removed the endianness part. Can you please answer now. – Meathead Aug 30 '16 at 15:20
-
2
-
I am unable to put it in words...what I mean is, does the integer begin at the initial position of the stack pointer and end at the new position of the stack pointer, or does it start at the new position of the SP and end at the earlier position? – Meathead Aug 30 '16 at 15:30
Your comment is still asking about endian which is dependent on the processor (and the compiler if the processor goes both ways.) For MIPS, seee this and this - it is bi-endian.
Like @Rudy, it appears you want to know if 2 int
s: A
and B
have a defined order in their address location. The C specificaiton not dictate it. It would depend on the compiler and options used - something not given in the post.

- 1
- 1

- 143,097
- 13
- 135
- 256