Download Ch 8. Machine Architecture

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Great Ideas of CS with Java








Part 1 WWW & Computer programming in the language Java
Ch 1: The World Wide Web
Ch 2: Watch out: Here comes Java
Ch 3: Numerical computation & Function
Ch 4: Subroutines & Databases
Ch 5: Graphics
Ch 6: Simulation
Ch 7: Software engineering







Part 2 Understanding what a computer is and how it works
Ch 8: Machine architecture
Ch 9: Language translation
Ch 10: Virtual Environment for Computing
Ch 11: Security, Privacy, and Wishful thinking
Ch 12: Computer Communication





Part 3 Advanced topics
Ch 13: Program Execution Time
Ch 14: Parallel Computation
Ch 15: Noncomputability
1
SNU
IDB Lab.
Ch 8. Machine Architecture
Copyright © SNU IDB Lab.
SNU
IDB Lab.
Table of Contents




Machine and Language
P88 Machine Architecture
Programming the P88 Machine
Summary
3
SNU
IDB Lab.
Common Hardware Components
Standard Hardware
Organization

Processor (CPU)



Memory
Memory

(main & auxiliary)


(such as mouse and
keyboard)
Processor
(CPU)
Output
Devices


4
mouse, keyboard, etc.
Output device(s)

(such as video
display or printer)
main & auxiliary
holds data and instructions
Input device(s)

Input
Devices
Central Processing Unit
Interprets and executes the instructions
video display, printer, etc.
CPU and memory are physically
housed together
SNU
IDB Lab.
Physical Organization



Keyboard
Monitor
Chassis





CPU
memory
disk drives
I/O connectors
etc.
5
SNU
IDB Lab.
Two Kinds of Memory

Main memory



working area
temporarily stores program and data (while program is executing)
Auxiliary memory



permanent (more or less)
saves program and results
includes floppy & hard disk drives, CDs, tape, etc.
6
SNU
IDB Lab.
Main Memory Organization




Bit = one binary digit

Binary digit can have only
one of two values, 0 or 1
Byte = 8 bits
“Byte Addressable”

Main memory is a list of
numbered locations that
contain one byte of data in
each location
Number of bytes per data item
may vary
Address Data Byte
3021
1111 0000
3022
1100 1100
3023
1010 1010
3024
1100 1110
3025
0011 0001
3026
1110 0001
3027
0110 0011
3028
1010 0010
3029
…
7
Item 1: 2 bytes
stored
Item 2: 1 byte
stored
Item 3: 3 bytes
stored
Item 4: 2 bytes
stored
Next Item, etc.
SNU
IDB Lab.
Many Types of Programs


User-created applications
Existing applications





word-processor/editor
web browser
compiler or assembler
etc.
Operating System

DOS, MS- Windows(3.x, 95, 98, NT), MacOS, UNIX, etc.
8
SNU
IDB Lab.
Programming Language Hierarchy
High-Level Language (HLL)
Assembly Lanuage
Machine Language
Hardware
9
SNU
IDB Lab.
The highs and lows of programming languages ...

High-Level Language (HLL)

Machine Language (lowest level)

closest to natural language

least natural language for humans,
most natural language for hardware

words, numbers, and math symbols

not directly understood by hardware  just 0s and 1s

directly understood by hardware

“portable” source code (hardware
independent)

not portable (hardware dependent)

Java, C, C++, COBOL, FORTRAN,
BASIC, Lisp, Ada,

etc.
0011 0101 1100 0001
1110 0101 0001 1110
0101 1010 1100 0011
X=1;
Y=2;
Z= X+Y;
10
SNU
IDB Lab.
Assembly Language (middle level)




a more or less human readable version of machine language
words, abbreviations, letters and numbers replace 0s and 1s
easily translated from human readable to machine executable code
like machine code, not portable (hardware dependent)
COPY
ADD
COPY
COPY
COPY
AX, X
AX, Y
CN1, AX
AX, CN1
Z,AX
11
SNU
IDB Lab.
Getting from Source to Machine Code

“Compiler”  “Compile a program”
translating from a high-level language source code to machine (object, or
executable) code.

“Assembler”  “Assemble a program”
translating from assemble language source code to machine (object, or
executable) code.

Compilers need to know the specific target hardware
12
SNU
IDB Lab.
Compilers vs. Assemblers vs. Interpreters


Compilers and Assemblers

translation is a separate user step

translation is “off-line,” i.e. not at run time
Interpreters - another way to translate source to object code

interpretation (from source to object code) is not a separate user step

