Stack Discipline, Lecture Slide - Computer Science, Slides of Computers and Information technologies

Process Memory Model, IA32 stack organization, Register Saving conventions, Before and After main(), Project

Typology: Slides

2010/2011

Uploaded on 10/07/2011

rolla45
rolla45 🇺🇸

4

(6)

133 documents

1 / 46

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
115-410, F’11
Stack Discipline
Aug. 31, 2011
Dave Eckhardt
Dave Eckhardt
Slides originally stolen from 15-213
Slides originally stolen from 15-213
15-410
“An Experience Like No Other”
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e

Partial preview of the text

Download Stack Discipline, Lecture Slide - Computer Science and more Slides Computers and Information technologies in PDF only on Docsity!

Stack Discipline

Aug. 31, 2011

Dave Eckhardt Dave Eckhardt

Slides originally stolen from 15-213 Slides originally stolen from 15-

“An Experience Like No Other”

Synchronization

Registration Registration

 If you're here but not registered, see me before noon

 The wait list will probably be done today or tomorrow

 (^) If I have asked you for data, please provide it!

If you haven't taken 15-213 (A/B, malloc lab ok) If you haven't taken 15-213 (A/B, malloc lab ok)

 Contact me no later than today

Mid-Term Exam Mid-Term Exam

 Two plausible days

 When I ask you to fill out the web form, please do so promptly

Why Only 32?

You may have learned x86-64 aka EMT64 aka AMD64 You may have learned x86-64 aka EMT64 aka AMD

 x86-64 is simpler than x86(-32) for user program code

 (^) Lots of registers, registers more orthogonal

Why will 410 be x86 / IA32? Why will 410 be x86 / IA32?

 x86-64 is not simpler for kernel code

 (^) Machine begins in 16-bit mode, then 32, finally 64 » You don't have time to write 3264 transition code » If we gave it to you, it would be a^ big^ black box

 x86-64 is not simpler during debugging

 (^) More registers means more registers to have wrong values

 x86-64 virtual memory is a bit of a drag

 (^) More steps than x86-32, but not more intellectually stimulating

 There are still a lot of 32-bit machines in the world

 (^) ...which can boot and run your personal OS

Private Address Spaces

Each process has its own private address space. Each process has its own private address space.

kernel virtual memory (code, data, heap, stack) memory mapped region for shared libraries run-time heap (managed by malloc) user stack (created at runtime) unused 0 %esp (stack pointer) memory invisible to user code brk 0xc 0x 0x read/write segments (.data, .bss) read-only segments (.init, .text, .rodata) loaded from the executable file 0xffffffff

Warning: Warning:

numbers numbers

specific to specific to

Linux 2.x Linux 2.x

on IA32!! on IA32!!

Warning: Warning:

details vary details vary

by OS and by OS and

kernel kernel

version! version!

Linux Memory Allocation

Linked

BF

7F

3F

Stack Libraries Text Data 08

Some

Heap

BF

7F

3F

Stack Libraries Text Data Heap 08

More

Heap

BF

7F

3F

Stack Libraries Text Data Heap Heap 08

Initially

BF

7F

3F

Stack Text Data 08

IA32 Stack

 Region of memory managed

with stack discipline

 “Grows” toward lower

addresses

 Register %esp indicates

lowest stack address

 (^) address of “top” element  (^) stack pointer Stack Pointer %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom”

IA32 Stack Popping

Popping Popping

 popl Dest

 Read memory at address

given by %esp

 Increment %esp by 4

 Store into Dest operand

Stack Pointer %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” +

%esp %eax %edx %esp %eax %edx %esp %eax %edx 0x 555 0x 0x 0x10c 0x 0x 555

Stack Operation Examples

0x 0x10c 0x 555

0x108 0x pushl %eax 0x 0x10c 0x 213

0x

popl %edx 0x

%esp %eip %esp %eip 0x804854e 0x 0x 0x10c 0x 0x 0x804854e 0x

Procedure Call Example

0x 0x10c 0x 123 0x call 8048b 804854e: e8 3d 06 00 00 call 8048b90

8048553: 50 pushl %eax 0x8048b 0x %eip is program counter

%esp %eip 0x %esp 0x8048591 %eip 0x 0x104 0x 0x 0x10c 0x 0x

Procedure Return Example

0x 0x10c 0x 123 ret 8048591: c3 ret 0x %eip is program counter 0x 0x

Call Chain Example

Code Structure Code Structure

yoo(…) {

who();

} who(…) {

  • • • amI();
  • • • amI();
  • • • } amI(…) {

amI();

} yoo who amI amI amI

Call Chain

 Procedure amI()

recursive

amI

Stack Pointer %esp yoo who proc Frame Pointer %ebp Stack “Top”

Stack Frames

Contents Contents

 Local variables

 Return information

 Temporary space

Management Management

 Space allocated when enter

procedure

 (^) “Set-up” code

 Deallocated when return

 (^) “Finish” code

Pointers Pointers

 Stack pointer %esp indicates

stack top

 Frame pointer %ebp indicates

start of current frame

amI

swap()

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } int zip1 = 15213; int zip2 = 91125; void call_swap() { swap(&zip1, &zip2); } call_swap:

  • • • pushl $zip2 # Global var pushl $zip1 # Global var call swap
  • • • &zip &zip Rtn adr %esp

Resulting

Stack

Calling swap from call_swap

swap()

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Body Set Up Finish Core