cs24-20fa The Stack

Introduction to Computing Systems (Fall 2020)

hello

What is “The Stack”?

0xFFFFFFFFFFFF 
Kernel Memory
 
Stack
 
 
 
Heap
 
Data
 
Text
 
Reserved
0x000000000000 


func.c

__attribute__((noinline)) 
int callee(int x) {
    return x;
}

int caller(int x) {
    return callee(x) + 1;
}

\(\leftrightarrow\)

gcc func.c -S -O1


callee:
    mov    %edi, %eax
    retq
caller:
    call   callee
    add    $1, %eax
    retq


0000000000400497 <callee>:
  400497:	89 f8                	mov    %edi,%eax
  400499:	c3                   	retq
000000000040049a <caller>:
  40049a:	e8 f8 ff ff ff       	callq  400497 <callee>
  40049f:	83 c0 01             	add    $0x1,%eax
  4004a2:	c3                   	retq
 
%rdi 0x0000000000000007
%rax 0x00007fffffffdf28
 
Caller Stack Frame
 
Return Address to main
%rsp→ 
...garbage...
 
...garbage...
 
...garbage...
 
...garbage...
 
...garbage...
0000000000400497 <callee>:
  400497:	89 f8                	mov    %edi,%eax
  400499:	c3                   	retq
000000000040049a <caller>:
  40049a:	e8 f8 ff ff ff       	callq  400497 <callee>
  40049f:	83 c0 01             	add    $0x1,%eax
  4004a2:	c3                   	retq
 
%rdi 0x0000000000000007
%rax 0x0000000000000007
 
Caller Stack Frame
 
Return Address to main
 
Return Address to caller
%rsp→ 
...garbage...
 
...garbage...
 
...garbage...
 
...garbage...
0000000000400497 <callee>:
  400497:	89 f8                	mov    %edi,%eax
  400499:	c3                   	retq
000000000040049a <caller>:
  40049a:	e8 f8 ff ff ff       	callq  400497 <callee>
  40049f:	83 c0 01             	add    $0x1,%eax
  4004a2:	c3                   	retq
 
%rdi 0x0000000000000007
%rax 0x0000000000000007
 
Caller Stack Frame
 
Return Address to main
 
Return Address to caller
%rsp→ 
...garbage...
 
...garbage...
 
...garbage...
 
...garbage...
0000000000400497 <callee>:
  400497:	89 f8                	mov    %edi,%eax
  400499:	c3                   	retq
000000000040049a <caller>:
  40049a:	e8 f8 ff ff ff       	callq  400497 <callee>
  40049f:	83 c0 01             	add    $0x1,%eax
  4004a2:	c3                   	retq
 
%rdi 0x0000000000000007
%rax 0x0000000000000007
 
Caller Stack Frame
 
Return Address to main
%rsp→ 
...garbage...
 
...garbage...
 
...garbage...
 
...garbage...
 
...garbage...
0000000000400497 <callee>:
  400497:	89 f8                	mov    %edi,%eax
  400499:	c3                   	retq
000000000040049a <caller>:
  40049a:	e8 f8 ff ff ff       	callq  400497 <callee>
  40049f:	83 c0 01             	add    $0x1,%eax
  4004a2:	c3                   	retq
 
%rdi 0x0000000000000007
%rax 0x0000000000000008
 
Caller Stack Frame
 
Return Address to main
%rsp→ 
...garbage...
 
...garbage...
 
...garbage...
 
...garbage...
 
...garbage...


%rax 0x0000000000400527
%rdx 0x00007fffffffdf28
%rsi 0x000000000000000a
%rdi 0x0000000000000005
%rbp 0x00007fffffffde30
%rsp 0x00007fffffffde28
  0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF
0x00007fffffffde00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x00007fffffffde10 70 05 40 00 00 00 00 00 30 04 40 00 00 00 00 00
0x00007fffffffde20 10 df ff ff ff 7f 00 00 3a 05 40 00 00 00 00 00
0x00007fffffffde30 70 05 40 00 00 00 00 00 8a df a3 f7 ff 7f 00 00
0000000000400497 <callee>:
  400497:	55                   	push   %rbp
  400498:	48 89 e5             	mov    %rsp,%rbp
  40049b:	6b c7 64             	imul   $0x64,%edi,%eax
  40049e:	5d                   	pop    %rbp
  40049f:	c3                   	retq
