Вы находитесь на странице: 1из 6

ECE 120

Lecture 35

November 18, 2015

Programming in LC-3 assembly language


Lecture Topics

Example program written in LC-3 assembly language


LC-3 TRAP mechanism

Reading assignments

FL15

Textbook 9.1

V. Kindratenko

ECE 120

Lecture 35

November 18, 2015

Example

Problem statement: Given a sequence of 100 numbers stored in memory in 2s complement


representation starting from address x4000, count the number of negative values. R2 is to
contain the count, R3 is to contain the address in memory of the last negative value found.
Assume that at least one negative value exists.
Systematic decomposition
start
; register use:
; R0 - address of next element
; R1 - counter
; R2 - count
; R3 - memory address of the last
negative value
; R4 - holds -100
; R5 - temp
; R6 - temp

initialize

Get next
Element
R5<-M[R0]
n=1

is it
Negative?

n=0

Add and store its


address
R2<-R2+1
R3<-R0

are
there
more?

yes

no

stop

LC-3 assembly program

; Given a sequence of 100 values stored in 2's complement format


; starting at x4000, count the number of negative values.
; R2 to contain the count, R3 to contain memory address of the last
negative value
;
.ORIG x3000
;
; Initialization
LD R0, ARRAY_START
AND R1, R1, #0
LDR R2, R0, #0
ADD R3, R0, #0
LD R4, ARRAY_END
;
; Get next element
LOOP LDR R5, R0, #0
;
; is this negative?
BRzp SKIP
;

FL15

V. Kindratenko

ECE 120

Lecture 35

November 18, 2015

; Found new negative, count remember it


ADD R2, R2, #1
ADD R3, R0, #0
;
; Move to next element
SKIP ADD R1, R1, #1
ADD R0, R0, #1
;
; are there more?
ADD R5, R1, R4 ; R5=R1-100
BRn LOOP
HALT
;
ARRAY_START .FILL x4000
ARRAY_END .FILL #-100
.END

When writing a program in LC-3 assembly language, use the following style guidelines to
improve the readability and understandability of the program
o Provide a program header, with authors name, date, etc., and purpose of program
o Start labels, opcode, operands, and comments in same column for each line
o Use comments to explain what each register does
o Give explanatory comment for most instructions
o Use meaningful symbolic names
o Mix upper and lower case for readability
ASCIItoBinary, InputRoutine, SaveR1
o Provide comments between program sections
o Each line must fit on the page -- no wraparound or truncations
o Long statements split in aesthetically pleasing manner
We save the program into a file with extension .asm, e.g., count100.asm
Convert it to an LC-3 executable: lc3asm count100.asm :
STARTING PASS 1
0 errors found in first pass
STARTING PASS 2
0 errors found in second pass
lc3asm translated the program stored in count100.asm into a binary code stored in
count100.obj, which we can now execute in the LC-3 simulator
o Pass 1: scan the entire program and create the symbol table is created:

FL15

Symbol
Address
LOOP
x3005
SKIP
x3009
ARRAY_START
x300E
ARRAY_END
x300F
Pass 2: machine language program is generated
Line-by-line, each assembly language instruction is converted into machine
(binary) language instruction

V. Kindratenko

ECE 120

Lecture 35

November 18, 2015

LC-3 TRAP mechanism

System Calls
o Certain operations require specialized knowledge and protection
specific knowledge of I/O device registers and the sequence of operations
needed to use them
I/O resources shared among multiple users/programs; a mistake could affect
lots of other users!
o Not every programmer knows (or wants to know) this level of detail
o Therefore it is desirable to provide service routines or system calls (part of operating
system) to safely and conveniently perform low-level, privileged operations
User program invokes system call
Operating system code performs operation
Returns control to user program
o In LC-3, this is done through the TRAP mechanism
For a TRAP routine to be executed, the following must be present
o A set of service routines
part of operating system -- routines start at arbitrary addresses (convention is
that system code is below x3000) up to 256 routines
o Table of starting addresses
stored at x0000 through x00FF in memory
called System Control Block in some architectures
o TRAP instruction
used by program to transfer control to operating system
8-bit trap vector names one of the 256 service routines
o A linkage back to the user program
want execution of the user program to resume immediately after the TRAP
instruction
TRAP instruction
15

TRAP

14

13

12

11

10

1 1 1 1 0 0 0 0
opcode

unused

trap vector

FL15

Trap vector
identifies which system call to invoke
8-bit index into table of service routine addresses, called trap vector table
in LC-3, this table is stored in memory at 0x0000 0x00FF
8-bit trap vector is zero-extended into 16-bit memory address
TRAP instruction causes the corresponding service routine to execute
o PC is loaded with the address of the first instruction of the corresponding service
routine
MARZEXT(trapvector)
MDRMEM[MAR]
R7PC (note that we save old PC in R7)
PCMDR

V. Kindratenko

ECE 120

Lecture 35

November 18, 2015

Once the service routine is done, control is passed back to the user program suing RET
instruction, which is just another name for JMP R7 instruction
PCR7 (restore old PC to return to the user program)
15

14

13

12

11

10

1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0

RET

opcode

R7

FL15

must make sure that service routine does not change R7, or we wont know
where to return
also, must make sure R7 does not have a useful value that will be overwritten in
the process of calling a TRAP
TRAP instructions implemented in LC-3
vector
symbol
routine
x20
GETC
read a single character (no echo)
x21
OUT
output a character to the monitor
x22
PUTS
write a string to the console
x23
IN
print prompt to console, read and echo character from keyboard
X23
PUTSP
write a string to the console; two chars per memory location
x25
HALT
halt the program
x26
write a number to the console (undocumented, when available)
On saving and restoring registers
o We must save the value of a register if
its value will be destroyed by service routine, and
we will need to use the value after that action
o Who should save it?
caller of service routine?
knows what it needs later, but may not know what gets altered by
called routine
called service routine?

V. Kindratenko

ECE 120

Lecture 35

November 18, 2015

knows what it alters, but does not know what will be needed later by
calling routine
Called routine callee-save
Before start, save any registers that will be altered (unless altered value is
desired by calling program)
Before return, restore those same registers
Calling routine caller-save
Save registers destroyed by own instructions or by called routines (if known), if
values are needed later
save R7 before TRAP
save R0 before TRAP x23 (input character)
Or avoid using those registers altogether
Where to save?
In memory of course!

ST R0, SaveR0 ; store R0 in memory


ST R7, SaveR7 ; store R7 in memory
GETC ; call TRAP which destroys R0 and R7
LD R7, SaveR7; restore R7
; consume input value from R0
LD R0, SaveR0; restore R0

; reserved storage for registers


SaveR0 .BLKW 1
SaveR7 .BLKW 1

FL15

V. Kindratenko

Вам также может понравиться