Programs in Memory Bryce Boe 2012/08/29 CS32, Summer 2012 B.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

Hand-Held Devices and Embedded Systems Course Student: Tomás Sánchez López Student ID:
Part IV: Memory Management
Chapter 10 Linking and Loading. Separate assembly creates “.mob” files.
Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
Program Development Tools The GNU (GNU’s Not Unix) Toolchain The GNU toolchain has played a vital role in the development of the Linux kernel, BSD, and.
Lecture 10: Linking and loading. Lecture 10 / Page 2AE4B33OSS 2011 Contents Linker vs. loader Linking the executable Libraries Loading executable ELF.
Linkers and Loaders 1 Linkers & Loaders – A Programmers Perspective.
Computer Organization CS224 Fall 2012 Lesson 12. Synchronization  Two processors or threads sharing an area of memory l P1 writes, then P2 reads l Data.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Generating Programs and Linking Professor Rick Han Department of Computer Science University of Colorado at Boulder.
An introduction to systems programming
MEMORY MANAGEMENT By KUNAL KADAKIA RISHIT SHAH. Memory Memory is a large array of words or bytes, each with its own address. It is a repository of quickly.
7/13/20151 Topic 3: Run-Time Environment Memory Model Activation Record Call Convention Storage Allocation Runtime Stack and Heap Garbage Collection.
Memory Layout C and Data Structures Baojian Hua
Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access memory.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Chapter 91 Memory Management Chapter 9   Review of process from source to executable (linking, loading, addressing)   General discussion of memory.
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
System Calls 1.
MIPS coding. SPIM Some links can be found such as:
H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It.
Assemblers.
Topic 2d High-Level languages and Systems Software
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
COMP3190: Principle of Programming Languages
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
First Compilation Rudra Dutta CSC Spring 2007, Section 001.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
CSc 453 Linking and Loading
CS252: Systems Programming Ninghui Li Based on Slides by Gustavo Rodriguez-Rivera Topic 2: Program Structure and Using GDB.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
Linking I Topics Assembly and symbol resolution Static linking Systems I.
Program Translation and Execution I: Linking Sept. 29, 1998 Topics object files linkers class11.ppt Introduction to Computer Systems.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Hello world !!! ASCII representation of hello.c.
Operating Systems A Biswas, Dept. of Information Technology.
Program Execution and ELF Files Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014 Abed Asi.
Object Files & Linking. Object Sections Compiled code store as object files – Linux : ELF : Extensible Linking Format – Windows : PE : Portable Execution.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
Program Execution in Linux David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
Compiling examples in MIPS
Programs – Preprocessing, Compilation and Linking
Lecture 3 Translation.
Computer Architecture & Operations I
Process concept.
Slides adapted from Bryant and O’Hallaron
Computer Systems MTSU CSCI 3240 Spring 2016 Dr. Hyrum D. Carroll
The University of Adelaide, School of Computer Science
Linking & Loading.
Separate Assembly allows a program to be built from modules rather than a single source file assembler linker source file.
Program Execution in Linux
Software Development with uMPS
Topic 2e High-Level languages and Systems Software
Assembler Design Options
Computer Organization & Compilation Process
Chapter 11 Introduction to Programming in C
Memory Allocation CS 217.
Chapter 11 Introduction to Programming in C
Memory Management Tasks
Programming Languages
Program Execution in Linux
Computer Organization & Compilation Process
An introduction to systems programming
Chapter 11 Class Inheritance
Program Assembly.
Chapter 7 Memory Management
Presentation transcript:

Programs in Memory Bryce Boe 2012/08/29 CS32, Summer 2012 B

Overview Project 2 Overview Inheritance and assignment operator Virtual Keyword / Polymorphism Abstract classes Compilation Process Programs on Disk and in Memory

Project 2: A semi-simple card game Turn-based card game where the goal is to eliminate other players by bringing their hp down to zero Resources (created only once at the start): – Cards Can have multiple instances of the same card – Player

Classes Game – Deals cards – Hands control to players in order Player (abstract) – Has two sets of cards (deck and discard) – Plays cards on other players (or themselves) Deck (of cards) – Simple AST for holding cards and shuffling Card – Can attack or heal other players

