instruction - Computer Science

Download Report

Transcript instruction - Computer Science

Introduction to Computer Science
CSCI 150 Section 002
Session 23
Dr. Richard J. Bonneau
IONA Technologies
[email protected]
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
1
Today’s Outline
 Today’s ‘notices’
 Tonight’s Topics
– Review of Last Session - last of digital circuits
– Chapter 8 topics
» Computer Architecture - the P88 computer!
– Chapter 9 topics – very briefly …
» Language Translation
 Next Class Topics
– HTML – Hyper Text Markup Language
– Web Pages – constructed from HTML
– Lab session with lab exercises at the end
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
2
Today’s Notices
 Office
 Extension
 Office hours
Swords 339
2284
Tu-Th 4-6:30 and after class
 No class on Thursday – Easter Break
 Homework 7 Grading Status ??? – “not till after Easter Break” Kyle
 Homework 8 Due Week from Thursday
 Today’s “Pascal programs” can be found on Web Site …
 Handout on “Programming the P88 Architecture” – also on web site
 CEF Survey – a week from Thursday – April 24th
 Final Exam – Saturday May 3rd –
2:30 – in
Haberlin Lab 408 – change of room!!
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
3
Summary of Last Session
 Simple - non-arithmetic circuits - select/mux
– Can be used select/move data from one place to
another
– Also translate controls into specific individual, exclusive
signals
 Sequential circuits
–
–
–
–
–
clocks/pulsers
scopes and waveforms
latches - to hold state of data - I.e. 1 or 0 !
registers = collections of latches
types of memory as an addressable set of registers
 Interesting circuits – never got to see them …
– using supplied circuit elements and circuits from Circuit
Maker’s library
– input devices, animation, cars and rockets …
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
4
Outline of Chapters 8 & 9 - Machine
Architecture and Language Translation
 Machine Architecture - Chapter 8
– Let’s Build A Computer
– A Sample Architecture: The P88 Machine
– Programming the P88 Machines
 Language Translation - Chapter 9
–
–
–
–
–
Enabling the Computer to Understand Pascal
Syntactic Production Rules
Attaching Semantics to the Rules
The Semantics of Pascal
The Translation of Looping Programs
» (Not to be covered)
– Programming Languages Overview (if time allows!)
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
5
Machine Architecture - Chapter 8
 Let’s Build A Computer!!
 A Sample Architecture: The P88 Machine
 Programming the P88 Machine
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
6
Let Us Build A Computer
 CPU - Central Processing Unit
–
–
–
–
Computational registers
Instruction Register
Decoding circuitry - to select correct operations
Memory accessing circuitry
 Memory
– No brains - just a lot of bits! And addressing!
– Stores two kinds of information
» Data - used as variables in programs …
» Instructions - the ‘program’ to be executed
 Architectural Considerations
– How many and how specialized are the computational
registers?
– How many and what types of instructions?
– How complicated is memory access?
– Speed of operation of the processor?
– Multiple CPU’s - a parallel architecture??
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
7
The P88 Machine
 A Simple machine architecture - the P88
computer!
– Only one (computational) register for data manipulation
- AX
– Only 12 total instructions!
– Instruction register - IR
– Program counter - aka Instruction Pointer (IP)
– Also condition flag register (CF) – used for
remembering comparisons
– <See architecture diagram on p. 260 and next slide>
 Assumptions
– Use assembly language and not binary for instructions
– We will use the P88 program to show what is actually
happening inside the computer! As it is executing!
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
8
Computer Organization - CPU
Instruction
Pointer
Register
Code Deciphering
Circuits
Computation Circuitry
IP
IN
Address
OUT
Instruction
Register
IR
COPY
. . .
Condition
Flags
Register
Computation
Register
4/15/2003
CF
Memory
Instruction in Memory
Only one
. . .
Path activated A Multiplexer??
ADD
AX
CSCI 150: Introduction to Computer Science - Session 23
9
A Sample Architecture: The P88
Machine
 Architectural components of the P88
