Presentation is loading. Please wait.

Presentation is loading. Please wait.

Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.

Similar presentations


Presentation on theme: "Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing."— Presentation transcript:

1 Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing

2 Executable Program File on Disk ● The executable binary program file on disk contains two segments: Text – read only program code Data – initialized global and local variables Text Data

3 Program Running in Memory ● A program running in memory consists of 3-4 segments: ECSlow mem EDS ESS EES high mem Text Data Stack Heap

4 Program Running in Memory ● Text segment – read-only binary program code ● Data segment – Initialized global variables – Uninitialized global variables, cleared to zero when program starts ● Stack segment – stack for activation records of functions, containing local variables ● Heap segment – memory for dynamic allocation

5 Pages of the Program ● Each segment is shown as one contiguous piece of memory pointed to by a segment register (ECS, EDS, ESS, EES), but the memory management feature of the operating system might actually implement each segment as physically made up of several separated pages, each page typically 4KB in size. While the program is running, when memory is tight. not all the pages are present in memory, but are fetch from disk when needed.

6 Intel Base Registers ● The segments of the program running on an Intel box are pointed to by the base registers, and the actual position in the segment is indicated by an offset to the base register: – ECS: code segment register, EPC: pointer to actual code – EDS: data segment register, offset: pointer to actual data – ESS: stack segment register, ESP: pointer to top of stack, EBP: stack marker

7 Sharing of Segments ● When two or more instances of the same program are running, they may share several segments: – Text segment (code) – shared, read-only – Data segment – shared, “copy on write” – Stack segment – separate ● The heap is managed by the operating system and portions thereof are given out to programs upon their request for memory allocation.

8 Shared, “Copy on Write” Data Segment ● When two or more instances of the same program are running, they initially share a single data segment. Only when one instance attempts to write to the data segment is a copy made for it, which becomes an unshared exclusive copy for that instance.

9 Environment of a Program ● When a program is executed, it inherits the environment of the shell program that started it. The environment is a list of strings of environment variables and their values: – “PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin” – “MANPATH=/usr/man:/usr/X11/man:/var/man” – “logname=pmana” – “TERM=linux” ● The environment is part of the data segment of the program

10 Command Line Arguments ● When the command to execute a program is given to the shell, the command is issued with command-line arguments. The format of the command is: progname arg1 arg2 arg3... argN The entire command line string is made available to the running program in its data segment.

11 Program Initialization ● When a program starts executing, the program initialization code (c0.asm in Turbo C and crt*.s in GCC) creates an activation record on the stack for the main() function: – Push onto the stack the address of a null-terminated array of pointers to the environment strings – Push onto the stack the address of an array of pointers to the command-line arguments – Push onto the stack the number of command-line arguments – Then call the function main().

12 The Main Program ● When “main()” executes it must go through a “function entry protocol” in order to be able to access the values in the activation record on the stack that has been prepared for it. Main() sees these values as the values of the variables argc, argv, envv in its declaration: int main(int argc, char *argv[], char *envv[]) As main() exits, it must go though a “function exit protocol” to arrange the return of an “int” value and to clean up whatever changes it made to its activation record and to the registers.

13 Function Entry Protocol ● All functions go through the same function entry protocol for C programs. For example if the program is “int func(int x, int y, int z) { int a, b, c;... } then, the function entry protocol is: func:push %ebp mov %esp, %ebp sub $12, %esp...

14 Activation Record for func ● Before func is called to execution, the calling program has prepared an activation record for func by pushing the values of z, y, and x on the stack (reverse order), so that x is on top. ● When func executes, it goes through its function entry protocol, making z, y, and x available within func via EBP, and reserving space on the stack for a, b, and c

15 Activation Record for func ● The activation record now looks like z y x return addr Old EBP a b c EBP ESP 8(EBP) 12(EBP) 16(EBP) -4(EBP) -8(EBP) -12(EBP) low mem high mem

16 Function Exit Protocol ● As a function exits and returns to the caller, it must undo the changes that it made to the activation record and to the registers, and must arrange to return a value to the EAX register, if the return value is an int. The function exit protocol is: mov %eds:value, %eax mov %ebp, %esp pop %ebp ret

17 Function Body ● To access the arguments and local variables within func, their offsets with respect to EBP are used. For example the assignment, b = z + 5 translates to the assembly code: mov 16(%ebp), %eax add $15, %eax mov %eax, -8(%ebp)


Download ppt "Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing."

Similar presentations


Ads by Google