Your turn! Change the program so it adds only two numbers: always … · Your turn! Change the...
Transcript of Your turn! Change the program so it adds only two numbers: always … · Your turn! Change the...
Conclusions
5
• Creating a loop is easy: just branch
backward in the program!
• The hard part is avoiding an infinite loop.
• We need a way to make decisions, so that
the program can eventually exit the loop if some condition becomes true (e.g.it has
repeated 100 times)
Decision-making instructions
6
BR Set PC to operand unconditionally
BRLT i Set PC to operand if A < 0
BREQ i Set PC to operand if A = 0
Decision making problem: Same as previous “sum” program, but print the sum only if positive or zero; if negative, print an error message
7
negMsg: CHARO 0x0045,i
BR finish
main: LDA sum,d
…
BRLT negMsg
STA sum,d
DECO sum,d
finish: STOP
What ASCII code is this?
Decision making problem: Same as before, but print the sum only if positive or zero; if negative, print the letter ‘E’ (Error)
negMsg: CHARO 0x0045,i
BR finish
main: LDA sum,d
…
BRLT negMsg
STA sum,d
DECO sum,d
finish: STOP
How many ways are there to reach finish?
ASCII code for 'E'
EOL3
QUIZ: What does this program do?
9
BR main
a: .BLOCK 2
main: DECI a, d
LDA a, d
SUBA 0x002A, i
STA a, d
DECO a, d
STOP
.END
QUIZ: What does this program do?
10
BR main
a: .BLOCK 2
main: DECI a, d
LDA a, d
SUBA 0x002A, i
STA a, d
BREQ yes
DECO a, d
fin: STOP
yes: CHARO 0x0046, i
BR fin
.END
ASCII code for 'F'
QUIZ: Write an assembly program to subtract 2010 from 4210 and put the result in memory
11
BR main
sum: .WORD 0x0000
num1: .BLOCK 2
num2: .BLOCK 2
num3: .BLOCK 2
main: LDA sum,d
DECI num1,d
ADDA num1,d
DECI num2,d
ADDA num2,d
DECI num3,d
ADDA num3,d
STA sum,d
DECO sum,d
STOP
.END
Take inspiration from the
program we studied last time:
EOL
QUIZ: Would the decision program from last time still work if the negMsg block were placed after the main program instead of before?
12
main: LDA sum,d
…
BRLT negMsg
STA sum,d
DECO sum,d
finish: STOP
negMsg: CHARO 0x0045,i
BR finish
QUIZ: Change the program so it prints 'Z' (for zero) if the sum is zero, and prints the actual sum if nonzero.
13
main: LDA sum,d
…
BRLT negMsg
STA sum,d
DECO sum,d
finish: STOP
negMsg: CHARO 0x0045,i
BR finish
High-level software (Ch.9)
Algorithms (and data structures)
Low-level software
Components
Circuits
Gates
Transistors
6.5 Algorithms and Pseudocode
Algorithm = A sequence of steps for solving a problem
Muḥammad ibn Mūsā al-Khwārizmī (780-850A.D.)
16
Remember: Decision-making instructions
17
BR Set PC to operand unconditionally
BRLT i Set PC to operand if A < 0
BREQ i Set PC to operand if A = 0
How to describe an algorithm in an intuitive way?
18
Flowcharts!
Not in text
We use decision-making instructions to build:
• branches
• loops
Problems with flowcharts: • They’re hard to follow when they get complex
• They’re hard to draw in electronic documents
20
Not in text
Problems with flowcharts: • They’re hard to follow when they get complex
• They’re hard to draw in electronic documents
21
Not in text
Our text uses only pseudocode
6.5 Algorithms and Pseudocode
Pseudocode = A mixture of English and formatting to make the steps in an algorithm explicit
There are no syntax rules in pseudocode!
Pseudocode is not case sensitive!
Example: Repeated-division algorithm (convert base-10 number to other bases):
22
While ( the quotient is not zero )
Divide the decimal number by the new base
Make the remainder the next digit to the left in the answer
Replace the original decimal number with the quotient
… but pseudocode is more precise
24
IF concerned about cholesterol
Put butter substitute in a pot
ELSE
Put butter in a pot
Turn on burner
Put pot on the burner
WHILE (NOT bubbling)
Leave pot on the burner
Put other ingredients in the blender
Turn on blender
WHILE (more in pot)
Pour contents into lender in slow steam
Turn off blender
Draw the flowchart for this pseudocode
25
IF concerned about cholesterol
Put butter substitute in a pot
ELSE
Put butter in a pot
Turn on burner
Put pot on the burner
WHILE (NOT bubbling)
Leave pot on the burner
Put other ingredients in the blender
Turn on blender
WHILE (more in pot)
Pour contents into lender in slow steam
Turn off blender
Pseudocode functionality
Pseudocode has all of the concepts encountered in any high-level programming language, only the syntax is informal:
– Variables
– Assignment
– I/O
– Selection / decision
– Repetition / loop
– Boolean expressions
26
We can test a pseudocode algorithm w/pencil & paper, a.k.a. “desk checking”
28
What is 93 in base 8?
93/8 gives 11 remainder 5
11/8 gives 1 remainder 3
1/ 8 gives 0 remainder 1
answer 1 3 5
While ( the quotient is not zero )
Divide the decimal number by the new base
Make the remainder the next digit to the left in the answer
Replace the original decimal number with
29
Organizing the solution in a computer-like way gives us
better idea of what is required for the computer to
execute it, e.g. two numbers need to be entered, etc.
Translating a pseudocode algorithm
32
How we translate depends on the
language into which we are translating!
The text example (pp.184-5) is with
translation into PEP assembly, but in class
we’re translating the repeated division alg.
into Python!
6.6 Testing
34
Test plan:
• Code coverage, a.k.a. clear-box testing
• Data coverage, a.k.a. black-box testing
QUIZ: Testing
35
Is the test plan on p.187:
• Code coverage, a.k.a. clear-box testing?
• Data coverage, a.k.a. black-box testing?
Read and take notes in notebook:
Software Piracy and Copyrighting
Have you every "borrowed" software
from a friend?
Have you ever "lent" software to a
friend?
Did you know that about 100,000 jobs are
lost in the US every year due to such
"borrowing" and "lending?"
36
Chapter Review Questions
• List the operations that a computer can perform
• Describe the important features of the Pep/8 virtual machine
• Distinguish between immediate addressing mode and direct addressing mode
• Write a simple machine-language program
• Distinguish between machine language and assembly language
37
Chapter Review Questions
• Describe the steps in creating and running an assembly-language program
• Write a simple program in assembly program
• Distinguish between instructions to the assembler (a.k.a. directives) and instructions to be translated into machine code (a.k.a. executable instructions)
• Distinguish between following an algorithm and developing one
38
Chapter Review Questions
• Describe the pseudocode constructs used in expressing an algorithm
• Use pseudocode to express and algorithm
• Distinguish between black-box and clear-box testing
39
Homework Due Monday, Nov. 9
End of chapter exercises
10, 11, 12, 13, 14, 15
18, 20, 27, 34, 36, 41
Correction in 20: use E1 instead of E0
Hint for 34: machine code 31 means decimal input
Hint for 41: How are integers represented in Pep/8?
44, 45
• The next few slides are collected references for the PEP/8, just to have everything in one place …
41
All PEP instructions, with machine codes 00 Stop STOP
04 Branch unconditional BR
08 Branch if Less Than BRLT
0A Branch if equal BREQ
31 Decimal Input Direct DECI
38 Decimal Output Immediate DECO
39 Decimal Output Direct DECO
49 Char. Input Direct CHARI
50 Char. Output Immediate CHARO
51 Char. Output Direct CHARO
70 Add to A Immediate ADDA
71 Add to A Direct ADDA
80 Subtract from A Immediate SUBA
81 Subtract from A Direct SUBA
C0 Load into A Immediate LDA
C1 Load into A Direct LDA
E1 Store A Direct STA 45