Download Programming Methodologies and Debugging in BME303 Intro to Computing - Prof. Orly Alter and more Exams Biology in PDF only on Docsity!
BME303 Intro. to Computing
Chapter 6: ProgrammingChapter 6: Programming
Methodologies for creating computer programs thatperform a desired functionperform a desired function.
Problem Solving
How do we figure out what to tell the computer to do?
Convert problem statement into algorithm,using
stepwise refinement
Convert algorithm into LC-3 machine instructions.
Debugging
gg
g
How do we figure out why it didn’t work?
Examining registers and memory, setting breakpoints, etc.
Time spent on the first can reduce time spent on the second!
BME303 Intro. to Computing
Stepwise RefinementStepwise Refinement
•Also known as systematic decomposition.
y
p
•Start with problem statement:
“We wish to count the number of occurrences of a character
We wish to count the number of occurrences of a character
in a file. The character in question is to be input fromthe keyboard; the result is to be displayed on the monitor.”
•Decompose task into a few simpler subtasks.•Decompose each subtask into smaller subtasks,and these into even smaller subtasks, etc....until you get to the machine instruction level.
y
g
BME303 Intro. to Computing
Three Basic ConstructsThree Basic Constructs
There are three basic ways to decompose a task:
Task
Subtask 1
Test
condition
Test
condition
True
False
False
Subtask 2
Subtask 1
Subtask 2
Subtask
True
Sequential
Conditional
Iterative
BME303 Intro. to Computing
Which construct?Which construct?
• As long as there is money in my bank
As long as there is money in my bankaccount, keep spending it…
Money in
False
o ey
account
True
spend Iterative
BME303 Intro. to Computing
Which construct?Which construct?
• Use credit card for purchases above $
Use credit card for purchases above $100,otherwise use checks or cash
Purchase
$100?
True
False
Credit card
Check/cash
Conditional
BME303 Intro. to Computing
How to ... using LC-
g
• Sequential
Sequential
– nothing special is needed (PC
Æ
PC+1)
• Conditional and Iterative
– use NZP condition code registers and BRExample: is R1 greater or equal to R5?
Subtract:
R1 – R
sets NZP
BME303 Intro. to Computing
Eg : Code for ConditionalEg.: Code for Conditional
Code: If R1 = 0, add 1, else subtract 1.Then, move result to R3.
Generate Condition
Instruction
A
Then, move result to R3.
0000
B
Subtask 1
?
C
C
Subtask 2
0000
111
D
Next
Subtask
D
BME303 Intro. to Computing
Eg : Code for ConditionalEg.: Code for Conditional
Code: If R1 = 0, add 1, else subtract 1.Then, move result to R3.
Generate Condition
Instruction
A
Then, move result to R3.
ADD R
R1+
0000
B
Subtask 1
?
C
BRz C
0000 010 “C”
ADD R
R1-
C
Subtask 2
0000
111
D
BRnzp D
0000 111 “D”
ADD R
R1+
Next
Subtask
D
ADD R
R1+
AND R
R1
BME303 Intro. to Computing
Eg : Code for IterationEg.: Code for Iteration
Counter: If R3>0, Add R1 to R2 store in R4, else stop.
Generate Instruction Condition
A
Condition
0000
B
Subtask
?
C
Subtask
C
Next
0000
111
A
Subtask
BME303 Intro. to Computing
Eg : Code for IterationEg.: Code for Iteration
Counter: If R3>0, Add R1 to R2 store in R4, else stop.
Generate Instruction Condition
A
ADD R
R3 + 0
Condition
0000
B
Subtask
?
C
BRn C
0000 100 “C”
ADD R
R1+R
Subtask
C
Next
0000
111
A
ADD R
R3 - 1
BRnzp A
0000 111 “A”
Subtask
BRnzp
A
A
HALT
BME303 Intro. to Computing
The EDSAC was the world's first stored-program computer to
p
g
p
operate a regular computing service. Designed and built atCambridge University, England, the EDSAC performed its firstcalculation on 6th May 1949.
BME303 Intro. to Computing
“As soon as we started programming, we
As soon as we started programming, we
found to our surprise that it wasn't as easyto get programs right as we had thought.
g
p
g
g
g
Debugging had to be discovered. I canremember the exact instant when I
li
d th t
l
t
f
lif
f
realized that a large part of my life fromthen on was going to be spent in findingmistakes in my own programs ”mistakes in my own programs.
- Maurice Wilkes, designer of EDSAC, on
programming 1949 programming, 1949
BME303 Intro. to Computing
BugsBugs
First bug was just that: a moth got into theFirst bug was just that: a moth got into thecomputer and short circuited it.
Bugs are
errors
made by the programmer – a
g
y
p
g
fault (mistake) in the design of a program.
Bugs typically cause execution errors, resulting
g
yp
y
g
in incorrect behavior.
ALL
programmers write code with bugs
- Skilled programmers make fewer errors and find
them more quickly.
BME303 Intro. to Computing
DebuggingDebugging
•You’ve written your program and it doesn’t work.
y
p
g
•Now what?
Wh t d
d
h
l
t i
it?
•What do you do when you’re lost in a city?
Drive around randomly and hope you find it?
Return to a known point and look at a map?
In debugging, the equivalent to looking at a map
is
tracing
your program.
is
tracing
your program.
- Examine the sequence of instructions being executed.• Keep track of results being produced.• Compare result from each instruction to the
expected
result
- Compare result from each instruction to the
expected
result.