teh-cmc / go-internals

A book about the internals of the Go programming language.
Other
7.83k stars 352 forks source link

Clarify "top of the stack" #36

Open vedantroy opened 4 years ago

vedantroy commented 4 years ago

It would be helpful to use different term than "top" to refer to the first "element" in the stack that would be popped off. Maybe, tip or head?

Here's a sentence that felt confusing:

"".b+12(SP) and "".a+8(SP) respectively refer to the addresses 12 bytes and 8 bytes below the top of the stack (remember: it grows downwards!).

From this sentence, I thought the stack looked like as follows (here the stack is growing downwards):

RETURN ADDRESS 0(SP)
Argument A 8(SP)
Argument B 12(SP)

However, the diagram in the book is as follows:

   |    +-------------------------+ <-- 32(SP)              
   |    |                         |                         
 G |    |                         |                         
 R |    |                         |                         
 O |    | main.main's saved       |                         
 W |    |     frame-pointer (BP)  |                         
 S |    |-------------------------| <-- 24(SP)              
   |    |      [alignment]        |                         
 D |    | "".~r3 (bool) = 1/true  | <-- 21(SP)              
 O |    |-------------------------| <-- 20(SP)              
 W |    |                         |                         
 N |    | "".~r2 (int32) = 42     |                         
 W |    |-------------------------| <-- 16(SP)              
 A |    |                         |                         
 R |    | "".b (int32) = 32       |                         
 D |    |-------------------------| <-- 12(SP)              
 S |    |                         |                         
   |    | "".a (int32) = 10       |                         
   |    |-------------------------| <-- 8(SP)               
   |    |                         |                         
   |    |                         |                         
   |    |                         |                         
 \ | /  | return address to       |                         
  \|/   |     main.main + 0x30    |                         
   -    +-------------------------+ <-- 0(SP) (TOP OF STACK)

Looking at this diagram I see that the top of the stack refers to the bottom of the stack, but it might be nice to use different terminology.