00000000004004a0 <caller>:
  4004a0:	55                   	push   %rbp
  4004a1:	48 89 e5             	mov    %rsp,%rbp
  4004a4:	bf 00 00 00 00       	mov    $0x0,%edi
  4004a9:	e8 e9 ff ff ff       	callq  400497 <callee>
  4004ae:	5d                   	pop    %rbp
  4004af:	c3                   	retq
%rax 0x0000000000400527
%rdx 0x00007fffffffdf28
%rsi 0x000000000000000a
%rdi 0x0000000000000005
%rbp 0x00007fffffffde30
%rsp 0x00007fffffffde28
  0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF
0x00007fffffffde00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x00007fffffffde10 70 05 40 00 00 00 00 00 30 04 40 00 00 00 00 00
0x00007fffffffde20 10 df ff ff ff 7f 00 00 3a 05 40 00 00 00 00 00
0x00007fffffffde30 70 05 40 00 00 00 00 00 8a df a3 f7 ff 7f 00 00
0000000000400507 <gcd>:
  400507:	48 89 f8         mov    %rdi,%rax
  40050a:	48 85 f6         test   %rsi,%rsi
  40050d:	75 02            jne    400511 <gcd+0xa>
  40050f:	f3 c3            repz retq
  400511:	55               push   %rbp
  400512:	48 89 e5         mov    %rsp,%rbp
  400515:	48 89 f7         mov    %rsi,%rdi
  400518:	48 99            cqto
  40051a:	48 f7 fe         idiv   %rsi
  40051d:	48 89 d6         mov    %rdx,%rsi
  400520:	e8 e2 ff ff ff   callq  400507 <gcd>
  400525:	5d               pop    %rbp
  400526:	c3               retq
%rax 0x0000000000000005
%rdx 0x00007fffffffdf28
%rsi 0x000000000000000a
%rdi 0x0000000000000005
%rbp 0x00007fffffffde30
%rsp 0x00007fffffffde28
  0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF
0x00007fffffffde00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x00007fffffffde10 70 05 40 00 00 00 00 00 30 04 40 00 00 00 00 00
0x00007fffffffde20 10 df ff ff ff 7f 00 00 3a 05 40 00 00 00 00 00
0x00007fffffffde30 70 05 40 00 00 00 00 00 8a df a3 f7 ff 7f 00 00
0000000000400507 <gcd>:
  400507:	48 89 f8         mov    %rdi,%rax
  40050a:	48 85 f6         test   %rsi,%rsi
  40050d:	75 02            jne    400511 <gcd+0xa>
  40050f:	f3 c3            repz retq
  400511:	55               push   %rbp
  400512:	48 89 e5         mov    %rsp,%rbp
  400515:	48 89 f7         mov    %rsi,%rdi
  400518:	48 99            cqto
  40051a:	48 f7 fe         idiv   %rsi
  40051d:	48 89 d6         mov    %rdx,%rsi
  400520:	e8 e2 ff ff ff   callq  400507 <gcd>
  400525:	5d               pop    %rbp
  400526:	c3               retq
%rax 0x0000000000000005
%rdx 0x00007fffffffdf28
%rsi 0x000000000000000a
%rdi 0x0000000000000005
%rbp 0x00007fffffffde30
%rsp 0x00007fffffffde28
  0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF
0x00007fffffffde00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x00007fffffffde10 70 05 40 00 00 00 00 00 30 04 40 00 00 00 00 00
0x00007fffffffde20 10 df ff ff ff 7f 00 00 3a 05 40 00 00 00 00 00
0x00007fffffffde30 70 05 40 00 00 00 00 00 8a df a3 f7 ff 7f 00 00
0000000000400507 <gcd>:
  400507:	48 89 f8         mov    %rdi,%rax
  40050a:	48 85 f6         test   %rsi,%rsi
  40050d:	75 02            jne    400511 <gcd+0xa>
  40050f:	f3 c3            repz retq
  400511:	55               push   %rbp
  400512:	48 89 e5         mov    %rsp,%rbp
  400515:	48 89 f7         mov    %rsi,%rdi
  400518:	48 99            cqto
  40051a:	48 f7 fe         idiv   %rsi
  40051d:	48 89 d6         mov    %rdx,%rsi
  400520:	e8 e2 ff ff ff   callq  400507 <gcd>
  400525:	5d               pop    %rbp
  400526:	c3               retq
