10-2 What is the difference between machine code and C? Why use C? How do execution of C and Matlab programs differ? What are three methods of expressing.

Slides:



Advertisements
Similar presentations
ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Advertisements

Lecture 1: Overview of Computers & Programming
Programming Types of Testing.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Starting Out with C++, 3 rd Edition 1 Chapter 1. Introduction to Computers and Programming.
COSC 120 Computer Programming
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
1 Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
1 Lecture 1  Getting ready to program  Hardware Model  Software Model  Programming Languages  The C Language  Software Engineering  Programming.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
Chapter 1: An Overview of Computers and Programming Languages
Ceng 230 Programming with C
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Chapter 1: Introduction To Computer | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 1 Introduction To Computers.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Computer Programming-1 CSC 111 Chapter 1 : Introduction.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
CCSA 221 Programming in C CHAPTER 2 SOME FUNDAMENTALS 1 ALHANOUF ALAMR.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Why Program? Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer.
Chapter Introduction to Computers and Programming 1.
CHAPTER 4: INTRODUCTION TO COMPUTER ORGANIZATION AND PROGRAMMING DESIGN Lec. Ghader Kurdi.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming 1.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
CISC105 General Computer Science Class 1 – 6/5/2006.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Programming With C.
Introduction to Computer Systems and the Java Programming Language.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 1 Introduction Chapter 1 Introduction 1 st Semester 2015 CSC 1101 Computer Programming-1.
What is Programming? A program is a list of instructions that is executed by a computer to accomplish a particular task. Creating those instructions is.
CHAPTER 1 INTRODUCTION 1 st Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
CHAPTER 1 INTRODUCTION 1 st semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
8-2 What is a program? What is a “Window Manager” ? What is a “GUI” ? How do you navigate the Unix directory tree? What is a wildcard? Readings: See CCSO’s.
CHAPTER 1 INTRODUCTION 2 nd Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
Chapter 1 Computers, Compilers, & Unix. Overview u Computer hardware u Unix u Computer Languages u Compilers.
A.Abhari CPS1251 Topic 1: Introduction to Computers Computer Hardware Computer components Connecting Computers Computer Software Operating System (OS)
Basic Computer Organization Rashedul Hasan.. Five basic operation No matter what shape, size, cost and speed of computer we are talking about, all computer.
Computer and Programming. Computer Basics: Outline Hardware and Memory Programs Programming Languages and Compilers.
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 1: Introduction to Computers and Programming.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
 Human language : commonly used to express feeling and understand what other people expression.  Computer language : are a languages by which a user.
Chapter 1: Introduction to Computers and Programming
Chapter 1 Introduction 2nd Semester H
Engineering Problem Solving With C An Object Based Approach
Lecture 1: Introduction to JAVA
ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Chapter 1. Introduction to Computers and Programming
Computer Electronic device Accepts data - input
CSCE Fall 2013 Prof. Jennifer L. Welch.
Programming Fundamentals (750113) Ch1. Problem Solving
King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1 Done By: Asmal Alosaimi Edited By: Fatimah Alakeel.
CSCE Fall 2012 Prof. Jennifer L. Welch.
King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1 Done By: Asmal Alosaimi Edited By: Fatimah Alakeel.
Basic Computer Organization
ICS103 Programming in C 1: Overview of Computers And Programming
Programming Logic and Design Eighth Edition
Presentation transcript:

10-2 What is the difference between machine code and C? Why use C? How do execution of C and Matlab programs differ? What are three methods of expressing a program algorithm? Related Chapters: ABC Chapters 0 & 1 & 11.3

Main Memory(RAM) Input Devices Output Devices Auxiliary Storage CPU Control Unit (CU) Arithmetic -Logic Unit (ALU) Memory Addresses instr1 instr2 -75 label System Unit

10-4 Main (Internal) Memory: All data and instructions are stored in main memory as a sequence of 0’s and 1’s called Bits (Binary Digits) Byte (smallest addressable element of memory) Storage size for one character of information (ASCII-8 8 bits). Every (single) address refers to a byte in memory bytes is defined as 1 kilo-byte (1KB) 2 20 bytes is defined as 1 mega-byte (1MB) 2 30 bytes is defined as 1 giga-byte (1GB)

10-5 Central Processing Unit (CPU): Transfers information into, out of, and between memory locations. Executes instructions stored in memory. Set of instructions for a CPU is known as a machine language. Each CPU (Intel Core i7, IBM PowerPC,...) has its own specific machine language.

Main Memory Control Unit Arithmetic/Logic Unit Instruction Cycle Execution Cycle FetchDecode Execute Store CPU RAM Includes Cache (very fast memory)

10-7 Classified as  Low Level Machine Language (binary-based code; machine dependent) Assembly Language (mnemonic form of machine language )

10-8  High Level Closer to natural languages. Generally, machine independent Usually, several machine instructions are combined into one high-level instruction. Examples: FORTRAN COBOLBASIC Java Python AdaPL/I Lisp C GPSSC++ Matlab

10-9 To illustrate differences in syntax for language levels, consider how a computer could be instructed to subtract two numbers stored in memory address locations 64 and 2048 and put the result in location 2048: increment value Memory Addresses Variable Names

