CPS120 week3
Document Sample


CPS 120 – Week 3
Computer hardware, decision making
Objectives of this unit
• Describe the machine cycle used to process
data in the computer.
• Explain the use of stored programs and
compilers and interpreters.
• Describe the development of programs and
the use of high level languages.
• Skill: decision making in VisualLogic
What’s in a modern computer?
5 Functional Units of a Computer
• The Central Processing Unit:
– (CPU),
– Buses,
– Ports and controllers,
– ROM;
• Main Memory (RAM);
• Input Devices;
• Output Devices;
• Secondary Storage;
– floppy disks,
– hard disk,
– CD-ROM
What does a CPU do?
Primary vs. Secondary storage
• The computer’s memory (RAM) holds data
only temporarily, at the time the computer is
executing a program.
• Secondary storage holds permanent or semi-
permanent data on some external magnetic or
optical medium
– Removable discs: CD-ROMs, DVD-ROM, diskette
– USB drives (“thumb” or “jump drive”)
– Hard drive
Differences between Storage
What Primary (RAM) Secondary (Disk drive,
Flashdrive, CD-ROM)
Addressable by CPU Yes No (copied to RAM)
Speed Fast (about 10 billionths Slow (10 millionths of a
of a second) second to 1 second)
Permanent No Yes
Capacity Low (1GB to 8GB) 1000GB +
Cost (per gigabyte) High ($10 to $40) Low (50 cents for
harddrive)
Control and the ALU
• The Control Unit : circuitry that uses electrical signals to
direct the entire computer system to carry out, or execute,
stored program instructions.
– Like an orchestra leader, the control unit does not execute
program instructions; rather, it directs other parts of the system
to do so.
– The control unit must communicate with both the
arithmetic/logic unit and memory.
• The Arithmetic/Logic Unit
The arithmetic/logic unit (ALU) contains the electronic
circuitry that executes all arithmetic (+, -, *, /) and logical
operations.
• A logical operation is usually a comparison. The computer
can then take action based on the result of the comparison.
Logic condition testing
• Equal-to condition (“==“) . ALU compares two values to
determine if they are equal.
– If the number of tickets sold equals the number of seats in the
auditorium, then the concert is declared sold out.
• Less-than condition (“<“) ALU compares values to
determine if one is less than another.
– If the number of speeding tickets on a driver's record is less than
three, then insurance rates are $425; otherwise, the rates are
$500.
• Greater-than condition (“>”) ALU computer determines if
one value is greater than another.
– If the hours a person worked this week are greater than 40, then
multiply every extra hour by 1.5 times the usual hourly wage to
compute overtime pay.
Simultaneous conditions
• A computer can simultaneously test for more
than one condition. A logic unit can usually
discern six logical relationships:
– equal to, ==
– less than, <
– greater than, >
– less than or equal to, <=
– greater than or equal to, >=
– not equal. !=
Registers: storage inside the CPU
• Registers: are temporary storage areas for
instructions or data.
• Registers are not RAM; they are special
additional storage locations that offer the
advantage of speed.
• Control unit directs registers to accept, hold, and
transfer instructions or data and perform
arithmetic or logical comparisons at high speed.
• Like the way a store owner uses a cash register-as
a temporary, convenient place to store what is
used in transactions.
Registers have different roles
• An accumulator, which collects the result of
computations.
• An address register, which keeps track of where a
given instruction or piece of data is stored in
memory. Each storage location in memory is
identified by an address, just as each house on a
street has an address.
• A storage register, which temporarily holds data
taken from or about to be sent to memory.
• A general-purpose register, which is used for
several functions.
Comparisons of storage
The Machine Cycle
Executing instructions
• Most computers today can execute only one
instruction at a time, though they execute it
very quickly.
• Many personal computers can execute
instructions in less than one-millionth of a
second. (1 μs)
• Supercomputers can execute instructions in
less than one-billionth of a second. (1 ns)
The Machine Cycle
• Once the necessary data and instruction are in memory, the central
processing unit performs the following four steps for each
instruction:
1. The control unit fetches (gets) the instruction from memory.
2. The control unit decodes the instruction (decides what it
means) and directs that the necessary data be moved from
memory to the arithmetic/logic unit.
3. The arithmetic/logic unit executes the arithmetic or logical
instruction. That is, the ALU is given control and performs the
actual operation on the data.
4. The arithmetic/logic unit stores the result of this operation in
memory or in a register.
Example: Add number to Accumulator
How does the CPU locate memory?
Every location has an address, just like apartment mailslots
Each memory location has an address
• The location in memory for each instruction and each
piece of data is identified by an address.
• That is, each location has an address number, like the
mailboxes in front of an apartment house.
• Like the mailboxes, the address numbers of the
locations remain the same, but the contents
(instructions and data) of the locations may change.
• New instructions or new data may be placed in the
locations when the old contents no longer need to be
stored in memory.
• Unlike a mailbox, however, a memory location can hold
only a fixed amount of data;
Memory Unit
• is an ordered sequence of
storage cells, each
capable of holding a
piece of information
• each cell has its own
unique address
• the information held can
be input data, computed
values, or your program
instructions.
20
Review
• Review on your own: How Computers and
Memory Work
Stored Programs
• John von Neumann’s idea: Store programs,
together with data, in the memory of the
computer.
• Memory contains: (a) instructions, (b) data.
• Conceptually, programs and data seem very
different. The first computers, distinguished
between them and stored them in different
places.
• Another point of view: programs ARE data.
Computer vs. Calculator
• What is the difference between a computer
and an inexpensive, drugstore calculator?
• Answer: The computer is programmable, and
this is achieved through the control unit.
• The control unit is responsible for running
programs from memory.
What is Machine Language?
• Most programmers write their programs in a
high-level programming language, l
– Java, C, C++, Ada, Pascal, COBOL, FORTRAN
• However, the CPU does not understand these
languages. The CPU has its own machine
language, which tells it what circuitry to
activate to do some operation.
• Compilers and interpreters translate from
high-level language to machine language.
What is “High-level” instruction?
• Example x = y + z (a simple addition)
• x, y, z are variables, i.e., positions (or
addresses) in memory with a specific name, to
which the programmer can refer.
• For example, let’s say x is at the 100th position
in memory, y at the 237th and z at the 17th.
Converted to Machine Language
• Takes about 4 instructions in a typical
computer
1. obtain first value (y) from memory
2. obtain second value (z) from memory
3. add two values in ALU
4. store result in memory (at x)
Example x = y + z
Assembly Language representation
• Assembly language is close to machine
language but still human-readable!
• LOAD RegA, 237
• LOAD RegB, 17
• ADD RegA, RegB, RegC
• STORE RegC, 100
Example x = y + z
Control unit registers
• The control unit has two special registers:
• Program counter (PC): contains the address
(in binary) in main memory of the next
instruction
• Instruction register (IR): contains the
instruction (in binary) that is currently being
executed
29
Fetch-decode-execute cycle
1. Fetch the instruction from the address in
memory pointed by PC (Program Counter) and
put it in IR (Instruction Register), then increment
the Program Counter by one, so that it points to
the next instruction.
2. Decode the binary sequence in IR to get the
meaning of the instruction (what circuitry must
be activated in ALU)
3. Execute the instruction (activate the relevant
circuitry in ALU)
• and again and again . . .
Obligatory Bad joke
• Q: “Why was the programmer found dead in
the shower?”
• A: “He was holding a shampoo bottle on
which the label read: ‘Lather, rinse, repeat.”
Comparison of Languages
High Level Low Level Machine Language
Examples C, C++, Java, Perl Assembly Pentium instruction
Language, p-code set.
Productivity High: easy to Low: requires 4 to Nearly impossible
express ideas 10 times as much to write or debug
code programs
Representation Nearly English Mathematical Binary
keywords keywords only 100110011000011
Tools Used by Compiler or Assembler Loader (none really)
Programmer Interpeter
Compilers vs. Interpreters
• Compiler: translates the program text into
object code, which is linked, and executed.
– Examples: C, C++, Java, FORTRAN, COBOL
• Interpreter: executes the program text directly
one statement at a time
– Examples: VisualLogic, Perl, PHP, SQL,
Features of Compilers, Interpreters
Feature Compiler Interpreter
Steps Required Three: Compile, Link, Only one: Execute
Execute
Program checked for At compile time At run time
correctness
Speed Fastest execution Slowest execution speed
Debugging Requires separate Debug inside the
environment interpreter
(e.g. Visual Studio) (e.g. VisualLogic)
Security of program Good Poor
Three C++ Program Stages
myprog.cpp myprog.obj myprog.exe
SOURCE OBJECT EXECUTABLE
written in
written in written in
machine
C++ machine
language
language
via compiler via linker
other code
from libraries,
etc.
35
Java Programming Language
• achieves portability by using both a compiler and an
interpreter
• first, a Java compiler translates a Java program into an
intermediate bytecode--not machine language
• then, an interpreter program called the Java Virtual
Machine (JVM) translates a single instruction in the
bytecode program to machine language and immediately
runs it, one at a time
36
Basic Control Structures
• a sequence is a series of statements that execute one
after another
• selection (branch) is used to execute different
statements depending on certain conditions
• Looping (repetition) is used to repeat statements while
certain conditions are met.
• a subprogram is used to break the program into smaller
units
37
SEQUENCE
Statement Statement Statement ...
38
SELECTION (branch)
IF Condition THEN Statement1 ELSE Statement2
Statement1
Condition ...
Statement2
39
LOOP (repetition)
WHILE Condition DO Statement1
False
...
Condition
Condition
Statement
40
SUBPROGRAM (function)
SUBPROGRAM1 ...
SUBPROGRAM1
a meaningful collection
of SEQUENCE,
SELECTION, LOOP,
SUBPROGRAM
41
42
VisualLogic demos
• Payroll program with multiple employees
• As above, plus simple overtime
• Fixed looping: compute factorials
• Convert to the “WHILE” loop metaphor
Get documents about "