Basic Principles: Memory Organization

Similar documents
Sources: On the Web: Slides will be available on:

The programming language C. sws1 1

The C Programming Language course syllabus associate level

Illustration 1: Diagram of program function and data flow

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

1 Abstract Data Types Information Hiding

System Calls Related to File Manipulation

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

How To Write Portable Programs In C

System Calls and Standard I/O

C Interview Questions

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

C++ Programming Language

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Chapter 13 Storage classes

Informatica e Sistemi in Tempo Reale

Stacks. Linear data structures

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Operating Systems. Privileged Instructions

Next, the driver runs the C compiler (cc1), which translates main.i into an ASCII assembly language file main.s.

/* File: blkcopy.c. size_t n

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Tutorial on C Language Programming

C++ INTERVIEW QUESTIONS

1 The Java Virtual Machine

Habanero Extreme Scale Software Research Project

Motorola 8- and 16-bit Embedded Application Binary Interface (M8/16EABI)

public static void main(string[] args) { System.out.println("hello, world"); } }

Instruction Set Architecture (ISA)

CP Lab 2: Writing programs for simple arithmetic problems

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Leak Check Version 2.1 for Linux TM

Java Interview Questions and Answers

Simple C Programs. Goals for this Lecture. Help you learn about:

Keil C51 Cross Compiler

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Caml Virtual Machine File & data formats Document version: 1.4

8.5. <summary> Cppcheck addons Using Cppcheck addons Where to find some Cppcheck addons

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

Introduction to Java

Semester Review. CSC 301, Fall 2015

Chapter 7D The Java Virtual Machine

ECS 165B: Database System Implementa6on Lecture 2

Lecture 22: C Programming 4 Embedded Systems

6.s096. Introduction to C and C++

1 Posix API vs Windows API

Software Vulnerabilities

CSC 2405: Computer Systems II

MatrixSSL Porting Guide

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

DNA Data and Program Representation. Alexandre David

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Object Oriented Software Design II

Crash Course in Java

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

CSC230 Getting Starting in C. Tyler Bletsch

ELEC 377. Operating Systems. Week 1 Class 3

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Chapter 2: Elements of Java

64-Bit NASM Notes. Invoking 64-Bit NASM

RTI Monitoring Library Getting Started Guide

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Operating System Structures

Glossary of Object Oriented Terms

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Overview. CISC Developments. RISC Designs. CISC Designs. VAX: Addressing Modes. Digital VAX

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

ADL User Guide for Open AT V4.10

X86-64 Architecture Guide

Compiler Construction

Parallel and Distributed Computing Programming Assignment 1

Application Note. Introduction AN2471/D 3/2003. PC Master Software Communication Protocol Specification

Module 816. File Management in C. M. Campbell 1993 Deakin University

ELEC3730 Embedded Systems Lecture 1: Introduction and C Essentials

Jonathan Worthington Scarborough Linux User Group

Stack Allocation. Run-Time Data Structures. Static Structures

Moving from CS 61A Scheme to CS 61B Java

Interface and Simulation of a LCD Text Display

Jorix kernel: real-time scheduling

Virtual Servers. Virtual machines. Virtualization. Design of IBM s VM. Virtual machine systems can give everyone the OS (and hardware) that they want.

DESIGN: A Program to Create Data Entry Programs

An Overview of Java. overview-1

What is COM/DCOM. Distributed Object Systems 4 COM/DCOM. COM vs Corba 1. COM vs. Corba 2. Multiple inheritance vs multiple interfaces

Client-server Sockets

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Computer Programming Tutorial

C Compiler Targeting the Java Virtual Machine

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

CSCE 665: Lab Basics. Virtual Machine. Guofei Gu

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Java programming for C/C++ developers

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

4D Plugin SDK v11. Another minor change, real values on 10 bytes is no longer supported.

Transcription:

Basic Principles: Memory Organization Main Memory is : an ARRAY OF BYTES Addresses: 0 to 0 xffffffff (machine dependent) A Pointer is : AN ADDRESS IN MEMORY Data is : contents of some area in memory. Code is : contents of some area in memory. (Virtual) MEMORY MAP of a process

Basic Principles: Stack (Function call) STACK: a region of memory. Storage for: Local variables Function arguments Return address Calling convention: language dependent. For C: push argument VALUE from right to left. Example: f(a, b, c); f locals temp SP ret addr a high address b c

Basic Principles: CPU Registers CPU contains some registers: Program counter ("pointer") General purpose registers (used as data or pointers) Internal status/control registers Structure of registers: very machine dependent (more in architecture course). Structure of memory: (almost) machine independent (as seen from most HLL) C programming language: allows easy access to "raw" memory.

Basic Principles: Programs Computer programs ( compiled code): 1 Start as high/level language like C. 2 Run compiler to generate binary OBJECT CODE file. 3 Linkage editor ("linker") combines OBJECT and LIBRARY files to create binary EXECUTABLE FILE. 4 Excutable loaded/mapped into main memory by PROGRAM LOADER and can then run. 5 Additions: dynamic linking/loading. Operating system provides SYSTEM SERVICES to a running program through SYSTEM CALLS.

Basic Principles: Operating System NOT same as window manager and/or command interpreter. Command interpreter is a USER program, can be either: 1 Command/line interpreter. 2 Windows point/and click interpreter. Operating system provides basic services: 1 Process scheduling 2 Memory management 3 Communications 4 File system 5 Other device access In UNIX/LINUX most things visible as "files".

C Programming Language 1 Basics. 2 Main differences from Java. 3 Data and storage types, pointers and structures. 4 Functions. 5 Input/output in C (or lack of).

C Language / Basics Simple example program blah.c: #include <stdio.h> int i = 1; main() { printf ("%d There is no magic\n", i); } Includes: Preprocessor commands (Global) Declarations Functions

C Language / Basics Compiling: gcc blah.c Creates (eventually) an executable file. (called "a.out" by default). To learn on your own: command flags (e.g. /o). Steps: 1 Preprocessor+2/pass compiler 2 Linker (link with C stdlib+init) For better control over multiple program files (re/learn on your own): make files

C vs. Java 1 Compiled, not interpreted. 2 Useful pre/processor. 3 No "magic" objects. 4 No garbage collection (explicit malloc / free). 5 WEAK type system. 6 Can access (almost) anything using POINTERS. 7 Very simple semantics (direct translation, very efficient). 8 No IO as part of language (!)

C Data and Storage Types Basic data types: (define before use) int x; char y; unsigned char c; float BloodyLongVarName ; double Whatever ; char * p; Structure definitions and typedefs: typedef struct element { struct element *next; int ID; char name [NAME_LENGTH]; } element; element my_element, elements[4]; my_element.id = 666; elements[0].next = &my_element;

C Data and Storage Types: Storage C storage types: Global variables: Define outside functions. Constant memory address. Names used across files. Local variables: Define in functions (at beginning, not middle!). Allocated on stack. Static variables (NOT like Java) Heap (dynamic) storage: Allocated by library functions system calls. and

C Data and Storage Types: Pointers Pointers: contain a MEMORY address. Definitions: char *p; /* Pointer to char */ char (*f)(); /* Pointer to function returning char */ int *f(); /* Function returning pointer to int */ On 80X86/LINUX: 32 bit number Access through a pointer: *p = 3; Next/>ID = 8; (*f)(); Operations on pointers: if(p == q) { exit(0)}; p = p + 1; /* increment by size of... */ "Address of" operator: f = &main; p = &c;

