Assy Language Prog Slides

Download Report

Transcript Assy Language Prog Slides

Chapter 7
Introduction to LC-3
Assembly Language
• Assembly Language
• Assembly Process
• Using the Editor & Simulator for Assembly Language Programming
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.
• An instruction has the following format:
LABEL OPCODE OPERANDS ;COMMENTS
optional
Example:
Loop1
mandatory
ADD
R3, R3, #-1 ; Decrement R3
Assembler Directives
• Pseudo-operations
– do not refer to operations executed by program
– used by assembler
– look like instruction, but “opcode” starts with dot
Opcode Operand
.ORIG
address
.END
Meaning
starting address of program
end of assembly 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
Compute the Sum of 12 Integers
• Program begins at location x3000.
• Integers begin at location x3100.
R1  x3100
R3  0 (Sum)
R2  12(count)
R2=0?
NO
R4
R3
R1
R2




M[R1]
R3+R4
R1+1
R2-1
YES
R1: “Array” index pointer (Begin with location 3100)
R3: Accumulator for the sum of integers
R2: Loop counter (Count down from 12)
R4: Temporary register to store next integer
Compute the Sum of 12 Integers
Address
Instruction
Comments
x3000
1 1 1 0 0 0 1 0 1 1 1 1 1 1 1 1
R1  x3100
x3001
0 1 0 1 0 1 1 0 1 1 1 0 0 0 0 0
R3  0
x3002
0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0
R2  0
x3003
0 0 0 1 0 1 0 0 1 0 1 0 1 1 0 0
R2  12
x3004
0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1
If Z, goto x300A
x3005
0 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0 Load next value to R4
x3006
0 0 0 1 0 1 1 0 1 1 0 0 0 1 0 0
x3007
0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 Increment R1 (pointer)
X3008
0 0 0 1 0 1 0 0 1 0 1 1 1 1 1 1
Decrement R2
(counter)
x3009
0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0
Goto x3004
Add to R3
R1: “Array” index pointer (Begin with location 3100)
R3: Accumulator for the sum of integers
R2: Loop counter (Count down from 12)
R4: Temporary register to store next integer
Compute the Sum of 12 Integers - Program
.ORIG x3000
; Add 12 integers
;
R1: Pointer to integer
;
R2: Loop counter
;
R3: Accumulator
;
R4: Temporary register
LD
AND
AND
ADD
R1  x3100
R3  0 (Sum)
R2  12(count)
R1 DATAADDR ; Load pointer
R2=0?
;
to integers
R3, R3, #0
; Accumulator = 0
R2, R2, #0
; Counter = 12
R2, R2, #12
YES
NO
R4
R3
R1
R2
; Add integers
LOOP
STOP
BRZ
LDR
ADD
ADD
ADD
BRNZP
STOP
R4, R1,
R3, R3,
R1, R1,
R2, R2,
LOOP
#0
R4
#1
#-1
BRNZP STOP
; Stop when done
; Add next integer
; Inc pointer
; Dec counter
; Stop
DATAADDR .FILL x3100
.END
Note: Used DATAADDR to hold address of DATA. Why?