10-10 Machine Language: (6-bit OpCode, 14-bit address fields with values 64 and 2048) value increment Memory Addresses Variable Names

10-11 Assembly Language: S increment,value C Language: value = value - increment; value increment Memory Addresses Variable Names

10-12 Programs written in high-level languages must be converted to machine language. Two approaches: (1) Compilation (see p. 28 FER Figure 2.4) Used with C, C++, Fortran,... (2) Interpretation Used with Matlab, Visual Basic,...

Step 1) Use editor to create a “source” file. We will name source files with a suffix “.c” Step 2) Run the gcc compiler on the source file to create an executable or object file with the default name a.out. For CS101 we will only create executable files. Step 3) Check to see if gcc caught any syntax errors, if so go back to step 1) Step 4) Run the program by typing >./a.out at the Unix prompt

10-14 Computer programming is the art/science of transforming a “real world” problem into a finite sequence of instructions(statements) in a computer language. The method stated in the following slide will be used throughout the rest of the semester. Follow this method in your Labs and for any Machine Problem.

Requirements Specification (Problem Definition) 2. Analysis---Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) 3. Design---Develop Algorithm (processing steps to solve problem) Use one of the following: Natural-Language Algorithm Flowchart Algorithm Pseudo-code Algorithm 4. Implementation --- Write the "Program" (Code) 5. Verification and Testing --- Test and Debug the Code

Requirements Specification (Problem Definition) Given a light-bulb with a pre-measured power consumption(in watts), compute the resistance (in ohms) of the bulb. 2. Analysis---Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = real number representing power Output=real number representing resistance

Design---Develop Algorithm Natural-Language Algorithm Prompt user for the power dissipation Read power Store value in storage location called power. Compute the resistance solving the formula “power = (voltage*voltage)/resistance” in terms of resistance. resistance = (voltage * voltage)/ power Print out the value stored in location resistance.

Design---Develop Algorithm Pseudo-code Algorithm print “enter power in watts” read power resistance = (117.0 * 117.0)/power print resistance

start Compute : resistance=(117*117)/power Output: : resistance stop 3. Design---Develop Algorithm Flowchart Algorithm Input : power Output : “enter power in watts” Flow is assumed down unless otherwise specified with an arrow. Trapezoid used to designate I/O. Rectangle used to designate one or more statements in a block. Circle used as continuation symbol for transfer to another page.

Implementation --- Write the "Program" (Code) (see the next slide) The lecture slides show the use of the gedit editor but the choice of editor is not critical.

C Code Implementation of the Algorithm /* C Program to compute the resistance */ /* of a light-bulb.*/ #include #define VAC void main(void) { /* Declare variables. */ float power, resistance; /* request user input power of */ /* light-bulb in watts. */ printf(”Please enter power(watts) :”); /* read value power */ scanf("%f", &power); /* Compute resistance assuming VAC = 117. */ resistance = (VAC * VAC) /power; /* Output the calculated resistance. */ printf(”Resistance is %f (ohms)\n", resistance); } (Note indentation scheme in above code.)

C program compilation Open gedit and enter your code. Note the &.

C program compilation Click the “Save” button in gedit to save your code but don’t close the gedit window. Click on the xterm window and type the command to compile your code.

Use the “gcc” program to compile your C source code in the file “resistance.c”. > ls resistance.c resistance.c~ Note: backup files begin and/or end with a ~ or a # symbol. Do not edit or compile these files! > gcc resistance.c The “gcc” program will not alter the file “resistance.c”. The output of “gcc” is by default contained in the file “a.out”. The file “a.out” is called an executable file. (continued on next slide) C program compilation

Note that the “gcc” program noted an error on line 18 in the program and “gcc” generated a warning for line 7. Also, note that there is no a.out file listed. Errors cause gcc to abort and you will not get an a.out file. Go back to gedit line 18 and fix the syntax error. (continued on next slide)

The error on line 18 was actually caused by a missing semicolon on line 16. In C a semicolon means “end of statement”. This differs from Matlab where a semicolon means suppress output and is optional. Since there was no semicolon after line 16, C assumed that lines represented one C statement. That is incorrect syntactically, so gcc (the compiler program or compiler for short) generated an error message and terminated before producing an a.out file.

C program compilation After adding a semicolon to the end of line 16, Click “Save” in gedit and go back to the xterm and type again... Note: The dot-slash in “./a.out ” means “ in this working directory”. If you type “ a.out ” at the Unix prompt Unix will first search your home directory for a file “ a.out ”. Of course this would not be the same “ a.out ” file you created in another directory.

10-28 A CPU only executes machine code which consists of instructions that are specific to a particular manufacturer. The machine code is a sequence of ones and zeros. The C language is more human-language-like than machine code and can be converted to machine code by using a compiler (gcc in CS101). C code is more portable than machine code. In the design and development of an algorithm we can use any combination of “Natural Language”, “Pseudo-code” and “Flow- chart”. Matlab code is executed by means of an interpreter that is running in the Matlab “environment”. Each time the Matlab code runs the interpreter converts the code into machine code. C code is compiled into an executable file (machine code) once. We then run the program (default name a.out) by typing the command (./a.out)_at the Unix prompt.