Transcript Chapter 1
Chapter 1
Introduction
Components of a Computer
CPU (central processing unit)
• Executing instructions
– Carrying out arithmetic and logical operations
(+, -, *, /, and, or, …)
– Coordinating: Accessing memory (read,
write), handling exceptions (divided by zero),
…
Components of a Computer (cont.)
Memory
• Storing and retrieving (writing and reading)
information (instructions and data).
• RAM (random-access memory)
– volatile
• Secondary storage
– Hard disk, CD, USB
– Non-volatile
Information in Memory
• Binary, 100110100010…
• A string of 0s and 1s can mean different
things
– A number (integer, floating-point)
– An instruction (addition, load, store)
• In RISC, every piece of information
(number or instruction) is 32 bits long.
Numbers in Memory
• Binary
• Converting decimal to binary
– Integers
100 = 1*2^6 + 1*2^5 + 0*2^4 + 0*2^3 + 1*2^2
+ 0*2^1 + 0*2^0
10010 = 11001002
• Converting decimal to binary
– Fractions
2*
2^0
0.1
0
2^(-1) 2^(-2) 2^(-3) 2^(-4) 2^(-5) 2^(-6) …
0.2
0.4
0.8
1.6
1.2
0.4 …
0
0
0
1
1
0 …
1*10^(-1) = 1*2(-4) + 1*2^(-5) + 0*2^(-6) + …
1*10^(-1) = 1.10011001100…*2^(-4)
0.110 = 0.000110011001100…2
• Converting binary to decimal
Integers
11001002 = 2^6 + 2^5 + 2^2 = 64 + 32 + 4
= 10010
Fractions
1.100112 = 2^0 + 2^(-1) + 2^(-4) + 2^(-5)
= 1 + 1/2 + 1/16 + 1/32
= 1.5937510
Components of a Computer (cont.)
• I/O (input/output) devices
– Keyboard, mouse
– Monitor
• Communication links
– Bus
– Network
Algorithms
Method for solving a problem using a
computer
• Unambiguous (well-defined and precise)
• Executable (can be carried out)
• Finite (does not run on forever)
An algorithm is language independent.
Program
Implementation of an algorithm in a
language (Java), so a computer can
understand.
• Stored in a file (source file)
• extension to identify the language
(hw1.java).
From Program to Machine Code
• Compiler: Translates a program (high-level
language) into machine language (lowlevel).
– Machine architecture dependent (Intel, sparc).
– End result: Executable file (binary).
• Often object files are linked together to
produce an executable.
From Program to Machine Code
• Interpreter
– Translate a program into an intermediate
language, independent of machine
architecture.
– Run an intermediate language program by an
interpreter that implements the intermediate
language on that machine.
• JVM (Java virtual machine) executes an
intermediate language program on the
underlying machine.
Program Errors
Computers (hardware) are very rigid, cannot
tolerate slightest errors:
• Missing “;”
• Misplaced spaces
• Upper case in place of lower case
• …
Programming Disciplines
• Design, design, design. Think logically,
structure your program carefully, know
exactly what each step is supposed to do,
anticipate future changes.
• Your program is to be read and modified
by others.
Programming Disciplines
• Consistent style
– Comments
– Indentations
– Line breaks
– Blank lines
– Positions of “{“ and “}”
Programming is a craftsmanship, requires
skills and art.