Relevant Card Interface virtual void perform_action(from, to, hp) – Called indirectly by player to perform the cards action on another player virtual void discard() – Called by player when Card is discarded virtual int get_hp() – Report the hp this card can attack (negative value) or heal (postive value) with

Relevant Player Interface virtual void take_turn(const Card& card); – Needs to determine who to play card on.

Your Task Add additional Cards – ReflectorCard Heals the attacker while performing the attack – RolloverHPCard Left over hp can be accumulated and used on later turns – SnowballCard Becomes stronger each time it is played

Implement Additional Players AttackWeakest – Always attack the weakest player ??? – Undetermined as of yet

Inheritance and Assignment Operator Often want to call parents assignment operator

Virtual Keyword Allows for late binding, aka dynamic dispatch Essentially Polymorphism – Associate many meanings to one function

Abstract Classes Classes can have purely virtual functions (no definition) A class with purely virtual functions are said to be abstract classes Cannot directly declare instances of abstract classes virtual void output() const = 0;

Programs on Disk and Memory

Program building Have: source code – human readable instructions Need: machine language program – binary instructions and associated data regions, ready to be executed clang/gcc does two basic steps: compile, then link – To compile means translate to object code – To link means to combine with other object code (including library code) into an executable program

Link combines object codes From multiple source files and/or libraries – e.g., always libc.a Use -c option with clang/gcc to stop after creating.o file -bash-4.1$ clang -c mypgm.c ; ls mypgm* mypgm.c mypgm.o – Is necessary to compile a file without a main function Later link it to libraries – alone or with other object files: -bash-4.1$ clang -o mypgm mypgm.o ; ls mypgm* mypgm mypgm.c mypgm.o

Compiling: 3 steps with C/C++ First the preprocessor runs – Creates temporary source code with text substitutions as directed – Use clang –E to run it alone – output goes to stdout Then the source is actually compiled to assembly code – Use clang -S to stop at this step and save code in.s file Last, assembler produces the object code (machine language)

Another View Usually performed by clang/clang++/gcc/g++ in one uninterrupted sequence

Layout of C/C++ programs Source code … becomes Object module

A sample C program – demo.c Has text section: the machine code Has initialized global data: a Uninitialized global data: b Static data: k Has a local variable: i #include int a[10]={0,1,2,3,4,5,6,7,8,9}; int b[10]; void main(){ int i; static int k = 3; for(i = 0; i < 10; i++) { printf("%d\n",a[i]); b[i] = k*a[i]; }

A possible structure of demo.o Object module contains neither uninitialized data ( b ), nor any local variables ( i )

Linux object file format ELF – stands for Executable and Linking Format – A 4-byte magic number followed by a series of named sections Addresses assume the object file is placed at memory address 0 – When multiple object files are linked together, we must update the offsets (relocation) Tools to read contents: objdump and readelf – not available on all systems \177ELF.text ….rodata ….data ….bss ….symtab ….rel.text ….rel.data ….debug ….line … Section header table

ELF sections.text = machine code (compiled program instructions).rodata = read-only data.data = initialized global variables.bss = block storage start for uninitialized global variables – actually just a placeholder that occupies no space in the object file.symtab = symbol table with information about functions and global variables defined and referenced in the program \177ELF.text ….rodata ….data ….bss ….symtab ….rel.text ….rel.data ….debug ….line … Section header table

ELF Sections (cont.).rel.text = list of locations in.text section that need to be modified when linked with other object files.rel.data = relocation information for global variables referenced but not defined.debug = debugging symbol table; only created if compiled with -g option.line = mapping between line numbers in source and machine code in.text; used by debugger programs \177ELF.text ….rodata ….data ….bss ….symtab ….rel.text ….rel.data ….debug ….line … Section header table

Creation of a load module Interleaved from multiple object modules –Sections must be relocated Addresses relative to beginning of a module –Necessary to translate from beginnings of object modules When loaded – OS will translate again to absolute addresses

Loading and memory mapping Includes memory for stack, dynamic data (i.e., free store), and un-initialized global data Physical memory is shared by multiple programs

Sections of an executable file Segments: