The von Neumann Model – Chapter 4

Download Report

Transcript The von Neumann Model – Chapter 4

1
The LC-3 – Chapter 7
COMP 2620
Dr. James Money
COMP 2620
Assembly Program
;
;
;
;
;
;
;
;
;
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
AND
LD
GETC
LDR
x3000
R2, R2, #0
R3, PTR
R1, R3, #0
;
; Test character for end of file
;
TEST
ADD
R4, R1, #-4
BRz
OUTPUT
;
;
;
;
R2
R3
R0
R1
is counter, initially 0
is pointer to characters
gets character input
gets first character
; Test for EOT (ASCII x04)
; If done, prepare the output
Assembly Program
;
; 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 char
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
Assembly Program
;
; Storage for pointer and ASCII template
;
ASCII
.FILL x0030
PTR
.FILL x4000
.END
Assembly Process



Before you can execute an LC-3 assembly
program, it has to converted to machine code
for the LC-3 ISA
The job of the assembler is to perform this
task
Essentially, it is a translation from one
assembly command to a corresponding LC-3
instruction
Two-Pass Process




We consider how the assembler performs
this translation to machine code
Recall there is a one-to-one correspondence
between assembly instructions and machine
instructions
One could do this in one pass
The first 9 lines are comments are ignored
Two-Pass Process


At line 0A, we have a pseudo-op which sets
the initial PC to 0x3000
Line 0B is an AND instruction which readily
translates to
0x3000: 0101010010100000
Two-Pass Process



However, at line 0C, there is no knowledge
yet of the memory location that PTR refers to
At this point, the assembler fails and exits if it
only uses one pass of the assembly file input
To get around this, we have to use two
passes of the assembly file
Two-Pass Process

Pass 1:
–
–

Create list of addresses corresponding to labels
This is called a symbol table
Pass 2:
–
–
We translate the assembly instructions
We use the symbol table to clarify references
Two-Pass Process



Now, at line 0C, we have to translate
LD R3,PTR
But we know from the first pass, PTR refers
to memory location 0x3013
Thus, this translates to the instruction
0x3001: 0010011000010001
First Pass


From our perspective, the symbol table is
only list of symbolic names with 16 bit
memory locations
We obtain this table by going through the file
completely and see what lines and
appropriate memory locations applies to
each symbol
First Pass


If we have made all our labels in the
assembly program, then we have no unfound
symbols in our table for the second pass
For now, we only consider single file
programs with only one .ORIG and .END
pseudo-ops
First Pass



The pseudo-op at line 0A makes the current
location 0x3000
This is called the location counter or the LC
The LC is initialized to whatever value is
provided by the .ORIG operand
First Pass



Then, for the rest of the file, the LC
increments one value for each line that is not
a comment
If there is a label, an entry is put in the
symbol table
The first pass ends when .END is
encountered
First Pass



The first label is at line 13, which is TEST
This is the fifth instruction, so LC is 0x3004
Thus the table looks like:
Symbol
Address
TEST
x3004
First Pass



The second label is at line 20
The LC is now increments to x300B
Thus, the table is updated to
Symbol
Address
TEST
x3004
GETCHAR
x300B
First Pass

After you reach .END, the symbol table is
Symbol
Address
TEST
x3004
GETCHAR
x300B
OUTPUT
x300E
ASCII
X3012
PTR
x3013
Second Pass



In the second pass, we generate the
machine code for each instruction
This time, though, we use the symbol table to
assist in memory references
When we reach line 0C, we can use the
address 0x3013 which corresponds to the
label PTR
Second Pass




The instruction is
LD R3, PTR
So, the opcode is 0010
The DR is 011
How do we compute the PC offset?
Second Pass






Recall the incremented PC is LC+1 here
Thus, PC = x3002
And PTR is x3013
Subtracting we have 0x3013-0x3002 = 0x11
Thus, PC offset = 000010001
Hence, our instruction is
0010011000010001
Second Pass



Note, if the address of PTR more than +256
or less that -255, an error is issued by the
assembler
This is because we only have 9 bits to
encode the offset
If this is the case, you will have to use
another load instruction
Second Pass



Second pass continues, and finishes,
incrementing LC as it goes
It uses the symbol table as needed
Resulting code is on the page 188