translation is “on-line,” i.e. at run time
Source
Code
Compiler,
Object
Code
Assembler,
Interpreter
13
SNU
IDB Lab.
Java Program Translation
Data for Java Program
Java Program



Both Compilation and Interpretation
Intermediate Code: “Byte Code”

portable low-level code

similar to assembly code,
but hardware independent

invisible to Java programmer
Bye Code Interpreter translates from
generic byte code to hardwarespecific machine code

Much easier than traditional
compilation  portability
Java Compiler
Byte-Code
Program
Java
Virtual
Machine
Byte-Code Interpreter
Machine-Language
Instructions
Computer Execution
of Machine-Language Instructions
Output of Java Program
14
SNU
IDB Lab.
Java Program Translation Including Linker
Previously Compiled Helper Programs
Java Program
Java Compiler
Data for Java Program
Java
Virtual
Machine
Byte-Code
Program
Byte-Code Interpreter
Machine-Language
Instructions
Linker
Computer Execution
of Machine-Language Instructions
Output of Java Program
15
SNU
IDB Lab.
Java Byte Code

Generated by Java compiler


Instead of generating machine language as most compilers do, the Java
compiler generates byte code.
Easily translated to machine language of various kinds of computers
 portability

Executed by Java interpreter

Invisible to programmer

You don't have to know anything about how byte code works to write a
Java program.
16
SNU
IDB Lab.
Why Byte Code?

Disadvantages:



requires both compiler and interpreter
slower program execution
Advantages:

Portability (very important!)


same program can run on computers of different types (useful with
the Internet)
Java compiler for new types of computers can be made quickly
17
SNU
IDB Lab.
Table of Contents




History of Computer
P88 Machine Architecture
Programming the P88 Machine
Summary
18
SNU
IDB Lab.
Overall P88 Machine Architecture

Actually programmer's view of the computer hardware
Figure 8.1
19
SNU
IDB Lab.
Programming over Machine Architecture

Basic Computer is very primitive


Can't understand things like WHILE
Use compiler to translate Pascal to Machine Language



numbers represent instructions
numbers represent data
Assembler Language is a little bit high-touch above
machine language


mnemonics for instructions
names for data locations
20
SNU
IDB Lab.
CPU & Memory (1/2)

CPU has four registers

Instruction Pointer (IP)


Instruction Register (IR)


tells what condition has just been observed.
Arithmetic/Computation Register (AX)


holds the instruction to be executed when it is brought in from memory.
Condition Flag (CF)


holds the address in memory of the instruction that will be executed next.
is where a computation occurs.
Memory 0
1
2
3
4
5
6
7
8
9
00
10
20
21
SNU
IDB Lab.
CPU & Memory (2/2)


CPU and Memory feature
The surrounding asterisks means the corresponding binary machine code
0011 0101 1001 0011
Figure 8.2
22
SNU
IDB Lab.
Fetch-execute cycle

Fetch-Execute cycle (Computer repeat it without end)

Fetch



Execute


find the instruction in memory at the address given by IP
put that instruction into IR.
execute the instruction in IR
Example
COPY AX, X
ADD AX, Y
COPY CN1, AX
COPY AX, CN1
COPY Z,AX
23
SNU
IDB Lab.
Figure 8.3
Figure 8.3
24
SNU
IDB Lab.
Figure 8.4
SNU
25
IDB Lab.
Table of Contents




History of Computer
P88 Machine Architecture
Programming the P88 Machine
Summary
26
SNU
IDB Lab.
Programming a Machine (1/9)

Simple Program


Java
z = x + y;
Assembly language
copy ax, x
add ax, y
copy z, ax
// copy the value of x to ax
// ax = ax + y
// copy the value of ax to z
27
SNU
IDB Lab.
Table 8.1: P88 Instruction Set
COPY AX, mem
COPY mem, AX
// AX  mem (AX: Computation Register)
// mem  AX

ADD
SUB
MUL
DIV
//
//
//
//

CMP AX, mem










JMP
JNB
JB
AX, mem
AX, mem
AX, mem
AX, mem
lab1
lab1
lab1
IN
AX
OUT AX
AX
AX
AX
AX




AX
AX
AX
AX
+ mem
- mem
+ mem
/ mem
// If AX < mem
** CF : Condition Flag
Then CF = B (below) Else CF = NB (not below)
// Go to instruction with label lab1
// Go to lab1 if CF = NB (Jump if not below)
// Go to lab1 if CF = B
(Jump if below)
// Read an integer from screen and Input it into AX
// Read an integer from AX and Output it to screen
28
SNU
IDB Lab.
Programming a Machine (2/9)

