02 -introduction To Scientific Programming And C

  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View 02 -introduction To Scientific Programming And C as PDF for free.

More details

  • Words: 1,580
  • Pages: 5
Hardware and Software • A computer is a machine designed to perform operations specified with a set of instructions called a program. • Hardware refers to the computer equipment. – keyboard, mouse, terminal, hard disk, printer, CPA • Software refers to the programs that describe the steps we want the computer to perform.

Introduction to Scientific Computer Programming – Computer Concepts and C

2

Computer Hardware

Computer Organization

•  CPU –

• Input Unit – Receives data into the computer (receiving section)

– Central processing unit

• Output Unit

•  ALU –

– Outputs information (shipping section)

– Arithmetic and logic unit

• Memory Unit

•  ROM –

– Called the primary memory, the warehouse section

– Read only memory

• Arithmetic and Logic Unit

•  RAM –

– Performs calculations and makes decisions

– Random access memory

• Central Processing Unit – Supervises all operations of the other sections

• Secondary Storage Unit – Stores programs and data currently not needed 3

4

Software: Operating System

Computer Software: Applications

•  A system of computer programs that control the interaction of the user and the computer hardware.

•  Application Software (Software Tools) – Word processors (Microsoft Word, WordPerfect, ...) – Spreadsheet programs (Excel, Lotus1-2-3, ...) – Computer games – Communication software (email, chat, web browser…) – Telecommunication software (VOIP, …)

– Examples: Linux, Windows, QNX,…

•  Purposes of the operating system – – – – – –

Controls communication between input and output hardware and each process running in the computer. Directs and orders all operations of the computer Allocates and shares resources (memory,processor…) Controls access to resources and data (security) Provides a user interface (UI) and/or graphical user interface (GUI) – Stores and accesses data and applications

5

6

1

Computer Software: Languages

Machine language

•  Some Computer Languages

•  Each type of processor (like Pentium 4, Athalon, Z80, …) has its own instruction set •  Each instruction in an instruction set does a single thing like access a piece of data, add two pieces of data, compare two pieces of data … •  Each instruction is represented by a unique number This # may be different for different instruction sets, but no two instructions in the same instruction set will have the same #

– – – –

Machine language (machine instruction set) assembly language high level languages C, C++, Ada, Fortran, Basic, Java

•  Do YOU know of any others? – mathematical computation tools (MATLAB, Mathematica, ...)

•  Application software is written using computer languages. 7

8

Machine Language Programs

Assembler

•  In machine language a program is a list of instructions

•  Assembler languages make it easier for the programmer.

– Each instruction is represented by a number – Inside the memory of the computer each number is represented in binary (as a constant length string of 1’s and 0’s) – The long string of 0’s and 1’s is easy for the computer to understand and very difficult for a Human to read or write

– Assembler is easier for humans to read/write – The numbers that identify each of the instructions in the instruction set are replaced with mnemonics like ADD, CMP, … – The code, written using these mnemonics is written into a text file.

9

10

Assembler Programs

Programs in High Level Languages

•  The code for an Assembler program is written into a text file. •  The computer read 1’s and 0’s not text •  How do we translate to machine readable form?

•  Assembler is easier to read/write than machine language. It is still very cumbersome •  High level languages are easier to write than assembler – The compiler is more complex, but that is a tool you use, not one you write

– A computer program called a compiler is used to translate the text file (called a source file) containing the assembler code into machine readable code – The compiler writes a binary file containing the machine readable code (called an object file)

11

12

2

History of C

C Standard Library

• C evolved from two previous languages

• C program consists of modules or pieces called functions. • The C Standard Library contains a extensive collection of functions to form a C program. • In creating a program in C, usually, you would use the following:

– BCPL in 1967 by Martin Richards – B in 1970 by Ken Thompson

• From B, Dennis Ritchie developed C at Bell Laboratories in 1972. – Today, virtually all Operating Systems are developed using the C language, or the C++.

– The C Standard Library functions – Functions you create yourself – Functions others have created, and are now available for you

13

14

C Program Execution

Source files

• There are six phases a C programs goes through to be executed

•  Contains the text you type into a text editor •  The text is a program •  The program is a list of instructions written in a special Human readable language (C) •  The program can be translated, from the Human readable language (in source file) to a machine readable language (in object file), by a compiler •  A compiler is a special piece of software used to translate from source files to object files

1. Editing – Typing in and saving of program files on disk

2. Preprocessing – Completes preprocessor directives (e.g. includes…)

3. Compiling – Translation of C program to machine language code

4. Linking – Links object code with the libraries and saves on disk

5. Loading – Places program in memory

6. Executing – Processing of instructions (program execution) 15

16

Source files

Perfect Code? Finding Errors 1

•  Write using a text editor •  Do not write your code using a word processor like Microsoft Word. A word processor will save in a special format. •  The compiler reads only text, not special • formats. •  Beware: your text does not differentiate between a word processor and an editor

•  It is highly unlikely than any of us will always write perfect code that contains no errors •  How do we find errors? – Are there different kinds of errors? Yes

•  A computer program can contain syntax errors, semantic errors, or logical errors •  When and how can we detect and correct • errors? – Compile errors – Link errors – Run Time Errors 17

18

3

Syntax Errors

Semantic Errors

•  A computer program can contain syntax errors •  A computer language follows simple rules

•  A computer program can contain semantic errors •  Semantics relates to the meaning of the words in a sentence or a computer language command

– how words and punctuation of different types may be combined. – In English syntax is similar to grammatical structure

– Just like a grammatically correct English sentence can be nonsense, a syntactically correct high level computer language command can also contain semantic errors

•  The compiler for a high level language can detect errors that break those simple of syntax (syntax errors) •  Syntax Errors are usually detected at compile time

•  Some semantic errors may be found by the compiler, some will be found when the program is linked, some may be found at run time

19

20

Logical Errors

Perfect Code? Finding Errors 2

•  When your program completes but gives an unexpected answer it usually means there is a error in the logic in your solution of the problem •  Logic errors can also cause a program to fail part way through execution

•  If your code contains compile time errors is it correct? NOT NECESSARILY •  How do we find the remaining errors? – Use a tool that does more checks than the compiler (splint). – Move on to linking the code to libraries etc. – The linker resolves references, words in your program than are defined elsewhere



Errors occur when the definitions cannot be found • Errors occur when the use of the word does not correspond to the definition 21

22

Summary: Executing a Computer Program

Perfect Code? Finding Errors 3 •  If your code compiles and contains no errors that can be found by the linker (or splint) is it correct? NOT NECESSARILY •  How do we find the remaining logic and semantic errors?

•  Compiler – Converts a source file (containing your human readable program in C) to and object file (computer readable binary file)

•  Linker – Converts object program to executable program

– When you run your executable program it may not complete (may or may not generate error message) – It may complete and give the wrong answer

23

24

4

Summary: Types of Errors •  Syntax errors – Errors in syntax, how words are combined and used reported by the compiler or splint

Introduction to Scientific Computer Programming – Computer Concepts and C

•  Semantic errors – – – –

Errors in the meaning of words, Reported by the linker (linker errors) Reported by the compiler (compile time errors) Found at execution time (run-time errors)

•  Logic errors – Errors causing the incorrect results, not reported – Errors causing program failure (run-time errors) 25

5

Related Documents