C Data and Storage Types: Pointers and Casting Consider: int i=2; char c = 5; float num; "Automatic" conversion: i = c; Forcing conversion casting: i =((float)i)/5 * c; Especially used for pointers: p = (element *) malloc (sizeof (element)); Can be used to (deliberately) "cheat": i = num; i = *((int *)(& num)); Or even: i = *((int *)(&main));

C Data and Storage Types: "Strings" Strings are NOT a true C data type. Implemented as: array of char. char my_ str[] = "There is no magic"; Convention: NULL TERMINATED string. (NULL is 0). Convention used in most standard library functions, such as open, strcmp, etc. IMPORTANT: "char"s are simply short, 1 byte integers. They can REPRESENT characters if we so wish, using, e.g. ASCII (the default), or ANY OTHER representation.

Functions in C All code in C is in some function. (But one CAN "cheat"...) main() is the function called by INIT after program is loaded. A function receives arguments BY VALUE, and (possibly) returns one value. Function PROTOTYPE: void main(int ac, char *av[]); Types LOOSELY checked. Functions definition is FLAT.

Functions in C Arguments to function: foo(a, b, c); Pushes COPY OF VALUES onto stack starting with rightmost. Called function can access LESS: foo(int a, int b) { a=5; /* Changes LOCAL copy */ return(a+b); } Works perfectly OK if it passes the compiler (e.g. in different files). Variable definitions at BEGINNING of function! foo() { int x, y=4; /* local variables (stack) */ static int z=3; /* single storage inst */ < function code> }

(Lack of) I/O in C C language has NO defs. for I/O Use: system calls + stdlib funcs. (stdlib functions use system calls) int fd, size, count, mode; char buf[buf_size]; fd = open("filename", flags, mode); size = read( fd, buf_addr, count); close( fd); Default file descriptors (UNIX): 0: standard input 1: standard output 2: standard error Also available / stream functions: FILE *f; f = fopen ("filename ", "rw"); printf ("Debug: just before crash?"); fflush(stdout); By default, BUFFERED IO.