0% found this document useful (0 votes)
2 views

Lecture Slides 05 051-Procstacks

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture Slides 05 051-Procstacks

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

University of Washington

Roadmap Memory & data


Integers & floats
Machine code & C
C: Java:
x86 assembly
car *c = malloc(sizeof(car)); Car c = new Car(); Procedures & stacks
c->miles = 100; c.setMiles(100);
Arrays & structs
c->gals = 17; c.setGals(17);
float mpg = get_mpg(c); float mpg =
Memory & caches
free(c); c.getMPG(); Processes
Virtual memory
Assembly get_mpg: Memory allocation
language: pushq %rbp Java vs. C
movq %rsp, %rbp
...
popq %rbp
ret
OS:
Machine 0111010000011000
100011010000010000000010
code: 1000100111000010
110000011111101000011111

Computer
system:

Procedures and Stacks


University of Washington

Section 5: Procedures & Stacks


 Stacks in memory and stack operations
 The stack used to keep track of procedure calls
 Return addresses and return values
 Stack-based languages
 The Linux stack frame
 Passing arguments on the stack
 Allocating local variables on the stack
 Register-saving conventions
 Procedures and stacks on x64 architecture

Procedure Calls
University of Washington

Memory Layout
2N-1
local variables;
Stack procedure context

Dynamic Data variables allocated with


new or malloc
(Heap)
static variables
Static Data (including global variables (C))

Literals literals (e.g., “example”)

Instructions
0
Procedures and Stacks
University of Washington

Memory Layout
Managed “automatically”
writable; not executable Stack (by compiler)

writable; not executable Dynamic Data Managed by programmer


(Heap)

writable; not executable Static Data Initialized when process starts

Read-only; not executable Literals Initialized when process starts

Read-only; executable Instructions Initialized when process starts

Procedures and Stacks


University of Washington

IA32 Call Stack


 Region of memory managed Stack “Bottom”
with a stack “discipline”
 Grows toward lower addresses
Increasing
 Customarily shown “upside-down” Addresses

 Register %esp contains


lowest stack address
= address of “top” element
Stack Grows
Down
Stack Pointer: %esp

Stack “Top”
Procedures and Stacks
University of Washington

IA32 Call Stack: Push


Stack “Bottom”
 pushl Src

Increasing
Addresses

Stack Grows
Down
Stack Pointer: %esp

Stack “Top”

Procedures and Stacks


University of Washington

IA32 Call Stack: Push


Stack “Bottom”
 pushl Src
 Fetch value from Src
 Decrement %esp by 4 (why 4?) Increasing
Addresses
 Store value at address
given by %esp

Stack Grows
Down
-4
Stack Pointer: %esp

Stack “Top”
Procedures and Stacks
University of Washington

IA32 Call Stack: Pop


Stack “Bottom”
 popl Dest

Increasing
Addresses

Stack Grows
Down
Stack Pointer: %esp

Stack “Top”
Procedures and Stacks
University of Washington

IA32 Call Stack: Pop


Stack “Bottom”
 popl Dest
 Load value from address %esp
 Write value to Dest Increasing
Addresses
 Increment %esp by 4

Stack Grows
Stack Pointer: %esp Down
+4

Stack “Top”

Procedures and Stacks

You might also like