Download presentation
Presentation is loading. Please wait.
Published byRosa Spencer Modified over 6 years ago
1
ENERGY 211 / CME 211 Lecture 25 November 17, 2008
2
What a CPU does Processing of code and data obtained from main memory
Components: Registers: for storing data currently in use, and maintaining key addresses, such as the program counter, or PC (address of current instruction) ALU (Arithmetic-Logic Unit): performs actual operations on data in registers Instruction Decoder: determines from code what operations the ALU will perform MMU (Memory Management Unit): manages transfer of code and data between main memory, registers, and instruction decoder
3
Stacks and Registers A stack is a data structure that maintains a list of objects in a "last-in, first-out" manner (LIFO) Operations: push places an object on top of the stack, pop removes it Blocks of memory used by functions (called activation records) are maintained in a stack The record on top of the stack represents the function currently executing; next record is for the function that called it, etc. Location of top of the stack is maintained in a register called the stack pointer
4
Calling Functions When a function is called, its activation record is constructed and placed on top of a stack in the program's memory Record typically contains: values of arguments space for a return value space for local variables data needed to resume calling function after current function returns (such as PC value when function was called) When function exits, record is popped
5
Passing Arguments If arguments are passed by value, then values are copied to the activation record of the called function If arguments are passed by reference, then their addresses are copied, and code will dereference to access values C uses pass-by-value FORTRAN uses pass-by-reference C++ uses both C/C++ support variable length argument lists, where function call determines size of activation record at runtime, not compile time
6
Allocating on the Stack
Local variables inside functions are allocated on the program's stack, within function activation records Allocation involves moving the stack pointer by the size of the variable, to create space for the variable and push it onto the stack When function exits, stack pointer is moved back, popping the variable, which goes away Uninitialized variables contain whatever data was previously there (garbage!) Large arrays should not be allocated on the stack, because stack space is quite limited
7
Static Allocation Program's memory contains an area for variables that persist throughout a program's execution Examples: static or global variables Locations and allocations determined at compile time Global variables accessible to all functions in the program, but must be declared In one file, declare normally In other files that use it, precede declaration with extern keyword Otherwise, name conflict might result, causing strange behavior at linking time
8
Allocating on the Heap The heap is a portion of a program's memory reserved for dynamic allocations Heap space is limited, so repeated allocations without de-allocating unused space will eventually result in failed allocations If pointer allocated with new is a local variable, and is not deleted or assigned elsewhere before function exits, NO chance of deleting it later, causing a memory leak Unless such a pointer is needed elsewhere, use a smart pointer
9
Compilers, Linkers, Loaders
A compiler translates source code text (from .cpp files) into object code (.o files) Object code is relocatable so that it can be placed anywhere in main memory and work A linker assembles object code from several .o files into an executable file Using g++ or MDS's build function normally performs both compiling and linking A loader reads an executable file into main memory, so that code and data can be sent to the CPU for execution
10
Next Time More on Computer Organization Libraries
Interpreted languages Software Engineering Basics Software life-cycle Coding conventions
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.