Fetch/Execute Cycles



Instructions COPY and ADD


Right to Left movement
Instructions IN and OUT



Get instruction from memory
Carry out instruction
like getInt and setInt
deals only with integers
Examples


number.as
sum.as
29
SNU
IDB Lab.
Programming a Machine (3/9)
0
1
2
a
#c5
#c6
copy ax, #c5
add ax, #c6
copy a, ax
0
5
6
number.as
Assembler program to perform:
a = (5 + 6);
30
SNU
IDB Lab.
Programming a Machine (4/9)
0
1
2
3
in ax
copy x, ax
in ax
copy y, ax
4
5
6
copy ax, x
add ax, y
copy z, ax
4
5
copy ax, z
out ax
6
7
8
x
y
z
0
0
0
Sample I/O:
<23
<16
>39
sum.as
Assembler program to perform:
{
x = a.getInt();
y = b.getInt();
z = (x + y);
c.setInt(z);
}
31
SNU
IDB Lab.
Programming a Machine (5/9)

Need to handle IF and WHILE situations
cmp instruction
jmp, jnb, jb instructions


Tracing (often the only way to understand)
"If" Example


largest.as
“Loop” Example

summer.as
32
SNU
IDB Lab.
Programming a Machine (6/9)
there
quit
r
s
in ax
copy r, ax
in ax
copy s, ax
copy ax, s
cmp ax, r
jnb there
copy ax, r
out ax
jmp quit
copy ax, s
out ax
halt
0
0
largest.as:
program to write out
the larger of two numbers read in:




Read num1 and num2;
If num1 > num2
then output num1
else output num2
Sample output:
<33
<44
>44
33
SNU
IDB Lab.
Programming a Machine (7/9)
0
1
2
3
4
5
6 #l0
7
8
9
10
11
12
13
14
15
16 #l1
17
18
copy
copy
copy
copy
in
copy
no-op
copy
cmp
jnb
copy
add
copy
in
copy
jmp
no-op
copy
out
40
41
42
43
44
45
0
limit
#c0
sum
x
#l0
#l1
ax, #c0
limit, ax
ax, #c0
sum, ax
ax
x, ax
ax, limit
ax, x
#l1
ax, sum
ax, x
sum, ax
ax
x, ax
#l0
ax, sum
ax
0
0
0
6
18
Java Equivalent
--------------------{
limit = 0;
sum = 0;
x = a.getInt();
while (limit < x)
{
sum = (sum + x);
x = a.getInt();
}
b.setInt(sum);
}
---------------------
summer.as
Program to add together
list of numbers until
zero is encountered.
Sample output.
<23
<14
<12
<0
>49
34
SNU
IDB Lab.
Programming a Machine (8/9)

Factorial Example (Iterative)



fact.as
Writing a Program from Scratch
Handling Lists or Arrays (Self Modifying Code)


E.g. sum of squares for input
quit on zero
35
SNU
IDB Lab.
Programming a Machine (9/9)
1
2
3
4
5
6 #l0
7
8
9
10
11
12
13
14
15
16
17
18
19 #l1
20
21
40 n
41 i
42 #c1
43 fact
44 _e0
note:
#l0
#l1
in
copy
copy
copy
copy
copy
add
copy
copy
cmp
jnb
copy
mult
copy
copy
add
copy
jmp
copy
out
halt
ax
n, ax
ax, #c1
i, ax
fact, ax
ax, n
ax, #c1
_e0, ax
ax, i
ax, _e0
#l1
ax, fact
ax, i
fact, ax
ax, i
ax, #c1
i, ax
#l0
ax, fact
ax
0
0
1
0
0
6
19
fact.as
Java equivalent (well almost)
--------------------{
n = a.getInt();
i = 1;
fact = 1;
while (i < (n+1))
{
fact = (fact * i);
i = (i + 1);
}
b.setInt(fact);
}
---------------------
36
Program to calculate the
factorial of a number
read in by using a loop.
(The previous example
for this problem,
written in Java, was
solved in class by using
a method invoking
itself recursively.)
SNU
IDB Lab.
Table of Contents




History of Computer
P88 Machine Architecture
Programming the P88 Machine
Summary
37
SNU
IDB Lab.
Summary


Overview the classic architecture of the great majority of
digital computers
The fetch-execute cycle on sequence of instructions



The Von Neumann Architecture
Learning the P88 assembly language
Demonstrate several sample P88 assembly programs
38
SNU
IDB Lab.


Ch8: Machine Architecture
Text Review Time
39
SNU
IDB Lab.