M[R1]
R3+R4
R1+1
R2-1
Compute the Sum of 12 Integers - Data
.ORIG x3100
; Data section
DATA
.END
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
x0001
x0002
x0004
x0008
xFFFF
xFFFE
xFFFC
xFFF8
x0007
x0004
x0002
x0003
; 12 integers
Compute the Sum of 12 Integers
• Use the LC3 Editor to enter the program and
data and store them as
add1.asm
data1.asm
• Use the LC3 Editor to assemble them:
add1.asm  add1.obj
data1.asm  data1.obj
• Then use the LC3 Simulator to test them:
load add1.obj and data1.obj
• Set the PC, appropriate breakpoints, and
execute the program (single step or run)
Sum of 12 Integers in One Package
.ORIG x3000
; Add 12 integers
;
R1: Pointer to integer
;
R2: Loop counter
;
R3: Accumulator
;
R4: Temporary register
LOOP
STOP
LEA
AND
AND
ADD
BRZ
LDR
ADD
ADD
ADD
BRNZP
BRNZP
R1, DATA
R3, R3, #0
R2, R2, #0
R2, R2, #12
STOP
R4, R1, #0
R3, R3, R4
R1, R1, #1
R2, R2, #-1
LOOP
STOP
; Load pointer to integers
; Accumulator = 0
; Counter = 12
; Stop when done
; Add next integer
; Inc pointer
; Dec counter
; Stop
; Data section
DATA
.END
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
x0001
x0002
x0004
x0008
xFFFF
xFFFE
xFFFC
xFFF8
x0007
x0004
x0002
x0003
; 12 integers
Note: Used LEA to load pointer to data. Why?
One Pass vs Two Pass Assemblers
• What does the assembler need to do?
–
–
–
–
–
–
Check for syntax errors
Build a symbol table
Assemble statements
Use pseudo-op instructions
Resolve Addresses
Create a load module
• Two Pass – Checks for syntax errors and builds
the Symbol Table during first pass, resolves
operand addresses during second pass.
• One Pass – Checks for syntax errors, builds the
Symbol Table, and resolves operand addresses
during the first pass. So why have a two pass?
An Assembly Language Program Example
;
; Program to multiply a number by the constant 6
;
.ORIG
x3050
LD
LD
AND
;
R1, SIX
R2, NUMBER
R3, R3, #0
The multiply loop
AGAIN
ADD
ADD
BRp
R3, R3, R2
R1, R1, #-1
AGAIN
; Constant 6
; Number
; Clear R3
;
The product.
.BLKW
.FILL
3
x0006
; Accumulate product
; Dec counter
No
; Value of Number
.END
Symbol Table:
add R3 to R2
decrement R1
HALT
NUMBER
SIX
clear R3
R1 = 0?
Yes
HALT
Symbol
Address
AGAIN
x3053
NUMBER
x3057
SIX
x305A
What if there were
More than One Object (Load) File
•
Example Symbol Table:
Symbols
Start
Number
Externals
Data
Exports
Value
Addresses
x3000
x300A
?
x30E0
• The “Linker/Loader” would generate another “global symbol table”
to resolve Externals & Exports at Load time. It would only
address the Externals (Imports) and Exports (Internals).
• An Export is a value make available to other modules
• An External is a value expected to be defined in another module
Trap Codes
•
LC-3 assembler provides “pseudo-instructions” for
each trap code, so you don’t have to remember their TRAP #’s.
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 keyboard.
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.
Program to add two single digit integers
;
;
;
;
.ORIG x3000
input two numbers
IN
LD R3, HEXN30
ADD R0, R0, R3
ADD R1, R0, #0
IN
ADD R0, R0, R3
add the numbers
ADD R2, R0, R1
print the results
LEA R0, MESG
PUTS
ADD R0, R2, #0
LD R3, HEX30
ADD R0, R0, R3
OUT
stop
HALT
; data
MESG
HEXN30
HEX30
.STRINGZ
.FILL
.FILL
.END
; begin at x3000
;input an integer character (ascii) {TRAP 23}
;subtract x30 to get integer
;move the first integer to register 1
;input another integer {TRAP 23}
;convert it to an integer
;add the two integers
;load the address of the message string
;"PUTS" outputs a string {TRAP 22}
;move the sum to R0, to be output
;add 30 to integer to get integer character
;display the sum {TRAP 21}
;{TRAP 25}
"The sum of those two numbers is: “
xFFD0
; -30 HEX
x0030
; 30 HEX
Program to add two single digit integers
• Enter,
• Assemble,
• Load
• Execute, and Test the program.
Write a program to count the 1’s in register R0
; Program to count 1's in Register R0
;
R3 is a working copy of R0
;
R1 contains the count
;
R2 is a loop counter
.orig x3100
ADD R3, R0, #0
;copy R0 into R3
AND R1, R1, #0
;clear count
ADD R3, R3, #0
BRZP NEXT
ADD R1, R1, #1
;test highest bit
;count if neg
NEXT
AND R2, R2, #0
ADD R2, R2, #-15
;check remaining 15 bits
;
R2 = -15 (count)
LOOP
ADD R3, R3, R3
BRZP AGAIN
ADD R1, R1, #1
ADD R2, R2, #1
BRN LOOP
;shift R3 left
AGAIN
HALT
.END
;count if neg
;inc count
Program to Check for overflow
;
;
Add R3=R0+R1, R2=0 indicates no overflow
.ORIG
x3000
AND R2, R2, #0
ADD R3, R0, R1
; test for overflow
ADD R0, R0, #0
BRN
NEG
ADD R1, R1, #0
BRN
DONE
ADD R3, R3, #0
BRZP DONE
ADD R2, R2, #1
BRNZP DONE
R0NEG ADD R1, R1, #0
BRZP DONE
ADD R3, R3, #0
BRN
DONE
ADD R2, R2, #1
DONE
HALT
.END
;Initially R2=0 (no Overflow assumed)
;R3=R0+R1
;test R0
;Branch if RO negative
;R0 pos, test R1
;R0 pos, R1 neg -> No overflow
;R0 pos, R1 pos, maybe, test R3
;R3 also pos -> no overflow
;Set R2=1 indicating overflow
;R0 neg, test R1
;R0 neg, R1 pos -> No overflow
;R0 neg, R1 neg, maybe, test R3
;R3 also neg -> no overflow
;Set R2=1 indicating overflow
Count the occurrences of a character in a file.
Count = 0
(R2 = 0)
Done?
YES
(R1 ?= EOT)
Ptr = 1st file character
Convert count to
ASCII character
(R0 = x30, R0 = R2 + R0)
NO
(R3 = M[x3012])
Print count
YES
Match?
NO
(TRAP x21)
(R1 ?= R0)
Input char
from keybd
(TRAP x23)
HALT
Incr Count
Load char from file
(R2 = R2 + 1)
(R1 = M[R3])
Load next char from file
(R3 = R3 + 1, R1 = M[R3])
(TRAP x25)
;
;
;
;
;
;
;
;
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
;
;
;
;
R2
R3
R0
R1
is counter, initially 0
is pointer to character file
gets input character
gets first character from file
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
;
; 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
; ASCII offset
PTR
.FILL
x4000
; PTR to character file
.END