Instruction pointer register - IP
Instruction register - IR
Condition Flag - CF
Computational Register - AX
}
CPU
–
–
–
–
– Memory with addressing logic
 Sneak Preview of Simulator Program: COMP1
– For major hardware components
– (Use Alt-Enter to get full screen display)
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
10
BASIC EXECUTION CYCLE OF
P88 MACHINE
 (1 - Fetch)
– Find instruction in memory at the address currently in the IP
register.
– Take the instruction at that address in memory and load it into
the IR.
– Increment the IP to the address of the next instruction
Steps managed
By the clock
Circuit !!
 (2 - Execute)
– Execute whatever instruction is now in IR
 (3 - Loop back to fetch step again)
 A never ending loop - the fetch-execute cycle classic computer design
 Note that an instruction might modify IP !! I.e. a
Jump instruction!!
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
11
P88 Simulator(s)
 Programs COMP1 and COMP2 - available on the course
web site at:
http://mathcs.holycross.edu/~rbonneau/CourseFiles/P88
 Able to compile a subset of pascal into assembly language
– only supports integer vars = so no declarations needed!
 Execute assembly language program visually - showing
instructions, registers, and memory interactions!
 Has the Pascal IDE Environment look/feel!
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
12
P88 (COMP1) Program Interface
Pascal
Source
Assembly
Language
Code
In Memor
Data
in
Memory
CPU
Registers
CPU
Execution state
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
13
First simulator run!
 Let’s try it using on the NUMBER.PAS source file:
begin
a := (5 + 6);
end
 How? (Steps in doing P88 software development)
–
–
–
–
Load the P-Pascal program
Compile it
Examine the assembly language
Then execute it instruction by instruction, fetch/execute by
fetch/execute – use function key F9
– Observe all the registers and memory as computer executes
 Only two assembly instructions needed now
– COPY dest, source where one is a register and one is
memory location – copies data from the source to the
destination
– ADD AX,source – adds data from a source memory
address location to current contents of accumulator register
with result staying in the accumulator
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
14
Programming the P88 Machine
 Let’s now examine the full P88 Machine Instruction
Set (see page 265 and handout)
 General Classes of instructions
