Transcript CSIS-110

CSIS-110
Introduction to
Computer Science
Dr. Meg Fryling
“Dr. Meg”
Fall 2012
@SienaDrMeg
#csis110
CSIS-110
Lecture Sixteen Agenda
• Questions?
• Assignments
• CSI Ch 7: Low-Level Programming Languages
(Assembly and Machine Language)
CSIS-110
Assignments
• Readings: See course scheduled on syllabus
– The textbook readings are going to help you with
homeworks AND labs
– See pdf on Piazza
• Homework 3
– Due Tues (Tomorrow) at 11am
• Lab 7 – Instructor sub this week!
– Due at START of lab 6
– Coming to lab late does NOT extend your due
date/time
CSIS-110
Low-Level Programming:
Assembly and Machine
Language
Father of game theory. Mathematical contributions to set theory, ergodic theory,
operator theory. Contributions to quantum mechanics, logic, war, computing
theory and practice, economics. A modern legend.
CSIS-110
Computer Operations
• The essential Von Neumann concept of stored
program made the computer a programmable
device
• Data and instructions are in RAM
• Store, retrieve, and process are actions that
the computer can perform on data
CSIS-110
The Fetch-Execute Cycle
Figure 6.3 The Fetch-Execute Cycle
6
CSIS-110
Machine Language
• Machine language: the instructions built into
the hardware of a particular computer
• Initially, humans had no choice but to write
programs in machine language because other
programming languages had not yet been
invented
01110001
0000000000000011
e.g. Add 3 to the current value in
the A (accumulator) register
CSIS-110
Machine Language
• Every processor type has its own set
of specific machine instructions
• The relationship between the processor and
the instructions it can carry out is completely
integrated
• Each machine-language instruction does only
one very low-level task - “atomic”
CSIS-110
Instruction Format
• There are two parts to an instruction
– The Opcode (instruction specifier)
– And 1 or more arguments (operand specifier)
01110001
00000000 00000011
e.g. Add 3 to the current
value in the A register
Word length = 2 bytes for the Pep/8 Virtual Computer we will use
Some operations have no operands, and some have several
Different machine languages (chipsets) have different instructions
CSIS-110
Instruction Format
General Assembly Language
• There are two parts to an instruction
– The Opcode (instruction specifier)
– And 1 or more arguments (operand specifier)
Opcode
Operand
Operand
HALT
ADD
#42
LOAD
#10
STORE
#528
LOOP
#463
#23
Assembly languages assign mnemonic letter
codes in place of binary digits!
CSIS-110
Opcode
Mnemonic
Opcode
Meaning
Explanation
0000
LOAD X
contents of X ->R
Copy contents of X from memory to R
0001
STORE X
contents of R -> X
Copy contents of R to X
0010
CLEAR X
0 -> X
Set the contents of X to 0
0011
ADD X
R + X -> R
Set the contents of R to the contents X + R
0100
INCREMENT X
X -> X + 1
Set the contents of R to the contents of X + 1
0101
SUBTRACT X
R- X -> R
Set the contents of R to the contents of the Register to the contents of R - X
0110
DECREMENT X
X–> X -1
Set the contents of X to the contents of X -1
0111
COMPARE X
IF X > R THEN GT = ON
IF X = R
THEN EQ = ON
IF X< R
THEN LT = ON
Set the GT flag if contents of X > R
Set the EQ flag if contents of X = R
Set the LT flag if contents of X < R
1000
JUMP X
Transfer to location X
Copy location X to the Program Counter and use that as the next instruction
1001
JUMPGT X
Transfer to location X if GT
is on
1010
JUMPEQ X
Transfer to location X if EQ
is on
1011
JUMPLT X
Transfer to location X if LT
is on
1100
JUMPNEQ X
Transfer to location X if EQ
is off
1101
IN X
Input X
Input an integer value from the keyboard and store it in location X
1110
OUT X
Output X
Output, in decimal notation, the value stored in location X
1111
HALT
Stop program execution
Just what you’d think!
Page 202
CSIS-110
Figure 7.5 Assembly Process
CSIS-110
CSIS-110
Moving data around
RAM to ALU register
ALU register to RAM
one spot in RAM to another
•
•
•
LOAD X
STORE X
MOVE X Y
CSIS-110
Arithmetic
+, -, *, /, AND, OR, NOT, etc. Built-in
ADD X Y Z
(add contents of X & Y, put in Z)
ADD X Y
(add contents of X to Y, put in Y)
ADD X
(add contents of X to register, put result in
register)
CSIS-110
Variable address mapping:
a is mapped to #100
b is mapped to #101
c is mapped to #102
d is mapped to #103
The program:
4
8
2
#100
#101
#102
#103
• A Register–accumulator
• Program Counter (PC)
address of next instruction
• Instruction Register (IR)
copy of instruction
executing
Write machine language that the
following Python code would get
translated to:
d=a+b
CSIS-110
4
8
2
#100
#101
#102
#103
What is going on
in our CPU
registers while
this program is
running?
• A Register–accumulator
• Program Counter (PC)
address of next instruction
• Instruction Register (IR)
copy of instruction
executing
Fetch-Execute Cycle
1) Fetch the next instruction (from address in PC)
2) Decode the instruction (and update the program counter)
3) Get data (operand) if needed
4) Execute the instruction
The program:
LOAD #100
ADD #101
STORE #103
HALT
#110
#111
#112
#113
R
PC
IR
CSIS-110
2
#100 A
3
#101 B
1
#102 C
5
#103 D
GT EQ LT
#014
#015
#016
#017
#018
#019
B=A
Question 1
A=B+C
#020
#021
#022
#023
CSIS-110
GT EQ LT
2
#100 A
3
#101 B
1
#102 C
5
#103 D
LOAD #100
#014
STORE #102
#015
LOAD #103
#016
STORE #100
#017
HALT
#018
#019
B=A
A=B+C
#020
#021
#022
#023
CSIS-110