Solving Problems using a Computer
Methodologies for creating computer programs
that perform a desired function.
Chapter 5
Problem Solving
• How do we figure out what to tell the computer to do?
• Convert problem statement into algorithm,
algorithm
using stepwise refinement.
• Convert algorithm into LC-3 machine instructions.
LC3 Programming
Debugging
• 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!
4-2
Stepwise Refinement
Problem Statement
Also known as systematic decomposition.
Because problem statements are written in English,
they are sometimes ambiguous and/or incomplete.
• Where is “file” located? How big is it, or how do I know
when I’ve reached the end?
• How should final count be printed? A decimal number?
• If the character is a letter, should I count both
upper-case and
d lower-case
l
occurrences?
?
Start with problem statement:
“We wish to count the number of occurrences of a character
in a file. The character in question is to be input from
the keyboard; the result is to be displayed on the monitor.”
Decompose task into a few simpler subtasks.
How do you resolve these issues?
• Ask the person who wants the problem solved, or
• Make a decision and document it.
Decompose each subtask into smaller subtasks,
and these into even smaller subtasks, etc....
until you get to the machine instruction level.
4-3
4-4
1
CuuDuongThanCong.com
/>
Three Basic Constructs
Sequential
There are three basic ways to decompose a task:
Do Subtask 1 to completion,
then do Subtask 2 to completion, etc.
Task
Get character
input from
keyboard
True
False
Test
condition
Subtask 1
Test
condition
Count and print the
occurrences of a
character in a file
False
Examine file and
count the number
of characters that
match
True
Subtask 1
Subtask 2
Subtask 2
Sequential
Subtask
Conditional
Iterative
Print number
to the screen
4-5
4-6
Conditional
Iterative
If condition is true, do Subtask 1;
else, do Subtask 2.
Do Subtask over and over,
as long as the test condition is true.
True
Test character.
If match, increment
counter.
file char
= input?
?
False
Check each element of
the file and count the
characters that match.
Count = Count + 1
more chars
to check?
False
True
Check next char and
count if matches.
4-7
4-8
2
CuuDuongThanCong.com
/>
Problem Solving Skills
LC-3 Control Instructions
Learn to convert problem statement
into step-by-step description of subtasks.
How do we use LC-3 instructions to encode
the three basic constructs?
• Like a puzzle, or a “word problem” from grammar school math.
¾What is the starting state of the system?
¾What is the desired ending state?
¾How do we move from one state to another?
• Recognize English words that correlate to three basic constructs:
¾“do A then do B” ⇒ sequential
¾“if G, then do H” ⇒ conditional
¾“for each X, do Y” ⇒ iterative
ắdo Z until W iterative
4-9
Code for Conditional
Test
Condition
ã Instructions naturally flow from one to the next,
so no special
p
instruction needed to go
g
from one sequential subtask to the next.
Conditional and Iterative
• Create code that converts condition into N, Z, or P.
Example:
Condition: “Is R0 = R1?”
Code: Subtract R1 from R0; if equal, Z bit will be set.
• Then use BR instruction to transfer control to the proper subtask.
4-10
Code for Iteration
Exact bits depend
on condition
being tested
True
Sequential
PC offset to
address C
Instruction
A
False
Exact bits depend
on condition
being tested
Generate
Condition
B 0000
?
Test
Condition
C
Instruction
A
False
Generate
Condition
0000
Subtask 1
Subtask 1
Subtask 2
0000 111
True
0000 111
Subtask 2
Unconditional branch
to Next Subtask
D
Next
Subtask
Assuming all addresses are close enough that PC-relative branch can be used.
C
PC offset to
address D
4-11
Next
Subtask
C
Subtask
C
Next
Subtask
?
B
Subtask
D
PC offset to
address C
Unconditional branch
to retest condition
Assuming all addresses are on the same page.
A
Next
Subtask
PC offset to
address A
4-12
3
CuuDuongThanCong.com
/>
Example: Counting Characters
Refining B
START
A
START
Initialize: Put initial values
into all locations that will be
needed to carry out this
task.
B
- Input a character.
- Set up a pointer to the first
location of the file that will
be scanned.
- Get the first character from
the file.
- Zero the register that holds
the count.
Input a character. Then
scan a file, counting
occurrences of that
character. Finally, display
on the monitor the number
of occurrences of the
character (up to 9).
B
STOP
C
Yes
B
Scan the file, location by
l
location,
ti
incrementing
i
ti the
th
counter if the character
matches.
Done?
No
B1
Test character. If a match,
increment counter. Get next
character.
Scan the file, location by
location, incrementing the
counter if the character
matches.
Display the count on the
monitor.
Initial refinement: Big task into
three sequential subtasks.
Refining B into iterative construct.
STOP
4-13
Refining B1
4-14
Refining B2 and B3
Yes
Done?
No
B2
Yes
B
Done?
Yes
Yes
B1
Done?
No
Test character. If a match,
increment counter. Get next
character.
No
B1
Yes
Done?
N
No
No
R2 = R2 + 1
B1
B2 Test character. If matches,
increment counter.
R1 = R0?
B2 Test character. If matches,
increment counter.
B3
B3 Get next character.
B3 Get next character.
R3 = R3 + 1
R1 = M[R3]
Refining B1 into sequential subtasks.
4-15
Conditional (B2) and sequential (B3).
Use of LC-2 registers and instructions.
4-16
4
CuuDuongThanCong.com
/>
The Last Step: LC-3 Instructions
Debugging
Use comments to separate into modules and
to document your code.
You’ve written your program and it doesn’t work.
Now what?
Yes
Done?
No
B2
Yes
R1 = R0?
No
R2 = R2 + 1
B3
R3 = R3 + 1
; Look at each char in file.
0001100001111100 ; is R1 = EOT?
0000010xxxxxxxxx ; if so, exit loop
; Check for match with R0
R0.
1001001001111111 ; R1 = -char
0001001001100001
0001001000000001 ; R1 = R0 – char
0000101xxxxxxxxx ; no match, skip incr
0001010010100001 ; R2 = R2 + 1
; Incr file ptr and get next char
0001011011100001 ; R3 = R3 + 1
0110001011000000 ; R1 = M[R3]
R1 = M[R3]
Don’t know
PCoffset bits until
all the code is done
What do you do when you’re lost in a city?
Drive around randomly and hope you find it?
3Return to a known point and look at a map?
In debugging, the equivalent to looking at a map
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.
4-17
Debugging Operations
4-18
LC-3 Simulator
Any debugging environment should provide means to:
1.
2.
3.
4.
execute
instruction
sequences
Display values in memory and registers.
Deposit values in memory and registers.
Execute instruction sequence in a program.
Stop execution when desired.
Different programming levels offer different tools.
•
•
set/display
registers
and memory
High-level languages (C, Java, ...)
usually have source-code debugging tools.
For debugging at the machine instruction level:
¾ simulators
¾ operating system “monitor” tools
¾ in-circuit emulators (ICE)
– plug-in hardware replacements that give
instruction-level control
stop execution,
set breakpoints
4-19
4-20
5
CuuDuongThanCong.com
/>
Types of Errors
Tracing the Program
Syntax Errors
Execute the program one piece at a time,
examining register and memory to see results at each step.
Single-Stepping
• You made a typing error that resulted in an illegal operation.
• Not usually an issue with machine language,
because almost any bit pattern corresponds to
some legal instruction.
• In high-level languages, these are often caught during the
translation from language to machine code.
code
• Execute one instruction at a time.
• Tedious, but useful to help you verify each step of your program.
Breakpoints
• Tell the simulator to stop executing when it reaches
a specific instruction.
ã Check overall results at specific points in the program.
ắ Lets you quickly execute sequences to get a
high-level overview of the execution behavior.
¾ Quickly execute sequences that your believe are correct.
Logic Errors
• Your program is legal, but wrong, so
the results don’t match the problem statement.
• Trace the program to see what’s really happening and
determine how to get the proper behavior.
Watchpoints
Data Errors
• Input data is different than what you expected.
• Test the program with a wide variety of inputs.
• Tell the simulator to stop when a register or memory location changes
or when it equals a specific value.
• Useful when you don’t know where or when a value is changed.
4-21
Example 1: Multiply
4-22
Debugging the Multiply Program
Single-stepping
This program is supposed to multiply the two unsigned
integers in R4 and R5.
clear R2
add R4 to R2
decrement R5
No
x3200
x3201
x3202
x3203
x3204
PC and registers
at the beginning
of each instruction
0101010010100000
0001010010000100
0001101101111111
0000011111111101
1111000000100101
R5 = 0?
Yes
HALT
PC
Set R4 = 10, R5 =3.
Run program.
Result: R2 = 40, not 30.
R2
R4
R5
Breakpoint at branch (x3203)
x3200
--
10
3
x3201
0
10
3
x3202
10
10
3
PC
x3203
10
10
2
x3203
10
10
2
x3201
10
10
2
x3203
20
10
1
x3202
3202
20
10
2
x3203
3203
30
10
0
x3203
20
10
1
x3203
40
10
-1
x3201
20
10
1
40
10
-1
x3202
30
10
1
x3203
30
10
0
x3201
30
10
0
x3202
40
10
0
x3203
40
10
-1
x3204
40
10
-1
40
10
-1
R2
R4
R5
Should stop looping here!
Executing loop one time too many.
Branch at x3203 should be based
on Z bit only, not Z and P.
4-23
4-24
6
CuuDuongThanCong.com
/>
Example 2: Summing an Array of Numbers
Debugging the Summing Program
This program is supposed to sum the numbers
stored in 10 locations beginning with x3100,
leaving the result in R1.
Running the the data below yields R1 = x0024,
but the sum should be x8135. What happened?
R1 = 0
R4 = 10
R2 = x3100
x3000
x3001
x3002
x3003
x3004
x3005
x3006
x3007
x3008
x3009
R1 = R1 + M[R2]
R2 = R2 + 1
R4 = R4 - 1
No
R4 = 0?
Yes
0101001001100000
0101100100100000
0001100100101010
0010010011111100
0110011010000000
0001010010100001
0001001001000011
0001100100111111
0000001111111011
1111000000100101
Example 3: Looking for a 5
This program is supposed to set
R0=1 if there’s a 5 in one ten
memory locations, starting at x3100.
Else, it should set R0 to 0.
R0 = 1, R1 = -5, R3 = 10
R4 = x3100,, R2 = M[R4]
[ ]
R2 = 5?
No
R3 = 0?
R4 = R4 + 1
R3 = R3-1
R2 = M[R4]
Yes
R0 = 0
Contents
x3100
x3107
x3101
x2819
x3000
--
--
--
x3102
3102
x0110
0110
x3001
3001
0
--
--
x3103
x0310
x3002
0
--
0
x3003
0
--
10
x3104
x0110
x3004
0
x3107
10
x3105
x1110
x3106
x11B1
x3107
x0019
x3108
x0007
x3109
x0004
4-25
HALT
No
Address
HALT
Yes
Start single-stepping program...
PC
R1
R2
R4
Should be x3100!
Loading contents of M[x3100], not address.
Change opcode of x3003
from 0010 (LD) to 1110 (LEA).
4-26
Debugging the Fives Program
x3000
x3001
x3002
x3003
x3004
x3005
x3006
x3007
3007
x3008
x3009
x300A
x300B
x300C
x300D
x300E
x300F
x3010
0101000000100000
0001000000100001
0101001001100000
0001001001111011
0101011011100000
0001011011101010
0010100000001001
0110010100000000
0001010010000001
0000010000000101
0001100100100001
0001011011111111
0110010100000000
0000001111111010
0101000000100000
1111000000100101
0011000100000000
4-27
Running the program with a 5 in location x3108
results in R0 = 0, not R0 = 1. What happened?
Address
Contents
x3100
9
Perhaps we didn’t look at all the data?
Put a breakpoint at x300D to see
how many times we branch back.
x3101
7
x3102
3102
32
x300D
1
7
9
x3101
x3103
0
x300D
1
32
8
x3102
x3104
-8
x300D
1
0
7
x3103
x3105
19
0
0
7
x3103
x3106
6
x3107
13
x3108
5
x3109
61
PC
R0
R2
R3
R4
Didn’t branch
back, even
though R3 > 0?
Branch uses condition code set by
loading R2 with M[R4], not by decrementing R3.
Swap x300B and x300C, or remove x300C and
branch back to x3007.
4-28
7
CuuDuongThanCong.com
/>
Example 4: Finding First 1 in a Word
Debugging the First-One Program
This program is supposed to return (in R1) the bit position
of the first 1 in a word. The address of the word is in
location x3009 (just past the end of the program). If there
are no ones, R1 should be set to –1.
Program works most of the time, but if data is zero,
it never seems to HALT.
R2[15] = 1?
Yes
No
decrement R1
shift R2 left one bit
No
Breakpoint at backwards branch (x3007)
PC
R1 = 15
R2 = data
R2[15] = 1?
Yes
x3000
x3001
x3002
x3003
x3004
x3005
x3006
x3007
x3008
x3009
0101001001100000
0001001001101111
1010010000000110
0000100000000100
0001001001111111
0001010010000010
0000100000000001
0000111111111100
1111000000100101
0011000100000000
HALT
R1
PC
R1
x3007
14
x3007
x3007
13
x3007
3007
3
x3007
12
x3007
2
x3007
11
x3007
1
x3007
10
x3007
0
x3007
9
x3007
-1
x3007
8
x3007
-2
x3007
7
x3007
-3
x3007
6
x3007
-4
x3007
5
x3007
-5
4
If no ones, then
th branch
b
h to
t HALT
never occurs!
This is called an “infinite loop.”
Must change algorithm to either
(a) check for special case (R2=0), or
(b) exit loop if R1 < 0.
4-29
4-30
Debugging: Lessons Learned
Trace program to see what’s going on.
• Breakpoints, single-stepping
When tracing, make sure to notice what’s
really happening, not what you think should happen.
2. Assembly Language
• In summing program,
program it would be easy to not notice
that address x3107 was loaded instead of x3100.
Test your program using a variety of input data.
• In Examples 3 and 4, the program works for many data sets.
• Be sure to test extreme cases (all ones, no ones, ...).
4-31
8
CuuDuongThanCong.com
/>
Human-Readable Machine Language
An Assembly Language Program
Computers like ones and zeros…
0001110010000110
Humans like symbols…
;
; Program to multiply a number by the constant 6
;
.ORIG x3050
LD
R1, SIX
LD
R2, NUMBER
AND
R3, R3, #0
; Clear R3. It will
; contain the product.
; The inner loop
;
AGAIN
ADD
R3, R3, R2
ADD
R1, R1, #-1 ; R1 keeps track of
BRp
AGAIN
; the iteration.
;
HALT
;
NUMBER .BLKW 1
SIX
.FILL x0006
;
.END
ADD
R6,R2,R6
; increment index reg.
Assembler is a program that turns symbols into
machine instructions.
ã ISA-specific:
close correspondence between symbols and instruction set
ắmnemonics for opcodes
ắlabels for memory locations
• additional operations for allocating storage and initializing data
4-33
4-34
LC-3 Assembly Language Syntax
Opcodes and Operands
Each line of a program is one of the following:
Opcodes
• reserved symbols that correspond to LC-3 instructions
ã listed in Appendix A
ắex: ADD, AND, LD, LDR, …
• an instruction
• an assember directive (or pseudo-op)
• a comment
Operands
Whitespace (between symbols) and case are ignored.
Comments (beginning with “;”)
; ) are also ignored.
•
•
•
•
•
An instruction has the following format:
LABEL OPCODE OPERANDS ; COMMENTS
optional
mandatory
registers -- specified by Rn, where n is the register number
numbers
b
-- indicated
i di
d by
b # (decimal)
(d i l) or x (hex)
(h )
label -- symbolic name of memory location
separated by comma
number, order, and type correspond to instruction format
¾ex:
ADD R1,R1,R3
ADD R1,R1,#3
LD R6,NUMBER
BRz LOOP
4-35
4-36
9
CuuDuongThanCong.com
/>
Labels and Comments
Assembler Directives
Label
Pseudo-operations
• placed at the beginning of the line
• assigns a symbolic name to the address corresponding to line
ắex:
LOOP ADD R1,R1,#-1
BRp LOOP
C
Comment
t
ã
ã
ã
ã
anything after a semicolon is a comment
ignored by assembler
used by humans to document/understand programs
tips for useful comments:
¾avoid restating the obvious, as “decrement R1”
¾provide additional insight, as in accumulate product in R6
ắuse comments to separate pieces of program
ã do not refer to operations executed by program
• used by assembler
• look like instruction, but “opcode” starts with dot
Opcode
Operand
Meaning
.ORIG
address
starting address off program
end of program
.END
.BLKW
n
allocate n words of storage
.FILL
n
allocate one word, initialize with
value n
.STRINGZ
n-character
string
allocate n+1 locations,
initialize w/characters and null
terminator
4-37
4-38
Trap Codes
Style Guidelines
LC-3 assembler provides “pseudo-instructions” for
each trap code, so you don’t have to remember them.
Use the following style guidelines to improve
the readability and understandability of your programs:
Code
Equivalent
Description
HALT
TRAP x25
Halt execution and print message to
console.
IN
TRAP x23
Print prompt on console,
console
read (and echo) one character from keybd.
Character stored in R0[7:0].
OUT
TRAP x21
Write one character (in R0[7:0]) to console.
GETC
TRAP x20
Read one character from keyboard.
Character stored in R0[7:0].
PUTS
TRAP x22
Write null-terminated string to console.
Address of string is in R0.
1. Provide a program header, with author’s name, date, etc.,
and purpose of program.
2. Start labels, opcode, operands, and comments in same column
for each line. (Unless entire line is a comment.)
3. Use comments to explain what each register does.
4. Give explanatory comment for most instructions.
5. Use meaningful symbolic names.
• Mixed upper and lower case for readability.
• ASCIItoBinary, InputRoutine, SaveR1
6. Provide comments between program sections.
7. Each line must fit on the page -- no wraparound or truncations.
• Long statements split in aesthetically pleasing manner.
4-39
4-40
10
CuuDuongThanCong.com
/>
Sample Program
Char Count in Assembly Language (1 of 3)
Count the occurrences of a character in a file.
;
;
;
;
;
;
;
;
;
Remember this?
Count = 0
(R2 = 0)
Done?
YES
(R1 ?= EOT)
Ptr = 1st file character
Convert count to
ASCII character
(R0 = x30, R0 = R2 + R0)
Print count
Match?
NO
(TRAP x21)
(TRAP x23)
HALT
Incr Count
Load char from file
x3000
R2 R2
R2,
R2, #0
R3, PTR
;
;
;
;
R2
R3
R0
R1
i
is counter,
t
i
initially
iti ll 0
is pointer to characters
gets character input
gets first character
R1, R3, #0
;
; Test character for end of file
;
TEST
ADD
R4, R1, #-4
; Test for EOT (ASCII x04)
BRz
OUTPUT
; If done, prepare the output
(R1 ?= R0)
Input char
from keybd
Initialization
.ORIG
AND
LD
GETC
LDR
NO
(R3 = M[x3012])
YES
Program to count occurrences of a character in a file.
Character to be input from the keyboard.
Result to be displayed on the monitor.
Program only works if no more than 9 occurrences are found.
(TRAP x25)
(R2 = R2 + 1)
(R1 = M[R3])
Load next char from file
(R3 = R3 + 1, R1 = M[R3])
4-41
4-42
Char Count in Assembly Language (2 of 3)
Char Count in Assembly Language (3 of 3)
;
; Test character for match. If a match, increment count.
;
NOT
R1, R1
ADD
R1, R1, R0 ; If match, R1 = xFFFF
NOT
R1, R1
; If match, R1 = x0000
BRnp
GETCHAR
; If no match, do not increment
ADD
R2, R2, #1
;
; Get next character from file.
;
GETCHAR ADD
R3, R3, #1 ; Point to next character.
LDR
R1, R3, #0 ; R1 gets next char to test
BRnzp TEST
;
; Output the count.
;
OUTPUT LD
R0, ASCII ; Load the ASCII template
ADD
R0, R0, R2 ; Covert binary count to ASCII
OUT
; ASCII code in R0 is displayed.
HALT
; Halt machine
;
; Storage for pointer and ASCII template
;
ASCII
.FILL x0030
PTR
.FILL x4000
.END
4-43
4-44
11
CuuDuongThanCong.com
/>
Assembly Process
First Pass: Constructing the Symbol Table
Convert assembly language file (.asm)
into an executable file (.obj) for the LC-3 simulator.
1. Find the .ORIG statement,
which tells us the address of the first instruction.
•
Initialize location counter (LC), which keeps track of the
current instruction.
2. For each non-empty line in the program:
a) If line contains a label,
label add label and LC to symbol table.
table
b) Increment LC.
– NOTE: If statement is .BLKW or .STRINGZ,
increment LC by the number of words allocated.
First Pass:
• scan program file
• find all labels and calculate the corresponding addresses;
this is called the symbol table
3. Stop when .END statement is reached.
Second Pass:
• convert instructions to machine language,
using information from symbol table
NOTE: A line that contains only a comment is considered an empty line.
4-45
4-46
Second Pass: Generating Machine Language
Practice
For each executable assembly language statement,
generate the corresponding machine language instruction.
Using the symbol table constructed earlier,
translate these statements into LC-3 machine language.
• If operand is a label,
look up the address from the symbol table.
Statement
Potential problems:
• Improper number or type of arguments
ắex: NOT R1,#7
ADD R1,R2
ADD R3,R3,NUMBER
ã Immediate argument too large
ắex: ADD R1,R2,#1023
ã Address (associated with label) more than 256 from instruction
¾can’t use PC-relative addressing mode
LD
R3,PTR
ADD
R4,R1,#-4
LDR
R1,R3,#0
Machine Language
BRnp GETCHAR
4-47
4-48
12
CuuDuongThanCong.com
/>
LC-3 Assembler
Object File Format
Using “assemble” (Unix) or LC3Edit (Windows),
generates several different output files.
LC-3 object file contains
This one gets
loaded into the
simulator.
• Starting address (location where program must be loaded),
followed by…
• Machine instructions
Example
p
• Beginning of “count character” object file looks like this:
4-49
0011000000000000
0101010010100000
0010011000010001
1111000000100011
.
.
.
.ORIG x3000
AND R2, R2, #0
LD R3, PTR
TRAP x23
4-50
Multiple Object Files
Linking and Loading
An object file is not necessarily a complete program.
Loading is the process of copying an executable image
into memory.
• system-provided library routines
• code blocks written by multiple developers
• more sophisticated loaders are able to relocate images
to fit into available memory
• must readjust branch targets, load/store addresses
For LC-3 simulator,
can load multiple object files into memory,
then start executing at a desired address.
Linking is the process of resolving symbols between
independent object files.
• system routines, such as keyboard input, are loaded
automatically
¾loaded into “system memory,” below x3000
ắuser code should be loaded between x3000 and xFDFF
ã each object file includes a starting address
• be careful not to load overlapping object files
• suppose we define a symbol in one module,
and want to use it in another
• some notation, such as .EXTERNAL, is used to tell assembler
that a symbol is defined in another module
• linker will search symbol tables of other modules to resolve
symbols and complete code generation before loading
4-51
4-52
13
CuuDuongThanCong.com
/>