The Machine Model Memory

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Basic Elements of C++ Chapter 2.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Lecture #5 Introduction to C++
Week 1 Algorithmization and Programming Languages.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Introduction to Programming
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Copyright © – Curt Hill Types What they do.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
By Mr. Muhammad Pervez Akhtar
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Expressions and Assignment Statements
A History Lesson Adapted from Chapter 1 in C++ for Java Programmers by Weiss and C for Java Programmers: a Primer by McDowell Development of language by.
Chapter 2 Variables.
CSE 220 – C Programming Bitwise Operators.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2: Introduction to C++
Chapter 12 Variables and Operators
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chap. 2. Types, Operators, and Expressions
Chapter 2 - Introduction to C Programming
Chapter 6: Data Types Lectures # 10.
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Object Oriented Programming
C Language VIVA Questions with Answers
Revision Lecture
Data types and variables
C Short Overview Lembit Jürimägi.
C Basics.
Chapter 2 - Introduction to C Programming
Programmazione I a.a. 2017/2018.
Basic Elements of C++ Chapter 2.
Chapter 12 Variables and Operators
Expressions and Assignment Statements
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Chapter 2: Introduction to C++.
Comp Org & Assembly Lang
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Chapter 2 Variables.
Programming Languages and Paradigms
C Language B. DHIVYA 17PCA140 II MCA.
Lecture 2: Bits, Bytes, Ints
Variables and Constants
SPL – PS1 Introduction to C++.
Chapter 12 Variables and Operators
Presentation transcript:

The Machine Model Memory A one dimensional array of individually addressable storage elements. Each element, called a byte, holds 8 binary digits (bits) of information. It is extremely important to understand and be able to distinguish the address of a storage element the contents of a storage element

The Machine Model Memory the address of a storage element Addresses begin at 0 and increase in unit steps to N-1 where N is the total number of bytes in the address space. A pointer variable holds an address. the content of a storage element Since each byte consists of only 8 bits, there are only 256 different values that can be contained in a storage element. These range from binary 00000000 to binary 11111111, which corresponds to decimal numbers 0 to 255.

The Machine Model Aggregation of basic memory elements More than 8 bits are needed for useful application to numerical problems. Thus it is common to group adjacent bytes into units commonly called words. Multiple word lengths are common, and common word lengths include 2 bytes, 4 bytes, and 8 bytes. In some architectures (Sun Sparc) it is required that the address of each word be a multiple of word length. That is, the only valid addresses of 4-byte words are 0, 4, 8, 12, 16, ...) .

C Programming Language Developed at AT&T Bell Labs in early 1970s Unix also developed at Bell Labs All but core of Unix is in C Standardized by American National Standards Institute (ANSI)

Introduction to C Procedural language Facilitates a structured and disciplined approach to computer program design Provides low-level access Highly portable

Structure of a C Program Preprocessor Directives Global Declarations int main ( ) { } // main Other functions as required Local Declarations Executable code Basic Block

C Program Structure The Basic Block - { declaration of variables executable code } Historically, unlike in Java and C++, all variable declarations must precede the first line of executable code within the block. With newer compilers this restriction may not be true, but in any case, scattering variable declarations throughout a program has adverse effects on the readability and maintainability of a program. Nesting of blocks is legal and common. Each interior block may include variable declarations.

Declaration of variables Two generic types of basic (unstructured) variables exist: integer floating point Integer variables may be declared as follows: char a; /* 8 bits */ short b; /* (usually) 16 bits */ int c; /* (usually) 32 bits */ long d; /* 32 or 64 bits */ long long e; /* (usually) 64 bits */

Declaration of variables These declarations implicitly create signed integers. An 8 bit signed integer can represent values in the range [-27,...0,....27-1] Signed integers are represented internally using 2's complement representation.

Declaration of variables Unsigned integers Each of the preceding declarations may also be preceded by the qualifier unsigned. unsigned char a; /* 8 bits */ unsigned short b; /* (usually) 16 bits */ An 8 bit unsigned integer can represent values in the range [0,....28-1] In all modern computer systems different hardware instructions are used for signed and unsigned arithmetic.

Encoding of integer constants: Integer constants may be expressed in several ways decimal number 65 hexadecimal number 0x41 octal number 0101 ASCII encoded character 'A' ALL of the above values are equivalent ways to represent the 8 bit byte whose value is: 01000001

Encoding of integer constants: Constants of different representation may be freely intermixed in expressions. x = 11 + 'b' - 055 + '\t'; x = 0xb + 0x62 - 0x2d + 0x9; x = 11 + 98 - 45 + 9; x = 73;

Floating point data Two variations on floating point variables float 32 bits double 64 bits Example float a, b; double c; Floating point constants can be expressed in two ways Decimal number 1024.123 Scientific notation 1.024123e+3 avogadro = 6.02214199e+23;

Executable code Expressions: consist of (legal combinations of): constants variables operators function calls Operators Arithmetic: +, -, *, /, % Comparative: ==, != , <, <=, >, >= Logical: !, &&, || Bitwise: &, |, ~, ^ Shift: <<, >>

Executable code Special types of expression A statement consists of an expression followed by a semicolon An assignment expression consists of lvalue = expression; lvalue is short for "left-value", which in turn represents any entity that may legitimately be assigned a value: The two most common examples are: A simple variable A pointer dereference

Executable code Warnings: Non-assignment statements (with the exception of function calls) are syntactically legal but (generally) semantically useless: x + y - 3; x <= 2; x == y;

Executable code Warnings: Use parentheses to avoid problems with: operator precedence (the order in which the operators are evaluated) y = x + 5 & z - 5; and operator associativity (the direction in which operations of equal precedence are evaluated) y = 10 / 5.0 * 4.0;

Executable code Control flow: if and while if (expression) statement | basic-block <--- Executed if expression value is true else statement | basic-block <--- Executed if expression value is false while (expression) statement | basic-block <--- Executed while expression remains true do <--- Executed until expression becomes false statement | basic block while (expression);

Executable code Control flow: if and while There is no boolean type in C. Integers are used for this purpose. An expression is false <=> its value is 0 If the expression is an assignment, the value of the expression is the value that is assigned. Be particularly careful not to confuse the following: if (x = (a + b)) if (x == (a + b)) Be careful not to accidentally use: while (x < y);

Executable code Function definitions A C function is comprised of 4 components the type of value returned by the function the name of the function parenthesized declaration of function parameters at least one basic block containing local variable declarations and executable code int main(int argc, char *argv[]) { --- basic block --- } /* argc - number of command line arguments */ /* argv - array of pointers to command line arguments */

Compiling and Running a Program To compile and print all warning messages, type gcc –Wall prog-name.c If using math library (math.h), type gcc –Wall prog-name.c -lm By default, the compiler produces the file a.out

Compiling and Running a Program To execute the program type ./a.out The ./ indicates the current directory To specify the filename for the object code, for example, p1.o, type gcc –Wall –o p1.o prog1.c then type ./p1.o to execute the program