Week 1 Lecture Material Penn State University CMPSC 201 – C++ Programming for Engineers Original...
Transcript of Week 1 Lecture Material Penn State University CMPSC 201 – C++ Programming for Engineers Original...
Week 1 Lecture Material
Penn State University
CMPSC 201 – C++ Programming for Engineers
Original class notes from Dough Hogan,
http://www.personal.psu.edu/djh300/index.htm
Computer Science Theory & Introduction
Hardware vs. SoftwareHardware :
physical components of a computer systemessentially, things you can touchinput, output, storage devices
Software : A collection of computer programs and its related
data that provides the instructions telling a computer what to do!
In contrast to hardware, software "cannot be touched“!
0s and 1s : Owing to its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used internally by all modern computers.
Hardware vs. Software
Application Software
System Software
Hardware
Components of a ComputerCentral processing unit (CPU)MemoryInput devicesOutput devicesStorage devices
Central Processing Unit (CPU)Basic job: handle processing of instructions
What’s an instruction?
Two parts:Control Unit (CU) Arithmetic and Logic Unit (ALU)
The ALUBuilt up from digital logic gates
ANDORNOTmost primitive level
MemoryHolds programs that are currently
running and the data being used by those programs.
Two categories:Read-only memory (ROM)
can only read dataRandom-access memory (RAM)
can read and write informationprimary storage - computer’s main memoryvolatile
Sequential Access vs. Random AccessSequential Access:
must access each location in memory in order
Random Access: can access memory locations using addresses, in any order
Speed implications?
Track 1
Track 2
Memory: sizesbase unit: 1 bit = binary digit, 0 or 18 bits = 1 byte (B)1000 bytes ≈ 1 kilobyte (KB)1000 KB ≈ 1,000,000 B ≈ 1 megabyte
(MB)1000 MB ≈ 1,000,000,000 B ≈ 1 gigabyte
(GB)
Storage DevicesFloppy disk
3.5 inches, 1.44 MBHard disk
typically sizes in GBCompact disc (CD)
650-700 MBCD-ROM: read-only memoryCD-R: recordableCD-RW: rewritable
More Storage DevicesDigital Versitale/Video Disc (DVD)
4.7 GBFlash drives
variesZip disks and tape drives
varies
Input Devicesmousekeyboardscannercameramicrophone
Output Devicesmonitor
cathode ray tube (CRT)liquid crystal display (LCD)
printerspeakers
Software OverviewSystem software
Controls basic operations of computerThe operating system
manages memory, files, application softwareFile management tasks – deleting, etc.
Software OverviewApplication software
Not essential to system runningEnables you to perform specific tasksEx:
Office softwareWeb browsersMedia playersGames
Algorithms and Languages
AlgorithmsAn algorithm is a set of instructions to solve a
problem.Think recipes!
Many algorithms may solve the same problem!How do we choose between them?
The answer is that:Different Algorithms differ in the time and the space
they take to run!we choose the most efficient algorithm according to
our resources! (space and time)Example: You may have a lot of memory and your only
concern is how fast the program runs, or the other way around.
Programming Paradigms/Models
Procedural Programming: specify steps to solve problem, use procedures to implement those stepsProcedures are called methods in C++
Object-Oriented Programming (OOP): create objects to model real-world entities. e.g. an employee object
As in real world each object has its ownInformation: (represented by data types)Behaviors: (implemented by methods/procedures)
Objects can interact, as they do in real world Event-Driven Programming: create methods that
respond to events like mouse clicks, key presses, etc.
Others: Functional, logic, etc.
Compiling Process
Source Code(C++)
ObjectCode
ExecutableProgram
ObjectCode fromLibraries
compiler linker
Compiler is a software that transforms a source code written in one programming language into a program in another language (usually machine language, a language than can be executed by CPU)
Code in machine language
linker :A program that takes two or more object files generated by a compiler and combines them into a single executable program.
Source Code(C++)
ObjectCode
ExecutableProgram
ObjectCode fromLibraries
compiler linker
Library has been complied before! , it won’t need to be compiled every time.
ObjectCode fromLibraries
Source Code of library
(C++)compiler
Compiled vs. Interpreted Languages
Interpreted LanguageRequires software
called an interpreter to run the code
Code is checked for errors as it runs (if erroneous code: does the best it can…)
Examples: HTML, JavaScript, PHP
Compiled LanguageRequires software called
a compiler to translate the program into (usually) an executable program with machine language
Syntax errors will be found during compilation
code will be syntax-error free while executing (running)
Examples: C, C++, Pascal, FORTRAN, BASIC
ErrorsSyntax Errors
Much like using incorrect punctuation or grammar in English
Compiler reports syntax errors; program won’t run until they’re resolved
Logic Errors Program doesn’t solve the problem correctlyIt May be correct syntactically, but it is not doing
what is supposed to do (according to specification of program)!
Runtime ErrorsErrors that occur while the program is running,
e.g. problems accessing memory, divide by zero
AbstractionAbstraction
Abstraction is a fundamental concept to CS
Principle of ignoring details , so that by focusing on more general
concepts we are able to use complex devicesFocusing on the WHAT, rather than HOW
Example: Abstract of a paper!Example: Your report template!
Levels of Abstraction0. Digital Logic1. Machine Language
A system of instructions, that are executable directly by the CPU.
Every CPU has an instruction set architecture (ISA) of its own!
2. Operating System3. Low-Level Language
Low refers to small or no amount of abstraction from machine language. e.g. Assembly language
4. High-Level Language High refers to strong abstraction from the details of the CPU
designUses natural language elements, thus it’s easier to
understand
5. Application Software
Binary NumbersUse two symbols: 0 and 1 to represents
numbersBase 2Compare with decimal number system
Uses symbols 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 to represents numbers
Base 10
At the lowest level of abstraction, everything in a computer is expressed in binary.
Binary Numbers, ctd.
0 0 1 1
10 211 3
100 4
101 5110 6111 7
1000 81001 9
1010 101011 111100 121101 131110 141111 15
10000 16
Binary Numbers, ctd.
0 0 1 1
10 211 3
100 4
101 5110 6111 7
1000 81001 9
1010 101011 111100 121101 131110 141111 15
10000 16
1 1 + 1_____________________
1 0 0
1 0 0 0 + 1_____________________
1 0 0 1
Convert Vs. Decimal
Other Number SystemsAny positive integer could be the base of a
number system. (Big topic in number theory.)
Others used in computer science:Octal: Base 8Hexadecimal:
Base 16New symbols A, B, C, D, E, F
Example of machine language instructions
At the lowest level of abstraction, everything in a computer is expressed in binary.
This is an example of an ‘Add’ instruction: 000000 00001 00010 00110 00000 100000
Couple of more examples of machine language instructions:100011 00011 01000 00000 00001 000100000010 00000 00000 00000 10000 000000
Hello World Example in Assembly Language
.model small
.stack
.datamessage db "Hello world, I'm learning Assembly !!!", "$".codemain proc mov ax,seg message mov ds,ax mov ah,09 lea dx,message int 21h mov ax,4c00h int 21hmin endpenad main
ASCIIEvery character on a computer -- letters,
digits, symbols, etc. -- is represented by a numeric code behind the scenes.
ASCII is a character-encoding scheme based on the ordering of the English alphabet
Short for American Standard Code for Information Interchange.
Most modern character-encoding schemes are based on ASCII, though they support many more characters than does ASCII.
We’ll learn more in lab…
Moor’s Law
# Transistors on a ProcessorProcessor Date Number of Transistors
4004 1971 2,250
8008 1972 2,500
8080 1974 5,000
8086 1978 29,000
286 1982 120,000
386 1985 275,000
486 DX 1989 1,180,000
Pentium 1993 3,100,000
Pentium II 1997 7,500,000
Pentium III 1999 24,000,000
Pentium 4 2000 42,000,000
Data forIntel processors:
Data from Section 4.1 of :
Yates, Daniel S., and David S. Moore and Daren S. Starnes. The Practice of Statistics. 2nd Ed. New York: Freeman, 2003.
A Graphical View
Graph from Intel's web site (http://www.intel.com/technology/mooreslaw/index.htm); Retrieved 9/24/2006
Pay attention to the units on the axes…
Moore’s LawPrediction from Gordon Moore of Intel in
1965.Implication: The speed of processors
doubles roughly every 12 to 18 months. Can this go on forever?