Uploaded by Waleed Salem

Lecture1FIN

advertisement
ECE15: Introduction to
Computer Programming
Using the C Language
Lecture Unit 1: Introduction
What Is this Course About?
❖ This course is about communicating with computers,
telling computers exactly what you want them to do:
‣ We will study the C programming language -- a very useful
language for communicating with computers.
‣ We will also study a special way of thinking when communicating
with computers: breaking down a task into its components, design
of algorithms, their evaluation, and so on.
❖ This is an easy course: everything should be clear!
❖ This is a difficult course: you will need to learn a new
“language” and adapt to a new way of thinking.
❖ The students in this course usually have many different
backgrounds. We will teach everything from scratch,
assuming no prior knowledge of programming.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
2
Outline of this Lecture
❖ Computer Hardware -- Overview
❖ Computer software -- overview
❖ Our very first C programs
❖ Some relevant history
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
3
Computer Hardware
Computers are everywhere: desktops, laptops, cars, smart
phones, clouds, TVs, microwave ovens, and so on...
All these devices share the following general structure:
Bus
Input Devices
CPU
Memory
Output Devices
Storage
Devices
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
4
Hardware: Main Components
CPU (Central Processing Unit):
Consists of numerous logic circuits, in particular the ALU
(arithmetic logic unit), operating at a very high frequency.
This is the heart of the computer: it is here that computation and data processing actually take place.
Memory:
Also known as “main memory” or random-access memory
(RAM). It is here that all the data and instructions needed
for the programs that are currently running are stored.
Bus:
Internal communication channel. Serves to exchange data
between the various components of the computer.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
5
Hardware: Main Components
Input Devices:
Usually, keyboard and mouse. But also anything that can
serve to input information into a computer.
Output Devices:
Usually, monitor and printer. But also anything that serves
to get data/information/signals out of a computer.
Storage Devices:
This is where all files are stored. Unlike the RAM memory,
these devices are non-volatile: stored data does not disappear when power is turned off. However, these devices
are much slower than RAM. Examples include: magnetic
or SS hard disk, flash memory, DVDs, magnetic tape.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
6
CPU: Frequency and Clock Cycles
Programs are converted into sequences of elementary
instructions for the CPU. These instructions are then carried out
using an internal clock. Thus the speed of a computer is roughly
proportional to:
number of clock cycles per second = frequency
Examples:
1984: First Macintosh -- 8MHz (eight million cycles per second)
1993: Intel486 Processor -- 33MHz (used in most PCs of that era)
Today: iPhone 8 cell phone 2,340MHz, Intel Pentium up to 4,400MHz
Note: Each elementary CPU instruction takes several
clock cycles. For instance, multiplication takes many
more clock cycles than addition.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
7
Main Components of the CPU
Arithmetic Logic Unit (ALU): The heart of the CPU
where computation is performed. Computation is broken down
into simple operations, such as:
‣ Arithmetic operations (addition, multiplication, etc.)
‣ Logic operations (AND, OR, etc.)
‣ Comparisons of data in the RAM memory or in CPU registers
CPU Registers: Small number of memory units that allow
especially fast access. Access to registers is much faster than
access to RAM. One of the registers serves as the program
counter, pointing to the instruction that will be carried out next.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
8
Example of CPU Operation
Goal: Compute A + B = C, with A, B, and C being
stored in the RAM memory.
RAM
Computation steps:
‣ Move the content of A into R1
‣ Move the content of B into R2
‣ Add R1 and R2, put result in R3
‣ Move the result from R3 into C
CPU
A
Registers
B
R1
ALU
R2
C
R3
Note that the simple operation A + B = C takes four clock
cycles. More complex operations take more cycles.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
9
Organization of RAM Memory
RAM
❖ Physically, RAM memory consists of one or
more chips, containing millions of capacitors (or
other electronic circuits)
capable of storing data.
❖ Logically, the memory can be thought of as a long
table divided into cells. Each cell has an address.
Using the cell’s address, the computer can read its
value or set its value. The cells are called bytes.
❖ How many bytes? A typical range for a laptop is from
512MB to 32GB. The first Macintosh 128KB. My Linux workstation 64GB. Here is a useful table:
KB = 210 bytes MB = 220 bytes GB = 230 bytes
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
10
How Is Information Stored in RAM?
❖ The fundamental unit of information is a bit. The bit is
binary, it can take only two values -- 0 and 1.
❖ Each byte is a sequence of 8 bits, for example
0000000, 01101110, etc. Since a byte is the
smallest addressable unit in the computer
memory, bits cannot be accessed individually
but only in chunks of 8 bits forming a byte.
❖ What does a byte mean? Many different
answers are possible:
‣ A number, for example 42, 100, 255, etc.
‣ A character, such as “a”, “b”, “c” or “C”, “P”, “U”, etc.
‣ Part of an instruction for the arithmetic logic unit in the CPU
‣ Anything you want, depending on how your program works
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
11
The Binary Number System
❖ Just like in the decimal number
system, we write:
*
9
3
7
5
103 102 101 100
5 + 70 + 300+ 9000 = 9375
using the digits {0,1,2,3,4,5,6,7,8,9} and powers of 10...
❖ So in the binary number system,
we shall write:
*
1
0
1
0
23
22
21
20
0 + 2 + 0 + 8 = 10
using the binary digits {0,1} and powers of 2.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
12
Conversion from Decimal to Binary
❖ What is decimal 10 in binary?
0 0 1 0 1 0
25
24
23
22
21
20
❖ The same answer, using a different method:
quotient
0
div:2
quotient
1
div:2
remainder
quotient
2
div:2
quotient
5
remainder
div:2
10
remainder
remainder
1 00 11 00
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
13
Outline of this Lecture
❖ Computer hardware -- overview
❖ Computer Software -- Overview
❖ Our very first C programs
❖ Some relevant history
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
14
What’s Software and How Does It Work?
❖ Software is a program or a collection of programs.
Each program is a sequence of instructions to the
computer, designed to perform a specific task.
❖ At run-time, the program’s instructions are downloaded
into the main memory (RAM) of the computer.
❖ The CPU circuits are hard-wired to do the following: fetch
instructions one-by-one from the main memory, decode
them, and execute them.
❖ The instructions are executed sequentially, one-by-one,
with one exception: certain instructions are branching
instructions, which make the CPU jump to a specific
address in the main memory and resume execution.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
15
Software: Machine Language
❖ Machine language is the only language that the CPU
really understands. It is different for different CPUs.
❖ Machine language is a set of binary strings that code
for elementary CPU instructions, such as:
‣ Data transfer between registers and RAM
‣ Comparisons and conditional branching
‣ Simple arithmetic and logic operations
❖ Here is an example:
0100 000001 000010
move from R1 to R2
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
16
Software: Assembly Language
❖ The Assembly language is a symbolic representation
of the binary machine language, for example:
loop:
mov
mov
mov
mov
add
mov
add
cmp
bne
#0, x
#1, y
#0, i
y, t
x, y
t, x
#1, i
i, n
loop
Put 0 in variable x
What does
Put 1 in variable y
this do?
Put 0 in variable i
Copy the value of y into variable t
Add the value of x to variable y
Copy the value of t into variable x
Add 1 to variable i
Compare the values of variables i and n
If they are not equal, go back to “loop:"
❖ Assembler is a program that converts programs written in
the Assembly language into machine language (binary).
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
17
Software: High-Level Languages
❖ Examples: Pascal, C, C++, Java, Lisp, Python, etc.
x = 0;
y = 1;
i = 0;
n = 30;
do {
t =
y =
x =
} while
y;
y + x;
t;
(i++ < n);
What does
this do?
y = 1, 1, 2, 3, 5, 8, 13, 21, 34, …
i = 0, 1, 2, 3, 4, 5, 6, 7, 8, …
‣ High-level languages include more powerful instructions than an
Assembly language. Easier to write and understand.
‣ Compiler is a program that converts programs written in a high-level
language directly into machine language of a specific computer.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
18
Software: Operating Systems
❖ An operating system is a collection of programs that
‣ Initiates the execution of all other programs
‣ Allocates resources (memory, CPU cycles) to other programs
‣ Controls the entire computer system and its peripherals
❖ Whenever a computer is powered on, an operating system
is running in the background, managing the computer.
❖ The operating system effectively mediates between
all of the hardware and software in a computer.
❖ Examples:
ux
n
i
L
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
19
The Five Steps of Software Writing
Step 1: Analysis -- definition of the input and desired output
Step 2: Design of the program:
‣ Developing an algorithm for producing desired output from input
‣ Designing the appropriate data structures
‣ Breaking down the algorithm into simple components
Step 3: Implementation -- coding using the chosen language
Step 4: Debugging, debugging, debugging ...
Step 5: User support and maintenance
The more time one spends on Steps 1 and 2, the easier are the
remaining steps. For large complex programs, Steps 4 and 5
are, by far, the most time consuming. In some cases, algorithm
development in Step 2 is extremely challenging.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
20
The C Programming Language
❖ Developed by Dennis Ritchie in the early 1970s.
❖ Originally intended to implement the Unix operating system.
❖ C is an extremely flexible language: it is possible to write
high-level complex programs in C on one hand, while being
able to deal with details on the level of bits on the other hand.
❖ Serves as the backbone for C++ and Java.
❖ Millions of important engineering programs
are written in C.
This is the language
we’ll study in ECE15!
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
21
Outline of this Lecture
❖ Computer hardware -- overview
❖ Computer software -- overview
❖ Our Very First C Programs
❖ Some relevant history
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
22
How to Say “hello world” in C?
/* The traditional first program in honor of Dennis
m Ritchie who invented C at Bell Labs in 1972
*/
#include <stdio.h>
int main()
{
printf("hello world!\n");
return 0;
}
RUN
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
23
Simple Addition Program in C
Task: Read 10 arbitrary numbers from the user
and print their sum.
Step 1: Analysis of the task -- definition
of the input and of the desired output.
‣ Input: a sequence of 10 integers
‣ Output: their sum
Step 2: Design of the program:
‣ Developing an algorithm
‣ Designing the appropriate data structure
‣ The algorithm will already be very simple!
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
24
Simple Addition Program: Design
❖ The Algorithm:
‣ Initialize the sum to zero.
‣ Do 10 times the following:
๏ Read the next integer value from the input
๏ Add this value to the current running sum
‣ Output (print) the sum.
❖ Data Structure: We will need three variables
‣ The current running sum (sum)
‣ The current integer value being read (value)
‣ The number of values read so far (i)
Note: The algorithm and the data structure do not depend
directly on the specific programming language.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
25
Simple Addition Program: Coding
#include <stdio.h>
int main()
{
int i, sum, value;
sum = 0;
for (i = 0; i <
scanf("%d",
sum = sum +
}
printf("%d \n",
}
Lecture Unit 1
RUN
10; i++) {
&value);
value;
sum);
return 0;
ECE15: Introduction to Computer Programming Using the C Language
26
From Source Code to Executable
source code
prog.c
Compiler
object code
prog.obj
other object code
other.obj
library
libc.lib
Linker
executable
prog.exe
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
27
Types of Programming Errors
Syntax Errors: such as typos, missing punctuation,
or any invalid use of the C language, for example:
sum = sum + value;
sum = num + value;
Syntax errors are immediately found by the compiler. Easy!
Run-time Errors: e.g. division by 0 in a loop; pass the compiler unnoticed, but the program crashes. Also relatively easy.
Semantic Errors: errors in meaning that cause the program
to produce incorrect output, for example:
Bug!
printf("%d \n", sum);
printf("%d \n", value);
Logical Errors: in the algorithm itself. Most difficult!
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
28
Outline of this Unit
❖ Computer hardware -- overview
❖ Computer software -- overview
❖ Our very first C programs
❖ Some Relevant History
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
29
The First Computing Device
It has all started with the abacus...
‣ Originated in China or the Middle East.
‣ Great improvement as compared to counting
using your hands and fingers.
‣ Abacus “remembers” the current state; user
controls transition to the next state.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
30
Wilhelm Schickard, 1623
❖ Wilhelm Schickard was a Professor of
Hebrew and astronomy at the University
of Tübingen in Germany.
❖ He produced the first known calculating
machine in 1623. The machine could add
and subtract six-digit numbers, and indicated overflow by ringing a bell.
Note: The Schickard calculating
machine was not programmable.
The first programmable machine
would come ~200 years later.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
31
Blaise Pascal, 1642
❖ French philosopher and mathematician.
❖ Build his first mechanical adder, known as
the Pascaline, at age 19. Design uses system of weights and gears.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
32
Joseph-Marie Jacquard, 1805
❖ French silk weaver and inventor,
constructed the first programmable machine in 1805.
❖ The machine used a sequence of
punched cards to input a desired
program (set of instructions).
❖ Variations of the Jacquard loom
are still used in the textile industry.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
33
Charles Babbage, 1791-1871
❖ Designed in 1822 the difference engine
to compute values of polynomial functions.
The first difference engine was composed
of ~25,000 parts and weighed fifteen tons.
❖ In 1833, extended this design to the more
complex analytical engine that included
sequential control, branching, and looping.
❖ Ada Lovelace wrote a program
for the analytical engine, to compute certain Bernoulli numbers,
in 1843. Ada is thought to be the
first computer programmer.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
34
Alan Turing, 1912-1954
❖ Genius “father of computer science.”
Conceived the Turing machine, the fundamental model of computation.
❖ Designed in 1940 an electro-mechanical
computer, the “bombe,” that was used to
break the German Enigma code.
❖ Proposed the “Turing test” for
artificial intelligence, which no
computer has been able to pass
till this day.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
35
The First Electronic Computers
❖ The first electronic computer, called Mark-1, was built
by Howard Aiken and his group at Harvard University in 1944.
❖ The ENIAC soon followed in 1946. It was created by a team
of scientists at the University of Pennsylvania, mainly to assist
in computations related to ballistic analysis for US military.
Creator of the ENIAC: Six digital
computers will surely suffice for all
the computation needs of the US.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
36
Digital Computer Hardware
❖ The first electronic computers
were built from vacuum tubes
and electro-mechanical relays.
❖ The transistor was invented at
Bell Labs in 1948. It replaced the
relays and the tubes.
❖ The year 1960 saw the first integrated circuits combining several
transistors in a single silicon chip.
❖ Today, computers are built from
chips comprising about a billion
transistors.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
37
IBM 7094 Mainframe in 1970
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
38
Input Using Perforated Tape in 1970
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
39
Input Using Punched Cards in 1975
Card Reader
Lecture Unit 1
Card Puncher
ECE15: Introduction to Computer Programming Using the C Language
40
First Personal Computer in 1975
Altair 8800 Computers
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
41
Microsoft is Founded in 1975
❖ In 1975, Bill Gates & Paul Allen
signed a contract with MITS, the
company producing Altair computers to develop a compiler for
their personal computer line.
❖ In 1980 Microsoft has contracted
to develop for IBM an operating
system for personal computers
called Microsoft Disk Operating
System or MS-DOS.
❖ The rest is history...
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
42
Apple Computers Born in 1975
In 1975, Steve Wozniak &
Steve Jobs founded Apple
and developed in Steve’s
garage a simple personal
computer that used a TV for
its screen.
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
43
High-Level Computer Languages
1957: FORTRAN
1958: ALGOL, LISP
1959: COBOL
1968: Pascal
1972: C
1973: ML
1980: Ada
1983: C++
1987: Perl
D. Ritchie
B. Kernighan
1989: ANSI-C (American National Standards Institute)
1994: Java
1995: JavaScript
1999: ISO-C (International Organization for Standardization)
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
44
First Apple Computers in 1977
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
45
IBM Personal Computer in 1981
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
46
And Today?
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
47
The Famous Moore’s Law
A factor of 100 every 10 years:
A factor of 2 every 24 months!
Lecture Unit 1
ECE15: Introduction to Computer Programming Using the C Language
48
Download