Download Machine/Assembly Language

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

Algorithm characterizations wikipedia , lookup

Program optimization wikipedia , lookup

Go (programming language) wikipedia , lookup

Algorithm wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Interpreter (computing) wikipedia , lookup

Assembly language wikipedia , lookup

ILLIAC IV wikipedia , lookup

Transcript
CS101 Lecture 26:
Low Level Programming
John Magee
30 July 2013
Some material copyright Jones and Bartlett
1
Overview/Questions
– What did we do last time?
– How can we control the computer’s circuits?
– How does the von Neumann Computer work?
– What is machine language?
– What is computer programming?
2
1
What did we do last time?
– Reviewed the full adder (the circuit which
does binary number addition)
– Introduced some other circuits which are part
of the CPU: the accumulator, the inverter
– Described the main memory as a location
from which to read data into the adder, and
to which data can be stored from the
accumulator.
– Introduced the von Neumann architecture.
3
An Abstract View of CPU Circuits
Unanswered Questions:
How to specify whether to add or subtract.
How to specify memory (RAM) addresses.
4
2
Stored Program Computer
(Von Neumann Architecture)
Figure 5.1 The von Neumann architecture
5
Control Processing Unit
Central Processing Unit (CPU)
Refers to the combination of the
Arithmetic/Logic Unit and the Control Unit.
6
3
Arithmetic/Logic Unit
The ALU performs basic arithmetic
operations and logic operations. Examples:
– Arithmetic: addition, subtraction,
multiplication
– Logical operations: AND, OR, NOT, XOR
Most modern ALUs have a small amount of
special storage units called registers
– The accumulator is the “main” register
7
Arithmetic/Logic Unit
Recall how addition and subtraction happen
(at the level of the CPU):
Addition:
– take a value from memory
– send it to the adder
– add to the value in the accumulator
Subtraction:
– take a value from memory,
– invert the bits, send to the adder, set carry-in to 1
– add to the value in the accumulator
8
4
Arithmetic/Logic Unit
The difference between addition and
subtraction is slight; it can be controlled by
sending a signal to the inverter and to the
carry-in bit.
This signal is like flipping a switch to tell the
circuits to behave a certain way.
– But instead of flipping the switch, we could
just use a binary input to an AND gate…
9
Arithmetic/Logic Unit
Given that:
– instructions are carried out by sending data
through circuits
– circuits can be controlled by flipping
switches….
A binary code can be used to control which
operation the CPU will do at a given time.
10
5
Control Unit
The control unit coordinates the flow of
operations and data in the computer.
– coordinates the ALU and memory
Instruction register (IR)
Contains the binary code indicating which
instruction is being executed.
Program counter (PC)
Contains the address (in memory) of the next
instruction to be executed.
11
Flow of Information
Bus
A set of wires that connect all major
components of the computer.
Figure 5.2 Data flow through a von Neumann architecture
12
6
The Computer Clock
The computer has oscillator generates an endless
rapid stream of off/on pulses (0,1,0,1…).
Each change from 0 to 1 is called a clock cycle.
– cycles per second is one measure of CPU speed
– measured in Hertz (MHz, GHz) – cycles per second
The clock cycle is used to coordinate the tasks of
the various circuits.
13
The Fetch-Execute Cycle
Figure 5.3 The Fetch-Execute Cycle
14
7
Fetch Execute Cycle
The previous diagram shows how it takes 4
steps to execute each instruction.
Is cycles per second a valid measure of CPU
performance?
15
Stored Program Computer
(Von Neumann Architecture)
Data and instructions to manipulate the data
are logically the same (binary code) and can
be stored in the same place (RAM).
The machine language of a particular
computer is the set of binary coded
instructions built into its hardware.
16
8
Machine Language
Characteristics of machine language:
– Every processor type has its own set
of specific machine instructions.
– The relationship between the processor type
and the instructions it can carry out is
completely integrated.
– Each machine-language instruction does only
one very low-level task.
17
Machine Language Example
A Virtual Computer is a hypothetical machine
designed to contain the important features of a
real computer that we want to illustrate.
Our examples will use the Pep/7 virtual computer
designed by Stanley Warford that has 32 machinelanguage instructions.
– Note: different than the Super Simple CPU.
–
–
http://www.rsu.edu/Faculty/PMacpherson/Programs/pep7.html
http://www.rsu.edu/faculty/PMacpherson/Programs/tutorial.htm
18
9
Instruction Format
The machine language of a particular computer is the set of
binary coded instructions built into its hardware.
This machine
language has
a 3-byte
instruction
format.
Figure 7.2 The Pep/7 instruction format
19
Instruction Format
Operation Code
Specifies which instruction
is to be carried out.
Register Specifier
Specifies which register
is to be used.
Addressing-mode Specifier
How to interpret the operand part of the instruction.
–
–
00 for immediate (value given in operand)
01 for direct (memory address given in operand)
20
10
Some Sample Instructions
Figure 7.3 Subset of Pep/7 instructions
21
Sample Instructions
What does this instruction mean?
“Load immediate value 7 into the accumulator”
22
11
Sample Instructions
What does this instruction mean?
“Load value from address 31 into the accumulator”23
Sample Instructions
What does this instruction mean?
“Store value from accumulator into address 10”
24
12
Sample Instructions
What does this instruction mean?
“Add value from address 522 into the accumulator” 25
Sample Instructions
What does this instruction mean?
“Read a character of input into address 10”
26
13
Sample Instructions
What does this instruction mean?
“Send a character from address 10 to output”
27
Computer Programming
The CPU does some low-level tasks, and each one
is specified as a machine-language instruction.
– How do we get the CPU to do stuff for us?
Computer programming is the process of analyzing
a problem, designing a solution, and expressing
that solution as a series of computer instructions.
Computer programs must be written in the
language the computer understands.
28
14
Programming Strategy
Divide and Conquer!
Break up a large problem into smaller units and solve each
smaller problem.
– Applies the concept of abstraction.
– The divide-and-conquer approach can be applied over and over
again until each subtask is manageable.
29
Machine Language Example
Hello, world! program
The classic “first” computer program is called “Hello World”
It prints the words “Hello, world” to the screen.
How to write it in Machine Language for the Pep/7?
– Specify explicit tasks in unambiguous language.
– Divide and conquer: Keep refining these tasks until we arrive at
a concrete set of instructions which map to machine language
instructions.
30
15
Writing a Computer Program
Write "Hello, World!"
Is this concrete yet?
Write "Hello, World!"
Write "H"
Write "e"
Write "l"
Write "l"
Write "o"
Is this concrete yet?
31
Writing a Computer Program
Write "H"
Write 0x48
Is this concrete yet?
Write "e"
Write 0x65
.
.
.
Write "o"
Write 0x6F
32
16
Writing a Computer Program
33
Machine Language
Machine language is the set of binary coded
instructions built into the hardware of a
particular computer, and used directly by
ALU/Control Unit.
– why are the instructions binary encoded?
– how does this encoding relate to the
hardware?
34
17
Review: Machine Language
Operation Code
Specifies which instruction
is to be carried out.
Register Specifier
Specifies which register
is to be used.
Addressing-mode Specifier
How to interpret the operand part of the instruction.
–
–
00 for immediate (value given in operand)
01 for direct (memory address given in operand)
35
What the Computer Can Do
Modern computer processors can do the following
types of basic tasks (CPU instructions):
–
–
–
–
–
Add, subtract, multiply, divide, increment, decrement
Logical AND, OR, XOR, NOT, and NEG operations
Load data in from RAM, store data out to RAM
Load data from/ send data to input/output
Compare register contents for equal to Zero, Less
than Zero, Negative
– Jump to another instruction (by address)
Note: by data, we mean one word (e.g. 32 bits)
36
18
The JUMP Instruction
Recall that the von Neumann computer stores data
and instructions in the same RAM.
– The Program Counter holds the address of the next
instruction to be executed. Usually, to find the next
instruction, the PrC increments the stored address.
The JUMP instruction changes the address stored
in the Program Counter programmatically.
– Conditionally: jump if accumulator equals 0
– Conditionally: jump if accumulator is less than 0
– Unconditionally: just jump
37
The JUMP Instruction
The JUMP instruction enables decision making and
repetition inside the CPU.
One could argue that JUMP is what makes a
computer really programmable.
Examples (with some abstraction):
– If a value is less than 0, then jump to instruction 7…
– If the quotient is 0, then end the program.
38
19
Lab 09
Learning Objective
To deepen your understanding of the
central processor unit, the fetch-execute
cycle, and the stored program concept.
In addition, you will use pseudo code to
express an algorithm, and translate the
algorithm into machine-language
instructions which the CPU can execute.
39
40
20
Super Simple CPU
– Input: 1 word (16 bits)
– Output 1 word (16 bits)
– Registers:
 Program Counter
 Accumulator
 Temp
 Instruction Register
