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 Language
At this point, we need to introduce a human
readable “language” that would do the same
job as the machine code
We will be using abbreviations for all the
opcodes and names for memory references
This language is called assembly
Assembly Language
Assembly language differs in one key way
from other higher level languages
There is a one-to-one translation to the
equivalent machine instruction
For higher level languages, this may be a
one to many translation
Assembly Language
The idea is to make programming in machine
language user friendly
At the same time, there is still the detailed
control of machine code
We use commands such as ADD, NOT, AND
instead of the binary opcodes
Assembly Language
We also have memory references
These can be symbols such as PRODUCT or
SUM and the assembler keeps track of this
for us
Assembly Language
;
; 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
Assembly Language
Each line of assembly has one of three
things
–
–
–
An instruction
An assembler pseudo-op or directive
A comment
Assembly Language
Whitespace is ignored
Names are case insensitive
Comments, which begin with ; are ignored
Assembly Language
An instruction has the following format:
LABEL OPCODE OPERANDS ; COMMENTS
optional
mandatory
Opcodes and Operands
Opcodes are reserved words that correspond
to machine instructions
They are listed in Appendix A: ADD, AND,
NOT, LD, LDR, etc
Opcodes and Operands
Operands
–
–
–
–
–
Registers – specified by Rn, where n is the
number of the register(0-7)
Numbers – indicated by # (decimal) or
x(hexadeciamal)
Label – symbolic name of the memory location
Operands are separated by commas
Number, type, and order correspond to the
machine instruction
Opcodes and Operands
For example
ADD
ADD
LD
BRz
R1,R1,R3
R1,R1,#3
R6,NUMBER
LOOP
Labels and Comments
Labels are placed at the beginning of the line
Assigns a symbolic name to the memory
location
Example:
LOOP
ADD R1,R1,#-1
BRp LOOP
Labels and Comments
Anything after a semicolon is a comment
Completely ignored by the assembler
Used for documentation purposes mostly
Tips:
–
–
–
Avoid restating the obvious
Provide addition insight about the code
Use comments to separate modules and parts of
code
Assembler Directives
Directives – do not refer to operations by the
CPU
Used by the assembler to generate the
machine code
They look like an instruction, but they begin
with a dot(.)
Assembler Directives
Opcode
Operand
Meaning
.ORIG
address
starting address of program
.END
end of program
.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
Traps
The assembler provides “shortcuts” for some
of the traps:
Code
Equivalent
Description
HALT
TRAP x25
Halt execution and print message to console.
IN
TRAP x23
Print prompt on 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.
Style
Use the following style guidelines to improve
the readability and understandability of your
programs:
1.
2.
3.
Provide a program header, with author’s name, date, etc.,
and purpose of program.
Start labels, opcode, operands, and comments in same
column for each line. (Unless entire line is a comment.)
Use comments to explain what each register does.
Style
4.
5.
Give explanatory comment for most instructions.
Use meaningful symbolic names.
6.
7.
Mixed upper and lower case for readability.
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.