%rax 0x0000000000000005
%rdx 0x00007fffffffdf28
%rsi 0x000000000000000a
%rdi 0x0000000000000005
%rbp 0x00007fffffffde30
%rsp 0x00007fffffffde28
  0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF
0x00007fffffffde00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x00007fffffffde10 70 05 40 00 00 00 00 00 30 04 40 00 00 00 00 00
0x00007fffffffde20 10 df ff ff ff 7f 00 00 3a 05 40 00 00 00 00 00
0x00007fffffffde30 70 05 40 00 00 00 00 00 8a df a3 f7 ff 7f 00 00
0000000000400507 <gcd>:
  400507:	48 89 f8         mov    %rdi,%rax
  40050a:	48 85 f6         test   %rsi,%rsi
  40050d:	75 02            jne    400511 <gcd+0xa>
  40050f:	f3 c3            repz retq
  400511:	55               push   %rbp
  400512:	48 89 e5         mov    %rsp,%rbp
  400515:	48 89 f7         mov    %rsi,%rdi
  400518:	48 99            cqto
  40051a:	48 f7 fe         idiv   %rsi
  40051d:	48 89 d6         mov    %rdx,%rsi
  400520:	e8 e2 ff ff ff   callq  400507 <gcd>
  400525:	5d               pop    %rbp
  400526:	c3               retq
%rax 0x0000000000000005
%rdx 0x00007fffffffdf28
%rsi 0x000000000000000a
%rdi 0x0000000000000005
%rbp 0x00007fffffffde30
%rsp 0x00007fffffffde20
  0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF
0x00007fffffffde00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x00007fffffffde10 70 05 40 00 00 00 00 00 30 04 40 00 00 00 00 00
0x00007fffffffde20 30 de ff ff ff 7f 00 00 3a 05 40 00 00 00 00 00
0x00007fffffffde30 70 05 40 00 00 00 00 00 8a df a3 f7 ff 7f 00 00
0000000000400507 <gcd>:
  400507:	48 89 f8         mov    %rdi,%rax
  40050a:	48 85 f6         test   %rsi,%rsi
  40050d:	75 02            jne    400511 <gcd+0xa>
  40050f:	f3 c3            repz retq
  400511:	55               push   %rbp
  400512:	48 89 e5         mov    %rsp,%rbp
  400515:	48 89 f7         mov    %rsi,%rdi
  400518:	48 99            cqto
  40051a:	48 f7 fe         idiv   %rsi
  40051d:	48 89 d6         mov    %rdx,%rsi
  400520:	e8 e2 ff ff ff   callq  400507 <gcd>
  400525:	5d               pop    %rbp
  400526:	c3               retq
%rax 0x0000000000000005
%rdx 0x00007fffffffdf28
%rsi 0x000000000000000a
%rdi 0x0000000000000005
%rbp 0x00007fffffffde20
%rsp 0x00007fffffffde20
  0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF
0x00007fffffffde00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x00007fffffffde10 70 05 40 00 00 00 00 00 30 04 40 00 00 00 00 00
0x00007fffffffde20 30 de ff ff ff 7f 00 00 3a 05 40 00 00 00 00 00
0x00007fffffffde30 70 05 40 00 00 00 00 00 8a df a3 f7 ff 7f 00 00
0000000000400507 <gcd>:
  400507:	48 89 f8         mov    %rdi,%rax
  40050a:	48 85 f6         test   %rsi,%rsi
  40050d:	75 02            jne    400511 <gcd+0xa>
  40050f:	f3 c3            repz retq
  400511:	55               push   %rbp
  400512:	48 89 e5         mov    %rsp,%rbp
  400515:	48 89 f7         mov    %rsi,%rdi
  400518:	48 99            cqto
  40051a:	48 f7 fe         idiv   %rsi
  40051d:	48 89 d6         mov    %rdx,%rsi
  400520:	e8 e2 ff ff ff   callq  400507 <gcd>
  400525:	5d               pop    %rbp
  400526:	c3               retq
Submit QuickChecks For Credit