HTMT NNC C5
HTMT NNC C5
LC3 Programming
Solving Problems using a Computer
Methodologies for creating computer programs
that perform 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
• 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
Also known as systematic decomposition.
4-3
Problem Statement
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 lower-case occurrences?
4-4
Three Basic Constructs
There are three basic ways to decompose a task:
Task
True
Subtask 1 Subtask 2
Subtask 2 Subtask
Get character
input from
keyboard
Print number
to the screen
4-6
Conditional
If condition is true, do Subtask 1;
else, do Subtask 2.
Test character.
If match, increment Count = Count + 1
counter.
4-7
Iterative
Do Subtask over and over,
as long as the test condition is true.
4-8
Problem Solving Skills
Learn to convert problem statement
into step-by-step description of subtasks.
4-9
LC-3 Control Instructions
How do we use LC-3 instructions to encode
the three basic constructs?
Sequential
• Instructions naturally flow from one to the next,
so no special instruction needed to go
from one sequential subtask to the next.
Subtask 1
Subtask 1 Subtask 2
0000 111 D
C
Subtask 2
Unconditional branch
Next to Next Subtask D
Subtask Next PC offset to
Subtask address D
Assuming all addresses are close enough that PC-relative branch can be used.
4-11
Code for Iteration
PC offset to
Exact bits depend address C
on condition Instruction
being tested A
Generate
Test False
Condition
Condition
0000 ? C
True B
Subtask
Subtask
0000 111 A
C
Next
Subtask
Next Unconditional branch
Subtask to retest condition
PC offset to
address A
A
Initialize: Put initial values
into all locations that will be
needed to carry out this
START task.
- Input a character.
- Set up a pointer to the first
Input a character. Then location of the file that will
scan a file, counting be scanned.
occurrences of that - Get the first character from
character. Finally, display the file.
on the monitor the number - Zero the register that holds
the count.
of occurrences of the
character (up to 9).
B
Scan the file, location by
location, incrementing the
counter if the character
STOP matches.
Yes
Done?
B
Scan the file, location by No
location, incrementing the B1
counter if the character Test character. If a match,
matches. increment counter. Get next
character.
B Yes
Done?
Yes No
Done?
B1
No B2 Test character. If matches,
B1
increment counter.
Test character. If a match,
increment counter. Get next
character. B3 Get next character.
No
B2
Yes R1 = R0?
Done? Yes No
No
B1
R2 = R2 + 1
R1 = M[R3]
Don’t know
PCoffset bits until
all the code is done
4-17
Debugging
You’ve written your program and it doesn’t work.
Now what?
4-18
Debugging Operations
Any debugging environment should provide means to:
1. Display values in memory and registers.
2. Deposit values in memory and registers.
3. Execute instruction sequence in a program.
4. Stop execution when desired.
set/display
registers
and memory
4-20
Types of Errors
Syntax Errors
• 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.
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.
Data Errors
• Input data is different than what you expected.
• Test the program with a wide variety of inputs.
4-21
Tracing the Program
Execute the program one piece at a time,
examining register and memory to see results at each step.
Single-Stepping
• 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.
Watchpoints
• 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-22
Example 1: Multiply
This program is supposed to multiply the two unsigned
integers in R4 and R5.
clear R2
x3200 0101010010100000
x3201 0001010010000100
add R4 to R2
x3202 0001101101111111
decrement R5
x3203 0000011111111101
x3204 1111000000100101
No
R5 = 0?
Set R4 = 10, R5 =3.
Yes
Run program.
HALT Result: R2 = 40, not 30.
4-23
Debugging the Multiply Program
Single-stepping
PC R2 R4 R5
x3200 -- 10 3
Breakpoint at branch (x3203)
PC and registers
at the beginning x3201 0 10 3
of each instruction x3202 10 10 3 PC R2 R4 R5
x3203 10 10 2 x3203 10 10 2
x3201 10 10 2 x3203 20 10 1
x3202 20 10 2 x3203 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 Should stop looping here!
x3201 30 10 0
x3202 40 10 0
Executing loop one time too many.
x3203 40 10 -1
Branch at x3203 should be based
x3204 40 10 -1
on Z bit only, not Z and P.
40 10 -1
4-24
Example 2: Summing an Array of Numbers
This program is supposed to sum the numbers
stored in 10 locations beginning with x3100,
leaving the result in R1.
R1 = 0
R4 = 10
x3000 0101001001100000
R2 = x3100 x3001 0101100100100000
x3002 0001100100101010
R1 = R1 + M[R2]
R2 = R2 + 1 x3003 0010010011111100
x3004 0110011010000000
R4 = R4 - 1 x3005 0001010010100001
x3006 0001001001000011
No x3007 0001100100111111
R4 = 0?
x3008 0000001111111011
Yes x3009 1111000000100101
HALT 4-25
Debugging the Summing Program
Running the the data below yields R1 = x0024,
but the sum should be x8135. What happened?
Address Contents Start single-stepping program...
x3100 x3107
PC R1 R2 R4
x3101 x2819 x3000 -- -- --
x3102 x0110 x3001 0 -- --
x3002 0 -- 0
x3103 x0310
x3003 0 -- 10
x3104 x0110 x3004 0 x3107 10
x3105 x1110
x3106 x11B1 Should be x3100!
x3107 x0019
x3108 x0007 Loading contents of M[x3100], not address.
Change opcode of x3003
x3109 x0004 from 0010 (LD) to 1110 (LEA).
4-26
Example 3: Looking for a 5
This program is supposed to set
x3000 0101000000100000
R0=1 if there’s a 5 in one ten x3001 0001000000100001
memory locations, starting at x3100. x3002 0101001001100000
x3003 0001001001111011
Else, it should set R0 to 0. x3004 0101011011100000
x3005 0001011011101010
R0 = 1, R1 = -5, R3 = 10
R4 = x3100, R2 = M[R4]
x3006 0010100000001001
x3007 0110010100000000
x3008 0001010010000001
Yes x3009 0000010000000101
R2 = 5? x300A 0001100100100001
x300B 0001011011111111
No x300C 0110010100000000
R4 = R4 + 1 x300D 0000001111111010
No
R3 = 0? R3 = R3-1 x300E 0101000000100000
R2 = M[R4] x300F 1111000000100101
Yes x3010 0011000100000000
R0 = 0 HALT 4-27
Debugging the Fives Program
Running the program with a 5 in location x3108
results in R0 = 0, not R0 = 1. What happened?
Address Contents
Perhaps we didn’t look at all the data?
Put a breakpoint at x300D to see
x3100 9 how many times we branch back.
x3101 7
PC R0 R2 R3 R4
x3102 32 x300D 1 7 9 x3101
x3103 0 x300D 1 32 8 x3102
4-30
Debugging: Lessons Learned
Trace program to see what’s going on.
• Breakpoints, single-stepping
4-31
2. Assembly Language
Human-Readable Machine Language
Computers like ones and zeros…
0001110010000110
Humans like symbols…
ADD R6,R2,R6 ; increment index reg.
4-33
An Assembly Language Program
;
; 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
4-34
LC-3 Assembly Language Syntax
Each line of a program is one of the following:
• an instruction
• an assember directive (or pseudo-op)
• a comment
Whitespace (between symbols) and case are ignored.
Comments (beginning with “;”) are also ignored.
optional mandatory
4-35
Opcodes and Operands
Opcodes
• reserved symbols that correspond to LC-3 instructions
• listed in Appendix A
ex: ADD, AND, LD, LDR, …
Operands
• registers -- specified by Rn, where n is the register number
• numbers -- indicated by # (decimal) or x (hex)
• 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-36
Labels and Comments
Label
• 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
Comment
• 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
4-37
Assembler Directives
Pseudo-operations
• do not refer to operations executed by program
• used by assembler
• look like instruction, but “opcode” starts with dot
4-39
Style Guidelines
Use the following style guidelines to improve
the readability and understandability of your programs:
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-40
Sample Program
Count the occurrences of a character in a file.
Remember this?
Count = 0
(R2 = 0) YES
Convert count to
Done?
(R1 ?= EOT)
ASCII character
(R0 = x30, R0 = R2 + R0)
HALT
Incr Count (TRAP x25)
4-41
Char Count in Assembly Language (1 of 3)
;
; 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.
;
;
; Initialization
;
.ORIG x3000
AND R2, R2, #0 ; R2 is counter, initially 0
LD R3, PTR ; R3 is pointer to characters
GETC ; R0 gets character input
LDR R1, R3, #0 ; R1 gets first character
;
; Test character for end of file
;
TEST ADD R4, R1, #-4 ; Test for EOT (ASCII x04)
BRz OUTPUT ; If done, prepare the output
4-42
Char Count in Assembly Language (2 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
4-43
Char Count in Assembly Language (3 of 3)
;
; Storage for pointer and ASCII template
;
ASCII .FILL x0030
PTR .FILL x4000
.END
4-44
Assembly Process
Convert assembly language file (.asm)
into an executable file (.obj) for the LC-3 simulator.
First Pass:
• scan program file
• find all labels and calculate the corresponding addresses;
this is called the symbol table
Second Pass:
• convert instructions to machine language,
using information from symbol table
4-45
First Pass: Constructing the Symbol Table
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.
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
4-47
Practice
Using the symbol table constructed earlier,
translate these statements into LC-3 machine language.
ADD R4,R1,#-4
LDR R1,R3,#0
BRnp GETCHAR
4-48
LC-3 Assembler
Using “assemble” (Unix) or LC3Edit (Windows),
generates several different output files.
This one gets
loaded into the
simulator.
4-49
Object File Format
LC-3 object file contains
• Starting address (location where program must be loaded),
followed by…
• Machine instructions
Example
• Beginning of “count character” object file looks like this:
4-51
Linking and Loading
Loading is the process of copying an executable image
into memory.
• more sophisticated loaders are able to relocate images
to fit into available memory
• must readjust branch targets, load/store addresses