You are on page 1of 7

ECE 120

Lecture 35

November 20, 2015

Assembly language programming with


I/O
Lecture Topics

LC-3 TRAP routines for handling I/O


Programming Examples

Reading assignments

FL15

Textbook 9.1, 10.4

V. Kindratenko

ECE 120

Lecture 35

November 20, 2015

LC-3 TRAP routines for handling I/O

TRAPs for input

vector
symbol
routine
x20
GETC
read a single character (no echo)
x23
IN
print prompt to console, read and echo character from keyboard
o GETC
R0 ASCII value for a character entered from the keyboard
No prompt will be printed
o IN
R0 ASCII value for a character entered from the keyboard
Will print prompt Input a character>
TRAPs for output
vector
x21
x22
X24
o OUT

o PUTS

symbol
OUT
PUTS
PUTSP

routine
output a character to the monitor
write a string to the console
write a string to the console; two chars per memory location

display the character whose ASCII value is stored in R0


display string stored in memory shoes address is stored in R0
one char stored per memory location (lower 8 bits)

Programming examples

Example 1: read a character without prompt and echo it to the display

.ORIG x3000
GETC
; read from KBD
OUT
; write to DSP
HALT
.END
Example 2: print null-terminated string

.ORIG x3000
LEA R0, MY_TEXT ; load starting address of the text
PUTS
; print text whose address is in R0
HALT
MY_TEXT .STRINGZ "Hello World!"
.END
Example 3: print single-digit number stored in R1
.ORIG x3000
AND R1, R1, #0
ADD R1, R1, #7
LD R0, ASCII0
ADD R0, R0, R1

FL15

;
;
;
;

clear R1
load single-digit number to print
acquire ASCII offset to digit 0
generate ASCII value for digit in R1

V. Kindratenko

ECE 120

Lecture 35

November 20, 2015

OUT
HALT
ASCII0 .FILL x30 ; ASCII value of digit 0
.END
Example 4: read string from the keyboard, one character at a time, and store it in memory
o Max string length is 99
.ORIG x3000
; load output string's address
LEA R1, STORED_STR
; store negative value of ASCII for Enter key in R1
LD R2, ASCII_ENT
NOT R2, R2
ADD R2, R2, #1
NEXT

; read a character
GETC ; get char from keyboard
OUT ; echo it back to terminal
; check if it is Enter
ADD R3, R2, R0
BRz DONE ; if it is, stop
; it is not, thus, store it in memory
STR R0, R1, #0
ADD R1, R1, #1
; repeat
BRnzp NEXT

DONE
; add 0 to the end of the string
AND R4, R4, #0
STR R4, R1, #0

FL15

HALT
;
STORED_STR .BLKW #100
ASCII_ENT .FILL xD ; ASCII value for Enter key
;
.END
Example 5: Print 8x8 sprite
o A sprite is a two-dimensional image that is integrated into a larger scene, e.g.:
********
*
*
* ^ ^ *
* * * *
*
*
* * * *

V. Kindratenko

ECE 120

Lecture 35

* ** *
********
How do we store it in LC-3 memory?
As a one-dimensional array
One symbol per memory location
In consecutive memory locations
Row-after-row (row-major order)
SPRITE
; row 0
.FILL x2A
.FILL x2A
.FILL x2A
.FILL x2A
.FILL x2A
.FILL x2A
.FILL x2A
.FILL x2A
; row 1
.FILL x2A
.FILL x20
.FILL x20
.FILL x20
.FILL x20
.FILL x20
.FILL x20
.FILL x2A
; row 2

November 20, 2015

;
;
;
;
;
;
;
;

*
*
*
*
*
*
*
*

; *
; " "

; *

Algorithm
Start

for each row


for each column
print symbol
print new line
done

R2sprite starting
address

R38; row counter

no

R30

Stop

yes

R48; column counter

no

yes

R40

FL15

Output NL

R0MEM[R2]
Output R0
R2R2+1

R3R3-1

R4R4-1

V. Kindratenko

ECE 120

Lecture 35
o

November 20, 2015

Implementation
.ORIG x3000
; load output string's address
LEA R2, SPRITE
; row counter set to 8
AND R3, R3, #0
ADD R3, R3, #8
NEXT_ROW
; R3 != 0 ?
ADD R3, R3, #0
BRz DONE
; column counter set to 8
AND R4, R4, #0
ADD R4, R4, #8
NEXT_COLUMN
; R4 != 0 ?
ADD R4, R4, #0
BRz DONE_ROW
; print next char
LDR R0, R2, #0 ; read next char
OUT
ADD R2, R2, #1 ; move to next char
;
; decrement column counter and move to next
ADD R4, R4, #-1
BRnzp NEXT_COLUMN
DONE_ROW
; print new line char
LD R0, ASCII_NL ; load NewLine ASCII value
OUT
; move to next row
ADD R3, R3, #-1
BRnzp NEXT_ROW
DONE
HALT
;
ASCII_NL .FILL xA
SPRITE
; row 0
.FILL x2A ; *
.FILL x2A ; *
.FILL x2A ; *
.FILL x2A ; *
.FILL x2A ; *
.FILL x2A ; *
.FILL x2A ; *
.FILL x2A ; *
; row 1
.FILL x2A ; *

FL15

V. Kindratenko

ECE 120

Lecture 35
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
; row
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
; row
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
; row
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
; row
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
; row
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
; row
.FILL
.FILL
.FILL
.FILL

FL15

x20
x20
x20
x20
x20
x20
x2A
2
x2A
x20
x5E
x20
x20
x5E
x20
x2A
3
x2A
x20
x2A
x20
x20
x2A
x20
x2A
4
x2A
x20
x20
x20
x20
x20
x20
x2A
5
x2A
x20
x2A
x20
x20
x2A
x20
x2A
6
x2A
x20
x20
x2A
x2A
x20
x20
x2A
7
x2A
x2A
x2A
x2A

November 20, 2015

; " "

; *
; *
; ^
; ^
; *
; *
; *
; *
; *
; *

; *
; *
; *
; *
; *
; *
; *
; *
; *
;
;
;
;

*
*
*
*

V. Kindratenko

ECE 120

Lecture 35
.FILL
.FILL
.FILL
.FILL
;
.END

FL15

x2A
x2A
x2A
x2A

;
;
;
;

November 20, 2015

*
*
*
*

Examples for self-study


o ASCII to binary conversion: Textbook 10.4.2
o Binary to ASCII conversion: Textbook 10.4.3

V. Kindratenko

You might also like