– RAM: 16 words (16 bits each)
http://www.cs.bu.edu/courses/cs101/labs/ECS_2e/Applets/APPLETS/CPU/applet_frame.htm
41
SSCPU Machine Language
Note: the Super Simple CPU has a different
instruction format and opcode list than the Pep/7
CPU.
Why? What are the implications of this?
SSCPU Instruction Format:
– Each instruction is 16 bits
– First 4 bits specify the operation code (opcode)
– Last 12 bits specify either a RAM address, or an
immediate value (a.k.a. a literal number)
42
21
SSCPU Machine Language
Some example instructions:
– 1111: stop the program
– 0001: add operand to accumulator
– 0010: subtract operand from accumulator
– 0011: load memory cell into accumulator
– 0101: store accumulator into memory cell
– 0110: input value into accumulator
– 0111: output value from accumulator
43
Lab Exercise
You will use a simple, simulated CPU to:
– observe the CPU’s fetch-execute cycle in action
– become acquainted with machine language
programming and instructions
– experiment with programming in a machine language
You will write a pseudo code algorithm, and a
machine language program to do addition of 2
numeric inputs.
44
22
Pseudo code
Pseudo code is a way of expressing algorithms
that uses a mixture of English phrases and
indention to make the steps in the solution explicit.
– There are no grammar rules in pseudocode.
– Pseudocode is not case sensitive.
Analogy: pseudo code is like an outline for writing
a paper -- it contains all of the main ideas, but is
not tied to the structure of grammar, etc.
45
Algorithms
Algorithm
A set of unambiguous instructions for solving a
problem or subproblem in a finite amount of time,
and using a finite amount of data.
For a given set of inputs, the algorithm will always
produce the same outputs.
46
23
Following an Algorithm
Grandpa Ron’s Chocolate Kahlua Mudslides
Ingredient List:
½ cup vodka
½ cup Kahlua
½ cup Bailey’s Irish Cream
¼ cup Hershey’s chocolate
1 blender full of ice
syrup
Process:
Fill blender with ice.
Pours other ingredients onto ice.
Blend until smooth consistency.
Pour into glasses and enjoy.
47
Following an Algorithm
Algorithm for preparing Chocolate Mudslides:
If not all ingredients are present
Go to store and buy missing ingredients
Fill blender with ice
Pour other ingredients into the blender
While (not smooth)
Turn on blender
Wait 10 seconds
Turn off blender
Test consistency with wooden spoon
Pour into glasses and serve
48
24
Partial Summary
– Even the modern computer is still a moronic
number-crunching, data moving device.
– Algorithms describe the explicit steps the
computer must follow to solve a problem.
– Pseudocode is a tool to help us express an
algorithm, but it is not understood by the
computer.
– Why would anyone want to write a program
in machine language?
49
Assembly Language
Assembly language
A language that uses mnemonic codes to
represent machine-language instructions
Mnemonic code examples:
– LOADA means “load value into accumulator”
– ADDA means “add value into accumulator”
– STOREA means “store value from accumulator”
50
25
The Assembly Process
Assembler
A program that reads each of the instructions
in mnemonic form and translates it into the
machine-language equivalent.
51
Pep/7 Assembly Language
Mode specifier: i = immediate, d = direct (memory)
52
26
Pep/7 Assembly Language
An assembly language can have pseudo
operations which do not map directly to the
underlying machine language operations.
53
Example: Pseudo Code Program
Reading and adding three numbers
Set sum to 0
Read num1
Add num1 to sum
Read num2
Add num2 to sum
Read num3
Add num3 to sum
Write sum
54
27
Example: Assembly Program
55
Partial Summary
– Assembly language: much easier to
write/read than machine language!
– Why not just use assembly language instead
of machine language?
– Who wrote the assembler program, and what
language was it written in?
– Why not just write programs in pseudo code,
and have a program translate that into
machine language instructions?
56
28
Take-Away Points
–
–
–
–
–
Von Neumann Architecture
Arithmetic/Logic Unit
Control Unit
Machine Language
Divide and Conquer strategy
57
Student To Dos
– Readings:
 Reed ch 7, pp 128-134
 Reed ch 6
– Homework on course schedule.
– Wednesday:
 Meet in CS teaching lab.
58
29