Chapter06

advertisement
Chapter 6
Low Level Programming
and Psuedocode
Chapter Goals - The BIG slices
• PIECE UNO: How a software works at the
lowest possible level
• PIECE DOS: How algorithms are created using
psuedocode
2
Chapter Goals – Smaller Slices
• operations that a computer can perform
• the Pep/8 virtual machine
• immediate addressing mode
direct addressing mode
• machine language and assembly language
3
Chapter Goals – Smaller Slices
• Low-level algorithms
• Using pseudocode to express and algorithm
• Pseudocode functionality (what can it do?)
• Translate a simple pseudocode algorithm into
assembly language
4
Some Definitions
Machine Code:
A binary code that tells the machine to do a particular instruction
Assembly Language
Uses “mnemonic” codes for instructions rather than “binary” codes.
Example: “Add” instead of “0111”
Pseudocode
Shorthand English-like language people use to express algorithms
(Each uses various levels of detail)
5
THE WHOLE
CHAPTER IN 4 SLIDES
Brought to you by…
6
SLIDE 1: What can Pep8 DO??
• Pep8 can do simple things like:
– Get characters from the keyboard
– Write characters to the screen
– Get numbers from the keyboard
– Write numbers to the screen
– Add numbers
– Store numbers to RAM
– Jump around in the program
– Etc…
7
SLIDE 2: A Pseudocode algorithm
Step 1)
Step 2)
Step 3)
Step 4)
Step 5)
Step 6)
Write an ‘H’ character to the screen
Write an ‘e’ character to the screen
Write an ‘l’ character to the screen
Write an ‘l’ character to the screen
Write an ‘o’ character to the screen
Stop
8
SLIDE 3: The Pep8 Assembly
CHARO
CHARO
CHARO
CHARO
CHARO
STOP
.END
0x0048,i
0x0065,i
0x006C,i
0x006C,i
0x006F,i
;Output an 'H'
;Output an 'e'
;Output an 'l'
;Output an 'l'
;Output an 'o'
;Tell Pep8 to stop
;Tell Assembler to stop
9
SLIDE 4: Assembled…
Assembly code is translated into binary for the
machine.
CHARO
0x0048,i ;Output an 'H'
Becomes…
500048
Which is really..
0101 0000
0000 0000 0100 1000
10
Algorithm, Assembly, and Machine
Code
11
Pseudocode
Pseudocode
A way of expressing algorithms using English
phrases and indention to make the steps in
the solution explicit
Example:
Step 1 ) Write an ‘H’ character to the screen
Etc…
12
Assembly Language
Assembly language
Uses mnemonic codes to represent machinelanguage instructions
Example:
CHARO
Instead of…
500048
0x0048,i
13
Machine Language
Machine language
Binary coded instructions built into the hardware
Circuits made of gates perform these instructions
Why would anyone choose to use machine language?
(Hint: they had no choice. Why?)
14
Machine Language
Characteristics of machine language:
– Each machine-language instruction does only one
very low-level task
– 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
15
An Assembler
Assembler
A program that translates assembly code into
machine code
16
More fun with
Assembly
17
Pep/8 Virtual Computer
Virtual computer
A hypothetical machine designed to contain the
important features of a real computer that we
want to illustrate
Pep/8
A virtual computer designed by Stanley Warford
that has 39 machine-language instructions
No; we are not going to cover all of them!
18
Pep/8 Assembly Language
Remember
the
difference
between
immediate
and
direct
addressing?
i : immediate
d: direct
19
Pep/8 Assembly Language
What is the difference between
operations and pseudo operations?
20
Pep/8 Assembly Language
Our Assembly Code for "Hello"
21
Assembly Process
22
Pep/8 Assembly Language
Our Assembly Code for "Hello“ … With Machine Code
23
Now that we have
some Pep8
vocabulary, we can
write some more
algorithms!
24
A New
Program
Problem:
Add 3 numbers
25
Pseudocode for Add 3 numbers
Step 1) load the sum (0) to the accumulator
Step 2) Read first number from the keyboard
Step 3) Add first number to the accumulator
Step 4) Read second number from the keyboard
Step 5) Add second number to the accumulator
Step 6) Read third number from the keyboard
Step 7) Add third number to the accumulator
Step 8) Store accumulator back to the sum
Step 9) Display the sum
Step 10) Stop
26
Our Completed Program
sum:
num1:
num2:
num3:
main:
BR
.WORD
.BLOCK 2
.BLOCK 2
.BLOCK 2
LDA
DECI
ADDA
DECI
ADDA
DECI
ADDA
STA
DECO
STOP
.END
main
0x0000
sum,d
num1,d
num1,d
num2,d
num2,d
num3,d
num3,d
sum,d
sum,d
27
More About
Pseudocode
We need to express more complicated
algorithms
Like Washing our Hair
28
A Algorithm for Hair
Step 0:
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Wet Hair
Apply shampoo
Lather
Rinse
If (Hair not clean enough) goto Step 1
Done
29
Pseudocode
With Pseudocode we can express…
Individual steps
As Well as Structure…
Repetition
Selection
30
Pseudocode Functionality
Repetition
Repeating a series of statements
Set count to 1
WHILE ( count < 10)
Write "Enter an integer number"
Read aNumber
Write "You entered " + aNumber
Set count to count + 1
How many values were read?
31
Pseudocode Functionality
Selection
Choose to execute one statement (or group of statements) or
another statement (or group of statements)
IF ( age < 12 )
Write "Pay children's rate"
Write "You get a free box of popcorn"
ELSE IF ( age < 65 )
Write "Pay regular rate"
ELSE
Write "Pay senior citizens rate"
32
Another
Program
Problem:
Sort pairs of numbers
33
Pseudocode Example
Problem: Read in pairs of positive numbers and print
each pair in order.
WHILE (not done)
Write "Enter two values separated by blanks"
Read number1
Read number2
Print them in order
34
Pseudocode Example
How do we know when to stop?
Let the user tell us how many
Print them in order?
If first number is smaller
print first, then second
If first number if larger
print second, then first
35
Pseudocode Example
Write "How many pairs of values are to be entered?"
Read numberOfPairs
Set numberRead to 0
WHILE (numberRead < numberOfPairs)
Write "Enter two values separated by a blank; press return"
Read number1
Read number2
IF(number1 < number2)
Print number1 + " " + number2
ELSE
Print number2 + " " number1
Increment numberRead
36
Our Completed Program
BR
mesg1:
mesg2:
numRead:
numPairs:
number1:
number2:
Main
.ASCII
.ASCII
.WORD
.BLOCK
.BLOCK
.BLOCK
"Enter number\x00"
"Enter pairs\x00"
0x00
2
2
2
Main:
STRO
DECI
mesg1, d
numPairs, d
Begin:
STRO
DECI
DECI
LDA
CPA
BRLE
BR
mesg2, d
number1,
number2,
number1,
number2,
less
greater
test:
LDA
ADDA
STA
CPA
BRLT
BR
numRead, d
1, i
numRead, d
numPairs, d
Begin
End
less:
DECO
DECO
BR
number1, d
number2, d
test
greater:
DECO
DECO
BR
number2, d
number1, d
test
End:
STOP
.END
d
d
d
d
Focus on the
structure
37
The Data Section
mesg1:
mesg2:
numRead:
numPairs:
number1:
number2:
.ASCII
.ASCII
.WORD
.BLOCK
.BLOCK
.BLOCK
"Enter number\x00"
"Enter pairs\x00"
0x00
2
2
2
38
A Code Section
Begin:
STRO
DECI
DECI
LDA
CPA
BRLE
BR
mesg2, d
number1,
number2,
number1,
number2,
less
greater
d
d
d
d
39
Let’s get Bitty
40
Architecture of Pep8
Pep/8 Registers/Status Bits Covered
– The program counter (PC) (contains the address
of the next instruction to be executed)
– The instruction register (IR)
(contains a copy of the instruction being executed)
– The accumulator (A register)
The memory unit is made up of 65,636 bytes of
storage
41
Architecture of Pep/8
Figure 6.1 Pep/8’s Architecture
42
Instruction Format
Figure 6.2 Pep/8 Instruction Format
43
Instruction Format
Operation code
Specifies which instruction is to be carried out
Register specifier
Specifies which register is to be used (only use A in this
chapter)
Addressing-mode specifier
Says how to interpret the operand part of the
instruction
44
Instruction Format
Figure 6.3 Difference between immediate addressing mode and
direct addressing mode
45
Instruction Format
Is there something we are not telling you
about the addressing mode specifier?
How can you tell?
46
Some Sample Instructions
Figure 6.4 Subset of Pep/8 instructions
47
Sample Instructions
What do these instructions mean?
48
Sample Instructions
What do these instructions mean?
49
Sample Instructions
What do these instructions mean?
50
Sample Instructions
What do these instructions mean?
Why is there only one on this page?
51
Sample Instructions
What do these instructions mean?
52
Written Algorithm of Hello
53
Hand Simulation
What is the fetch/execute cycle?
How much is the PC incremented?
54
Hand Simulation
What is the fetch/execute cycle here?
55
Pep/8 Simulator
Download