I/O & TRAP

TRAP Routines & I/O

System calls for input and output.

TRAP Instructions

TRAP instructions invoke operating system service routines. They're the LC-3's way of doing I/O and system operations.

TrapAliasDescription
TRAP x20GETCRead one character into R0 (no echo)
TRAP x21OUTPrint character in R0
TRAP x22PUTSPrint string starting at address in R0
TRAP x23INRead character into R0 (with echo)
TRAP x25HALTStop the program
You can use either form: TRAP x21 or OUT — they're identical.

Output: Printing Characters

OUT prints the ASCII character in the low 8 bits of R0:
.ORIG x3000
; Print 'A' (ASCII 65)
AND R0, R0, #0
ADD R0, R0, #15   ; 15
ADD R0, R0, #15   ; 30
ADD R0, R0, #15   ; 45
ADD R0, R0, #15   ; 60
ADD R0, R0, #5    ; 65 = 'A'
OUT

; Easier: print a whole string
LEA R0, MSG
PUTS

HALT
MSG .STRINGZ " Hello!"
.END

Input: Reading Characters

GETC reads a single character from the keyboard into R0. IN does the same but also echoes the character to the screen.
.ORIG x3000
LEA R0, PROMPT
PUTS            ; Print prompt
GETC            ; Read character into R0
OUT             ; Echo it back
LEA R0, NL
PUTS            ; Print newline
HALT
PROMPT .STRINGZ "Type a character: "
NL .STRINGZ "\n"
.END
Exercise

Write an "echo" program: read a character from the user (use GETC), then print it 3 times using OUT.

1
R0
x00000
R1
x00000
R2
x00000
R3
x00000
R4
x00000
R5
x00000
R6
x00000
R7
x00000
PCx3000
CC
NZP