![]() |
Jupyter at Bryn Mawr College |
|
|
Public notebooks: /services/public/dblank / CS240 Computer Organization / 2015-Fall / Notes |
A bit of review, by way of one of our authors:
The Motivated Bottom-up Approach:
specifically see:
Notes:
This notebook explores the TRAP vectors in Input/Output, I/O, in the LC-3.
There are a number of TRAP vectors defined by the LC-3 operating system. These
Trap Vector | Assembly Name | Use |
---|---|---|
x20 | GETC | Read a char into R0 |
x21 | OUT | Write a char in R0 to screen |
x22 | PUTS | Write a string (pointer in R0) to screen |
x23 | IN | Prompt, read a char into R0, and echo |
x24 | PUTSP | Write a PACKED string (pointer in R0) to screen |
x25 | HALT | Stop execution |
1. Programmer writes:
1111 0000 0010 0011: TRAP x23, or IN
2. Looks in Trap Vector Table, position x23
Saves PC in R7
3. Gets address of routine, sets PC
4. Executes routine
5. Last line of routine is JMP R7, or RET
6. Execution continues where it left off before trap routine
1100 000 111 000000: JMP R7, or RET
Read a char into R0.
.ORIG x3000
TRAP x20
HALT
.END
%exe
Write a char in R0 to screen
.ORIG x3000
AND R0, R0, #0
ADD R0, R0, x07 ;; ASCII A: 65, a: 97
TRAP x21
HALT
.END
%exe
Write a string (pointer in R0) to screen
PUTS will display a string pointed to by R0, ending in lf (x0A) and \0 (x00) also called nul.
A string is a series of characters in memory:
x3003: H
x3004: e
x3005: l
x3006: l
x3007: o
x3008: \n [linefeed]
x3009: \0 [nul]
But we can't put characters in like that. We must either do this:
x3003: x0048
x3004: x0065
x3005: x006C
x3006: x006C
x3007: x006F
x3008: x000A
x3009: x0000
.ORIG x3000
LEA R0, WORD
TRAP x22
HALT
WORD: .FILL x0048
.FILL x0065
.FILL x006C
.FILL x006C
.FILL x006F
.FILL x000A
.FILL x0000
.END
%exe
or do this:
.ORIG x3000
LEA R0, WORD
TRAP x22
HALT
WORD: .STRINGZ "Hello\n"
.END
%dis x3000 x3009
%exe
Prompt, read a char into R0, and echo
.ORIG x3000
TRAP x23
HALT
.END
%exe
The PUTSP is a put-string-packed. Memory contains ASCII letters, two-per word. But note that they are encoded from right to left:
e H
l l
\0 o
In hex that would be:
x65 48
x6c 6c
x00 6f
A newline is represented as a LF (line feed), #10, x0A.
x65 48
x6c 6c
x20 6f
x00 0A
.ORIG x3000
LEA R0, WORD
TRAP x24
HALT
WORD: .FILL x6548
.FILL x6c6c
.FILL x206f
.FILL x000A
.END
%exe
Write a game program to do the following: A person is sitting at a keyboard. Each time the person types a capital letter, the program outputs the lowercase version of that latter. If the person types a character "7", the program terminates.
.ORIG x3000
LD R2, TERM ;; load negative ASCII 7
LD R3, ASCII ;; load 'a' - 'A' (32)
AGAIN: TRAP x23 ;; Get
ADD R1, R2, R0
BRz EXIT
ADD R0, R0, R3
TRAP x21
BRnzp AGAIN
TERM: .FILL xFFC9
ASCII: .FILL x0020
EXIT: TRAP x25
.END
%exe