– load/store data:
accumulator)
– arithmetic:
– comparison:
– ‘jump’ :
– input/output:
4/15/2003
COPY (between memory and
ADD, SUB, MULT, DIV
CMP - (between memory and
accumulator)
sets the CF register
JMP, JB, JNB
these can change the IP!!
can test the CF
IN OUT - interaction with outside
world
CSCI 150: Introduction to Computer Science - Session 23
15
Copy Instructions
 Data flows between memory and CPU through the COPY
instructions
 Two variants:
COPY AX,<memory address>
AX <--(memory)
means load FROM memory into AX register
COPY <memory address>,AX
AX --> (memory)
means store AX contents to memory location
 In both cases, COPY <dest>,<source> data moves
from source to destination
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
16
Arithmetic Instructions
 Implement the 4 major arithmetic operations on integer
information
 All start with one piece of data already in AX and the other
piece of data in memory
 Result always ends up in AX
Assembly Instruction
ADD
SUB
MUL
DIV
AX,<memory>
AX,<memory>
AX,<memory>
AX,<memory>
Operation Performed
AX
AX
AX
AX
:=
:=
:=
:=
AX
AX
AX
AX
+
*
/
(memory
(memory
(memory
(memory
value)
value)
value)
value)
DIV is the same as Pascal Integer Divide operation – integer result only
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
17
Compare Instruction
 Only one instruction
 Compares the current contents of the accumulator
register (AX) with a memory location
 Will set value of the condition flag (CF) register
depending on result
Assembly Instruction
CMP
AX,<memory>
CF Register Value:
B = Memory is Bigger than AX
NB = Memory is NOT Bigger
4/15/2003
Operation Performed
If AX<(memory) then
CF = B
else
CF = NB
CSCI 150: Introduction to Computer Science - Session 23
18
Jump Instructions
 Three instructions in this class: First is unconditional, other
two are conditional
 First: Unconditional Jump Instruction
JMP <label>
Load addr of <label> into IP reg
(Note label is attached to another line of code in the program)
 Next: Conditional Jump Instructions
JB <label>
If CF = B then <label> -> IP
JNB <label>
If CF = NB then <label> -> IP
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
19
Input/Output Instructions
 Two instructions - one for input and one for
output
 Input instruction
IN
AX
Read integer from
user and store into AX
 Output instruction
OUT
4/15/2003
AX
Print the contents of
AX to user’s display
CSCI 150: Introduction to Computer Science - Session 23
20
Observations about P88 instructions
 A very simplified set of operations - most
architectures have quite a few more
instructions (e.g. procedure calls, etc.) and
complex memory addressing options
 The accumulation register AX gets used
in almost all operations - ‘where all the action
is’!
 The only instructions that affect the flow of
execution (I.e. the IP register) are the Jump
instructions (can use these to implement
looping, as we will see)
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
21
Simple assembly language code
 Simple assembly language sequence: Input,
Add two numbers, output to user
Equivalent
pascal code
– IN
AX ; input number from user into AX
– COPY a,AX; store into location labeled a
readln(a);
– IN
AX ; input another number from user
– COPY b,AX; store into location labeled b
readln(b);
– COPY AX,a; load location a into AX register
c := a + b;
– ADD AX,b; add location b to value in AX
– COPY c,AX; store sum into location labeled c
– COPY AX,c; load from location c into AX
– OUT AX ; print out sum to the user
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
writeln(c);
22
More Assembly Language Samples
 Look at the Pascal programs first and then the
generated assembly language programs
– Simplest program: ONE.PAS
– Let’s look at the very simple example: NUMBER.PAS
– To show looping: WHILE.PAS
Note:
COMP1 is
Very Fragile
Program might die at
any time!
– Most complicated: FACT.PAS - implements factorial
using a loop - let’s try with input of 4 ! Run at faster
speed?
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
23
Observations about Generated
Assembly Language Code
 Use of ‘generated’ constants: #C0 #C1 … show up
whenever there is some kind of constant number in
your program – e.g. X := 5 or while x < (n+1 )
 Temporary variables with names _E0, _E1, etc; used
to store partial results in memory locations. Used
when doing things like
– 2*(X+1) uses a temp for X+1 as well as a temp for 2*(X+1)
 New ‘instruction’ NO-OP = no-operation - just a place
where we can add a label for jumps!
 Notice any redundancy in the code sequences???
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
24
But how did we get here?
 We have seen HOW the computer can
execute the assembly language program - like
we could use CircuitMaker to run a circuit
 But where did the assembly language
statements come from?(Where did circuit
come from?)
 Or more specifically –
– How Do We Generate the Assembly
Language for a given Pascal Program?
 This is called Language TRANSLATION - The
Main Topic of Chapter 9
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
25
Language Translation - Chapter 9
 Enabling the Computer to Understand Pascal
 Syntactic Production Rules
 Attaching Semantics to the Rules
 The Semantics of Pascal
 The Translation of Looping Programs
(optional?)
 Programming Languages
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
26
Enabling the Computer to
Understand Pascal
 The computer hardware is unable to
understand the Pascal language
 But it can understand machine language or
even assembly language
 So how to get from Pascal to (P88) assembly?
 E.g. how to do:
Z := X + Y;
Pascal Source Code:
Hardware cannot execute
4/15/2003
copy
add
copy
copy
copy
AX,X
AX,Y
CN1,AX
AX,CN1
Z,AX
Assembly Language:
Hardware can execute
CSCI 150: Introduction to Computer Science - Session 23
27
Syntactic Production Rules
 Recall from Chapter 1: ’Arrow Notation’ : Rules to ‘describe the
Pascal statements’
 Example:
<statement> ==> <identifier > := <expression >
( a statement may be:
identifier := expression)
 We could use these types of rules to produce / write (all) valid
Pascal programs
 Every computer language has such a set of syntactic
production rules (with varying levels of complexity) also
known as the grammar of the language
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
28
Attaching Semantics to the Rules
 But how do we go from writing valid programs to
programs which can be executing programs?
 Two step process:
– Derive the appropriate set of production rules for a given program
» As you did for homework assignment
– Attach ‘meaning’ (aka semantics) to each rule as it is detected
» meaning takes the form of equivalent assembly language code
 Example:
– the assignment production rule <ident> := <expr>;
has the ‘meaning’
COPY AX,M(expr)
COPY M(ident),AX
Where M(expr)
means the memory
location allocated for
the expression
and M(ident) is the
memory location allocated
to the variable identifier
 Apply this process to the whole program - this is called
compiling - generating assembly code from source
code
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
29
The Semantics of Pascal
Key concept
in code
generation
of a
compiler
 Thus to EACH syntactic rule we must assign
the semantics or meaning of that rule
 See pages in the text for the semantics (code
fragment) for other production rules
– See page 287 for semantics of add and subtract rules!
– See next slide for semantics of the addition expression
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
30
Semantics of the Pascal Add Expression
R4: <e>  ( <e1> + <e2> ) :
M(<e>) = createname (for
temp variable)
code (<e> ) =
code ( <e1> )
code ( <e2> )
Assembly
code
COPY
AX,
M(<e1>)
generated
ADD AX, M(<e2>)
COPY M(<e>), AX
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
If there is
a match
with this
rule
Do these
steps in
the
compiler
31
Semantics of the entire program
 Just apply these rules repetitively (and
recursively) to all the productions as they
unfold and you will end up with a set of
assembly code which implements the intent of
the original pascal code!
 Can show more of this dynamically using
COMP2 - a Visual Compiler
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
32
COMP2 - Visual Compiler
Pascal
Source
Generated
Assembly
Code
Production
Rules with
Assembly
Code
4/15/2003
Consider ONE.PAS source
file again but this time
let’s watch the compilation
process! Then
NUMBER.PAS
CSCI 150: Introduction to Computer Science - Session 23
Data
Needed
By The
Program
33
The Translation of Looping
Programs (?)
 Skip this - much too complicated
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
34
Programming Languages
 Summary of popular computer languages
– Pascal - of course - developed primarily as a teaching
language - variants: Turbo Pascal, Object Pascal, Free Pascal
...
– Basic - teaching language - simple to build/run … a strong
descendant: Microsoft’s Visual Basic language
– COBOL - COmmercial Business Oriented Language
– Fortran - FORmula TRANslation language - numerical
calculations - science/engineering
– Lisp - “Lots of Insipid Stupid Parentheses”, highly recursive,
used for Artificial Intelligence projects
– Forth - small, fast, stack-based language for embedded (built
in to hardware) applications
– C - standardized, highly portable language of 70’s-90’s
– C++/Java - object oriented languages, derived from C
– C# - Microsoft’s version of Java (!)
– JavaScript – subset of Java used within browser-based
programs
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
35
Summary of Session Topics
 Computer Architecture
– hardware components
– instruction set
 Assembly Language
– how to program the instruction’s instruction set
 P88 - a simple, sample computer architecture
 COMP1 - P88 simulator
 PASCAL --> Assembly Language - Translation
 How does it happen?
 Production rules and semantics
 COMP2 : a Visual Compiler Pascal to P88
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
Pascal
Assembly
Language
P88
36
Next Session
 Tuesday, April 22nd
 A LAB – in Haberlin 408
 HTML and Web Pages!!!
 Using PFE and The Internet Explorer Browser
(IE)
4/15/2003
CSCI 150: Introduction to Computer Science - Session 23
37