Title: Discussion
1Schedule
2Powerful Language
- Moses 631, 713
- 31 And when Enoch had heard these words, he
bowed himself to the earth, before the Lord, and
spake before the Lord, saying Why is it that I
have found favor in thy sight, and am but a lad,
and all the people hate me for I am slow of
speech wherefore am I thy servant? - 13 And so great was the faith of Enoch that he
led the people of God, and their enemies came to
battle against them and he spake the word of the
Lord, and the earth trembled, and the mountains
fled, even according to his command and the
rivers of water were turned out of their course
and the roar of the lions was heard out of the
wilderness and all nations feared greatly, so
powerful was the word of Enoch, and so great was
the power of the language which God had given him.
3Chapter 6Programming the LC-3
4Systematic Decomposition
IDEA
Step by Step Procedure
- Definiteness
- Each step is precisely stated.
- Effective Computability
- Each step can be carried out.
- Finiteness
- Must terminate.
5Stepwise Refinement
- Also known as systematic decomposition.
- Start with problem statement
- We wish to count the number of occurrences of a
characterin 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.
6Problem 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 knowwhen Ive reached the end? - How should final count be printed? A decimal
number? - If the character is a letter, should I count
bothupper-case and lower-case occurrences? - How do you resolve these issues?
- Ask the person who wants the problem solved, or
- Make a decision and document it.
7Three Basic Constructs
8Sequential
Do Subtask 1 to completion, then do Subtask 2 to
completion, etc.
R08
0x3000 AND R0, R0, 0 0x3001 ADD R0, R0, 8 0x3002
AND R1, R1, 0 0x3003 ADD R1, R1, 2 0x3004 HALT
R12
9Conditional Construct
0x3000 AND R1, R1, R1 0x3001 BRz 2 0x3002 ADD R2,
R2, 1 0x3003 BRnzp 1 0x3004 ADD R2, R2,
2 0x3005 HALT
True
False
R10?
R2R21
R2R22
10Iterative Construct
0x3000 ADD R3, R2, -5 0x3001 BRz 2 0x3002 ADD
R2, R2, -1 0x3003 BRnzp -4 0x3004 HALT
False
R2?5?
R2 R2-1
11LC-3 Control Instructions
- How do we use LC-3 instructions to encodethe
three basic constructs? - Sequential
- Instructions naturally flow from one to the
next,so no special instruction needed to gofrom
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.
12Code for Conditional
PC offset toaddress C
Exact bits depend on condition being tested
Unconditional branchto Next Subtask
PC offset toaddress D
Assuming all addresses are close enough that
PC-relative branch can be used.
13Code for Iteration
PC offset to address C
Exact bits depend on condition being tested
Unconditional branchto retest condition
PC offset toaddress A
Assuming all addresses are on the same page.
14Counting Characters in a List
- Problem
- 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. - Procedure
- Input a character from the keyboard
- Count how many times that character appears in
list - Output the count in ASCII
- Halt
15Counting Characters
- Input a character.
- Scan a file and count the occurrences of that
character. - Display in the monitor the count.
16Counting Characters
17Counting Characters
18Counting Characters
19The Last Step LC-3 Instructions
- Use comments to separate into modules and to
document your code.
20All Instructions
21The Last Step LC-3 Instructions
- Use comments to separate into modules and to
document your code.
Look at each char in file. 0001100001111100
is R1 EOT? 0000010xxxxxxxxx if so, exit
loop Check for match with 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 MR3
Dont know PCoffset bits until all the code is
done
22Program (1 of 2)
Counting Characters
23Program (2 of 2)
Counting Characters
24ASCII Characters
25Debugging
- Term bug used in Thomas Edisons lifetime to
mean industrial defect - Hawkin's New Catechism of Electricity (1896)
- The term "bug" is used to a limited extent to
designate any fault or trouble in the connections
or working of electric apparatus. - First computer bug
- Found in a primitive calculator
- on 9 September 1945
- The actual moth is now on display
- in the Smithsonian
26Debugging
- Youve written your program and it doesnt work.
- Now what?
- What do you do when youre 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
mapis 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.
27Debugging Operations
- Any debugging environment should provide means
to - 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. - 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
28LC3 Simulator
Execute Instruction Sequence
Stop Execution
Memory and Registers
Set Breakpoints
Display
29Types 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 (Semantic Errors)
- Your program is legal, but wrong, so the results
dont match the problem statement. - Trace the program to see whats really happening
and determine how to get the proper behavior. - Algorithmic Errors
- Program works exactly as designed but design is
flawed. - Caused by incorrect assumptions or incomplete
understanding.
30Adding a List of Numbers
Program
Register Assignments
Flowchart
sum ---- R3 i ---- R2 a ptr ---- R1
sum 0 for (i -12 i lt 0 i) sum sum
ai (addr)0x3100 0x3101 0x310b Sum
a-12 a-11 ... a-1
R1 0x3100 R3 0 R2 -12
yes
R2 0?
no
Machine-Code
R4 Mem R1 R3 R3 R4 increment
R1 increment R2
Address x3000 1110 001 0 1111 1111 LEA R1
x3100 R1 x3001 x00FF x3100 x3001 0101 011
011 1 00000 AND R3, R3, 0 R3 0 x3002 0101
010 010 1 00000 AND R2, R2, 0 R2
0 x3003 0001 010 010 1 10100 ADD R2, R2, -12
R2 -12 x3004 0000 011 0 0000 0101 BRzp
x300A branch past rest of code ( x3005 x0005
) x3005 0110 100 001 000000 LDR R4, R1, 0 R4
Mem R1 0 (load next value from
a) x3006 0001 011 011 0 00 100 ADD R3, R3, R4
do the summing x3007 0001 001 001 1 00001 ADD R1,
R1, 1 increment R1 (a ptr) x3008 0001 010 010 1
00001 ADD R2, R2, 1 increment R2 ( i
) x3009 0000 111 1 1111 1010 BRnzp x3004 jump
back up to test ( x300a x0006 )
31Chapter 7 Assembly Language
32Are you getting tired of 1s and 0s?
- 0001010010000010 or ADD R2, R2, R2
- 1001011011111111 or NOT R3, R3
- etc
33Remember levels of abstraction?
Problems
Algorithms
High Level Languages
Language
Assembly code
Machine (ISA) Architecture
Machine code
Microarchitecture
LC-3 Architecture
Circuits
Logic gates, multiplexers, memory, etc.
Devices
Transistors
34High Level vs. Assembly Languages
- High Level Languages
- More user friendly
- More ISA independent
- Each high-level statement translates to several
instructions in the ISA of the computer. - Assembly Languages
- Lower level, closer to ISA. Very ISA-dependent.
- Each instruction specifies a single instruction
in the ISA. - Makes low level programming more user friendly.
35What does assembly code do for us?
- Allows us to work at a slightly higher level than
machine language. - Allows us to use symbolic names for opcodes
- Allows us to use symbolic names for memory
locations - SUM, PRODUCT
- Dont need to know every address of every storage
location. - Calculates addresses for us really a big deal!
- Helps to allocate memory locations.
- Provides additional error checking
- Are instructions specified correctly?
36Assembly Code Example
Multiply a number by the constant 6
37Assembly Code Example
Multiply a number by the constant
6 .ORIG x3000 LD R1, SIX first load the
constant 6 LD R2, NUMBER load the number to
multiply AND R3, R3, 0 Clear R3 (hold
product) inner loop follows AGAIN ADD R3, R3,
R2 keep a running sum ADD R1, R1, -1 R1
contains the iteration count BRp AGAIN Do
again if R1 is positive done TRAP x25
Halt the machine constants NUMBER .BLKW 1
store the number to multiply here. SIX
.FILL x6 constant 6 .END
38LC-3 Assembly Language Syntax
- Each line of a program is one of the following
- an instruction
- an assembler directive (or pseudo-op)
- a comment
- White space (between symbols) and case are
ignored. - Comments (beginning with ) are also ignored.
- An instruction has the following format
LABEL OPCODE OPERANDS COMMENTS
39Opcodes and Operands
40Opcodes and Operands
- Opcodes and operands are necessary in every
instruction - Pages 526-542 show the syntax for every LC-3
opcode. - Number of operands varies based on opcode.
- Operands can be obtained from
- Registers
- Memory
- A literal value
- Literal values used in the instructions must
contain a symbol identifying the base - Decimal
- x hexadecimal
- b binary
- AND R3, R3, 15 Decimal value
41Labels
- Labels are symbolic names for addresses.
- Labels are used for two very useful purposes
- (1) Targets for branch instructions
- (2) Memory locations that contain values to be
loaded or stored.
AGAIN ADD R3,R3,R2
LD R2, NUMBER
BRP AGAIN NUMBER .FILL xffff
42Comments
- Everything after a semicolon is a comment.
- Comments are for humans
- Make the program more comprehensible.
- Should provide additional insight
- Dont restate the obvious.
- Improve the visual presentation of code.
- Both comments and judicious use of extra blank
space - Align elements of program for easier readability
- Extra spaces are ignored.
43Assembler Directives
- Five assembly directives are used to specify
- .ORIG - Starting addresses for programs
- .FILL Reserve and initialize single memory
location - .BLKW Reserve a sequence of memory locations
- .STRINGZ Initialize n1 memory locations with n
characters - .END Specify the end of program text.
.ORIG ADDRESS Example .ORIG x3000
.FILL value Example .FILL 6
.BLKW count Example .BLKW 2
.STRINGZ string Example .STRINGZ Hello, World!
.END Example .END
44Assembly Code Example
Multiply a number by the constant
6 .ORIG x3000 LD R1, SIX first load the
constant 6 LD R2, NUMBER load the number to
multiply AND R3, R3, 0 Clear R3 (hold
product) inner loop follows AGAIN ADD R3, R3,
R2 keep a running sum ADD R1, R1, -1 R1
contains the iteration count BRp AGAIN Do
again if R1 is positive done TRAP x25
Halt the machine constants NUMBER .BLKW 1
store the number to multiply here. SIX
.FILL x6 constant 6 .END
45More on .STRINGZ
x3010 x0048 H x3011 x0065 e x3012
x006c l x3013 x006c l x3014
x006f o x3015 x002c , x3016 x0020
x3017 x0057 W x3018 x006f o x3019
x0072 r x301A x006c l x301B
x0064 d x301C x0021 ! x301D x0000 0
0x3010 .STRINGZ Hello, World!
46Potential Assembly Problems
- Improper number or type of arguments
- example NOT R1, 7 ADD R1, R2
ADD R3, R3, NUMBER - Immediate argument too large
- example ADD R1,R2,1023
- Address (associated with label) more than 256
locations from instruction - example BR 2000
47Potential Assembly Problems
- Improper number or type of arguments
- example NOT R1, 7 ADD R1, R2
ADD R3, R3, NUMBER - Immediate argument too large
- example ADD R1,R2,1023
- Address (associated with label) more than 256
locations from instruction - example BR 2000
Cant use a literal
Second SRC missing
Cant use a label
Must be between -16 and 15
Must be within -256 and 255 from PC
48LC-3 Assembler
This is what gets loaded into the simulator
This is used when we link multiple programs
49Assembly Process
- The assembler translates assembly language
programs (.asm) into the machine language of the
ISA (.obj). - There is a 1-to-1 correspondence between assembly
language instructions and instructions in the
final machine language. - First Pass
- scan program file
- find all labels and calculate the corresponding
addressesthis information is stored in the
symbol table - Second Pass
- convert instructions to machine language,using
information from symbol table
501st Pass Constructing the Symbol Table
- Find the .ORIG statement, which tells us the
address of the first instruction. - Initialize location counter (LC)
- Incremented for each new instruction
- For each non-empty line in the program
- If line contains a label, add LABEL and LC to
symbol table. - Increment LC. NOTE If statement is .BLKW or
.STRINGZ, increment LC by the number of words
allocated. - Stop when .END statement is reached.
- NOTE A line that contains only a comment is
considered an empty line.
512nd Pass Generating Machine Language
- Reset location counter (LC)
- For each executable assembly language statement,
generate the corresponding machine language
instruction. - resolve labels referenced in instructions using
the symbol table - increment LC for each instruction as on pass 1
- output resulting machine code to output files
- Stop when .END statement is reached.
52What the assembler does
.ORIG x3000 LD R1, SIX
LD R2, NUMBER AND R3, R3, 0 AGAIN ADD
R3, R3, R2 ADD R1, R1, -1 BRp
AGAIN HALT NUMBER .BLKW 1 SIX
.FILL x6 .END
.ORIG x3000 x3000 LD R1, SIX x3001
LD R2, NUMBER x3002 AND R3, R3,
0 x3003 AGAIN ADD R3, R3, R2 x3004 ADD
R1, R1, -1 x3005 BRp AGAIN x3006
HALT x3007 NUMBER .BLKW 1 x3008 SIX .FILL
x6 .END
Generate Addresses
Step 1 Generate Addresses (1st Pass)
53What the assembler does
.ORIG x3000 x3000 LD R1, SIX
x3001 LD R2, NUMBER x3002 AND
R3, R3, 0 x3003 AGAIN ADD R3, R3, R2 x3004
ADD R1, R1, -1 x3005 BRp
AGAIN x3006 HALT x3007 NUMBER .BLKW
1 x3008 SIX .FILL x6 .END
Label
Address
Generate Symbol Table
AGAIN
X3003
NUMBER
X3007
SIX
X3008
Symbol Table
Step 2 Generate Symbol Table (1st Pass)
54What the assembler does
.ORIG x3000 x3000 LD R1, SIX
x3001 LD R2, NUMBER x3002 AND
R3, R3, 0 x3003 AGAIN ADD R3, R3, R2 x3004
ADD R1, R1, -1 x3005 BRp
AGAIN x3006 HALT x3007 NUMBER .BLKW
1 x3008 SIX .FILL x6 .END
0x3000 0x2207 0x2405 0x56e0 0x16c2 0x127f 0x03fd 0
xf025 0x0000 0x0006
Starting address
First Instruction
Generate Machine Code
Label
Address
AGAIN
x3003
NUMBER
x3007
SIX
x3008
Step 3 Generate Machine Code (2nd Pass)
Symbol Table
55More complex example
.ORIG x3000 LD R1, SIX
LD R2, NUMBER LEA R0, PROMPT
PUTS BRnzp HERE PROMPT .STRINGZ
hello! HERE AND R3, R3, 0 AGAIN ADD R3,
R3, R2 ADD R1, R1, -1 BRp
AGAIN HALT NUMBER .BLKW 1 SIX
.FILL x6 .END
.ORIG x3000 LD R1, SIX
LD R2, NUMBER AND R3, R3, 0 AGAIN ADD
R3, R3, R2 ADD R1, R1, -1 BRp
AGAIN HALT NUMBER .BLKW 1 SIX
.FILL x6 .END
56Side Note on TRAP Codes
- LC-3 assembler provides pseudo-instructions for
each trap code, so you dont have to remember
them.
57Back to More complex example
.ORIG x3000 LD R1, SIX
LD R2, NUMBER LEA R0, PROMPT
PUTS BRnzp HERE PROMPT .STRINGZ
hello! HERE AND R3, R3, 0 AGAIN ADD R3,
R3, R2 ADD R1, R1, -1 BRp
AGAIN HALT NUMBER .BLKW 1 SIX
.FILL x6 .END
.ORIG x3000 X3000 LD R1, SIX
X3001 LD R2, NUMBER X3002 LEA R0,
PROMPT X3003 PUTS X3004 BRnzp
HERE X3005 PROMPT .STRINGZ hello! X3006 X3007
X3008 X3009 X300A X300B X300C HERE AND R3,
R3, 0 X300D AGAIN ADD R3, R3, R2 X300E
ADD R1, R1, -1 X300F BRp AGAIN X3010
HALT X3011 NUMBER .BLKW 1 X3012 SIX
.FILL x6 .END
Step 1 Generate Addresses
58What the assembler does
.ORIG x3000 X3000 LD R1, SIX
X3001 LD R2, NUMBER X3002 LEA R0,
PROMPT X3003 PUTS X3004 BRnzp
HERE X3005 PROMPT .STRINGZ hello! X3006 X3007
X3008 X3009 X300A X300B X300C HERE AND R3,
R3, 0 X300D AGAIN ADD R3, R3, R2 X300E
ADD R1, R1, -1 X300F BRp AGAIN X3010
HALT X3011 NUMBER .BLKW 1 X3012 SIX
.FILL x6 .END
Label
Address
PROMPT
X3005
HERE
X300C
AGAIN
X300D
NUMBER
X3011
SIX
X3012
Symbol Table
Step 2 Generate Symbol Table
59What the assembler does
0x3000 0x2211 0x240F 0xE002 0xF022 0x0E07 0x0068 0
x0065 0x006C 0x006C 0x006F 0x0021 0x0000 0x56E0 0x
16C2 0x127F 0x03FD 0xF025 0x0000 0x0006
Starting address
.ORIG x3000 X3000 LD R1, SIX
X3001 LD R2, NUMBER X3002 LEA R0,
PROMPT X3003 PUTS X3004 BRnzp
HERE X3005 PROMPT .STRINGZ hello! X3006 X3007
X3008 X3009 X300A X300B X300C HERE AND R3,
R3, 0 X300D AGAIN ADD R3, R3, R2 X300E
ADD R1, R1, -1 X300F BRp AGAIN X3010
HALT X3011 NUMBER .BLKW 1 X3012 SIX
.FILL x6 .END
First Instruction
Generate Machine Code
Step 3 Generate Machine Code
60Compare them, what is different?
0x3000 0x2211 0x240F 0xE002 0xF022 0x0E07 0x0068 0
x0065 0x006C 0x006C 0x006F 0x0021 0x0000 0x56E0 0x
16C2 0x127F 0x03FD 0xF025 0x0000 0x0006
0x3000 0x2207 0x2405 0x56E0 0x16C2 0x127
F 0x03FD 0xF025 0x0000 0x0006
.ORIG x3000 LD R1, SIX
LD R2, NUMBER LEA R0, PROMPT
PUTS BRnzp HERE PROMPT .STRINGZ
hello! HERE AND R3, R3, 0 AGAIN ADD R3,
R3, R2 ADD R1, R1, -1 BRp
AGAIN HALT NUMBER .BLKW 1 SIX
.FILL x6 .END
.ORIG x3000 LD R1, SIX
LD R2, NUMBER AND R3, R3, 0 AGAIN ADD
R3, R3, R2 ADD R1, R1, -1 BRp
AGAIN HALT NUMBER .BLKW 1 SIX
.FILL x6 .END
61Style Guidelines
- Provide a program header, with authors name,
date, etc.,and purpose of program. - Start labels, opcode, operands, and comments in
same columnfor each line. (Unless entire line
is a comment.) - Use comments to explain what each register does.
- Give explanatory comment for most instructions.
- Use meaningful symbolic names.
- Mixed upper and lower case for readability.
- EX ASCIItoBinary, InputRoutine, SaveR1
- Provide comments between program sections.
- Each line must fit on the page -- no wraparound
or truncations. - Long statements split in aesthetically pleasing
manner.
62Sample Program
63Char Count in Assembly Language (1 of 2)
- Program to count occurrences of a character in
a file. - Character to be input from the keyboard.
- Result to be displayed on the monitor.
- Note Program only works with 9 or less
occurrences. -
- .ORIG x3000 program start address
- AND R2,R2,0 R2 is counter, initially 0
- LD R3,PTR R3 is pointer to characters
- GETC R0 gets character input
-
- Get character and test for end of file
-
- LOOP LDR R1,R3,0 R1 gets character
- ADD R4,R1,-4 test for EOT (ASCII x04)
- BRz OUTPUT if done, prepare the output
- NOT R1,R1
- ADD R1,R1,1 R1 -R1
- ADD R1,R1,R0 Match?
- BRnp NEXT N, go on
64Char Count in Assembly Language (2 of 2)
-
- Move to next character from file.
-
- NEXT ADD R3,R3,1 Point to next character.
- BR LOOP Loop back and try again
-
- 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
65LC-3 Assembler Multiple Object Files
This is what gets loaded into the simulator
This is used when we link multiple programs
66Multiple Object Files
- An object file is not necessarily a complete
program. - system-provided library routines
- code blocks written by multiple developers
- Most microprocessors can load multiple object
files into memory, then start executing at a
desired address. - system routines, such as keyboard input, are
loaded automatically - loaded into system memory, below x3000 (for
LC-3) - user code should be loaded between x3000 and
xFDFF (for LC-3) - each object file includes a starting address
- be careful not to load overlapping object files
67Linking and Loading
- Loading is the process of copying an executable
imageinto memory. - more sophisticated loaders are able to relocate
imagesto fit into available memory - must readjust branch targets, load/store
addresses - Linking is the process of resolving symbols
betweenindependent 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
68Assembly of Multiple Files
- Linking multiple files together to create one
executable image. - Libraries contain useful utilities (printing,
math, etc.). - Linkers resolve external symbols at link time.
- The .EXTERNAL psuedo-op defines labels that are
located in the other files. This allows program
to assemble.
.EXTERNAL STARTofFILE Symbol in a different
file or library
69Assembly of Multiple Files
moduleA.asm
moduleB.asm
assembler
assembler
moduleA.obj
moduleB.obj
linker
program.bin