Stack Discipline Sep. 01, 2004 - PowerPoint PPT Presentation

About This Presentation
Title:

Stack Discipline Sep. 01, 2004

Description:

(code, data, heap, stack) memory mapped region for. shared libraries. run-time heap ... Heap. Dynamically allocated storage. When call malloc, calloc, new ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 42
Provided by: csC76
Learn more at: http://www.cs.cmu.edu
Category:
Tags: discipline | heap | sep | stack

less

Transcript and Presenter's Notes

Title: Stack Discipline Sep. 01, 2004


1
Stack DisciplineSep. 01, 2004
15-410Way easier than when we were students
  • Lecture assembled by
  • Dave Eckhardt
  • Bruce Maggs
  • Review slides from 15-213 originally developed by
    Randy Bryant and Dave O'Halloran.

L02_Stack
2
Outline
  • Zoom-speed review
  • Process memory model
  • Linux memory model as an example, yours will be
    different
  • IA32 stack organization
  • You will need to understand this fully
  • Register saving conventions
  • You will need to understand this fully
  • New material
  • Before after main()
  • Project 0

3
(No Transcript)
4
(No Transcript)
5
(No Transcript)
6
(No Transcript)
7
(No Transcript)
8
(No Transcript)
9
(No Transcript)
10
Procedure Control Flow
  • Use stack to support procedure call and return
  • Procedure call
  • call label Push return address on stack Jump to
    label
  • Return address value
  • Address of instruction beyond call
  • Example from disassembly
  • 804854e e8 3d 06 00 00 call 8048b90 ltmaingt
  • 8048553 50 pushl eax
  • Return address 0x8048553
  • Procedure return
  • ret Pop address from stack Jump to address

11
(No Transcript)
12
(No Transcript)
13
Stack-Based Languages
  • Languages that Support Recursion
  • e.g., C, Pascal, Java
  • Code must be Reentrant
  • Multiple simultaneous instantiations of single
    procedure
  • Need some place to store state of each
    instantiation
  • Arguments
  • Local variables
  • Return pointer
  • Stack Discipline
  • State for given procedure needed for limited time
  • From when called to when return
  • Callee returns before caller does
  • Stack Allocated in Frames
  • state for single procedure instantiation

14
(No Transcript)
15
(No Transcript)
16
(No Transcript)
17
(No Transcript)
18
(No Transcript)
19
(No Transcript)
20
(No Transcript)
21
(No Transcript)
22
(No Transcript)
23
(No Transcript)
24
(No Transcript)
25
(No Transcript)
26
(No Transcript)
27
(No Transcript)
28
Register Saving Conventions
  • When procedure yoo calls who
  •  yoo is the caller, who is the callee
  • Can Register be Used for Temporary Storage?
  • Conventions
  • Caller Save
  • Caller saves temporary in its frame before
    calling
  • Callee Save
  • Callee saves temporary in its frame before using

29
(No Transcript)
30
Stack Summary
  • The Stack Makes Recursion Work
  • Private storage for each instance of procedure
    call
  • Instantiations don't clobber each other
  • Addressing of locals arguments can be relative
    to stack positions
  • Can be managed by stack discipline
  • Procedures return in inverse order of calls
  • IA32 Procedures Combination of Instructions
    Conventions
  • Call / Ret instructions
  • Register usage conventions
  • Caller / Callee save
  • ebp and esp
  • Stack frame organization conventions

31
Before After main()
  • int main(int argc, char argv)
  • if (argc gt 1)
  • printf(s\n, argv1)
  • else
  • char av3 0, 0, 0
  • av0 argv0 av1 Fred
  • execvp(av0, av)
  • return (1)

32
The Mysterious Parts
  • argc, argv
  • Strings from one program
  • Available while another program is running
  • Which part of the memory map are they in?
  • How did they get there?
  • What happens when main() does return(1) ?
  • There's no more program to run...right?
  • Where does the 1 go?
  • How does it get there?
  • 410 students should seek to abolish mystery

33
The Mysterious Parts
  • argc, argv
  • Strings from one program
  • Available while another program is running
  • Inter-process sharing/information transfer is
    OS's job
  • OS copies strings from old address space to new
    in exec()
  • Traditionally placed below bottom of stack

34
The Mysterious Parts
  • What happens when main() does return(1)???
  • Defined to have same effect as exit(1)
  • The main() wrapper
  • Receives argc, argv from OS
  • Calls main(), then calls exit()
  • Provided by C library, traditionally in crt0.s
  • Often has a strange name
  • / not actual code /
  • void main(int argc, char argv)
  • exit(main(argc, argv))

35
Some Mysteries Deferred
  • Who calls main()?
  • How does it get passed the address length of
    the vector?
  • (Who builds that very first stack frame?)
  • How can main() be found?
  • The code has an address somewhere in the
    program
  • How does exec() know where that address is?
  • These will all become clear in Project 3

36
Project 0 - Stack Crawler
  • C/Assembly function
  • Can be called by any C function
  • Prints stack frames in a symbolic way
  • ---Stack Trace Follows---
  • Function fun3(c'c', d2.090000d), in
  • Function fun2(f35.000000f), in
  • Function fun1(count0), in
  • Function fun1(count1), in
  • Function fun1(count2), in
  • Key questions
  • How do I know 0x80334720 is fun1?
  • How do I know fun3()'s second parameter is d?

37
Project 0 Data Flow
38
Project 0 Data Flow
39
Project 0 Data Flow
40
Project 0 Data Flow
41
Summary
  • Review of stack knowledge
  • What makes main() special
  • Project 0 overview
  • Start interviewing Project 2/3/4 partners!
Write a Comment
User Comments (0)
About PowerShow.com