14
Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines

Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines

Embed Size (px)

Citation preview

Page 1: Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines

Computer Science 210Computer Organization

Strings, I/O, and Trap Service Routines

Page 2: Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines

Strings

• Sequences of characters, represented internally as ASCII values

• Basic ASCII is 8 bits, stored in lower order byte, with higher order byte clear

• Can also store two characters per 16-bit word

Page 3: Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines

;; Author: Ken Lambert

;; This program declares the string "Assembler is fun!"

.ORIG x3000

; Program codeHALT

; Data variablesMESSAGE .STRINGZ "Assembler is fun!"

.END

The STRINGZ directive puts a string’s ASCII values in consecutive memory cells, followed by a null character (ASCII 0)

Page 4: Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines

;; Author: Ken Lambert

;; This program outputs the string "Assembler is fun!"

;; Pseudocode design:

; for ch in "Assembler is fun!"; loop while display status >= 0; print ch

.ORIG x3000

;; Register usage:; R1 = contents of display status register; R0 = contents of display data register; R2 = address of the next character in the string

; Main program codeLEA R2, MESSAGE ; Get the base address of the string

CHLOOP LDR R0, R2, #0 ; Get the next character from the stringBRz ENDMAIN ; Quit when it's null (end of string)

POLL LDI R1, DSR ; Poll for negative display status register BRzp POLL ; (ready bit = 1)STI R0, DDR ; Display is ready, so output the characterADD R2, R2, #1 ; Increment the character's addressBR CHLOOP

ENDMAIN HALT

; Main program dataDSR .FILL xFE04 ; Address of the display status registerDDR .FILL xFE06 ; Address of the display data registerMESSAGE .STRINGZ "Assembler is fun!"

.END

String output with array-based loop and polling

Page 5: Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines

;; Author: Ken Lambert

;; This program outputs the string "Assembler is fun!"

;; Pseudocode design:

; print "Assembler is fun!"

;; Register usage:; R0 = base address of the string

.ORIG x3000

; Main program codeLEA R0, MESSAGE ; Load the address of the stringPUTS ; Call the trap service routine to output itHALT

; Main program dataMESSAGE .STRINGZ "Assembler is fun!"

.END

String output with trap service routine PUTS

Using the trap service routine reduces 8 lines of code to 2

Page 6: Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines

LC-3 Trap Service Routines

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 INprint prompt to console,read and echo character from keyboard

x25 HALT halt the program

The first four routines work with data in R0

Page 7: Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines

System Call

1. User program invokes system call.

2. Operating system code performs operation.

3. Returns control to user program.

In LC-3, this is done through the TRAP mechanism.

Page 8: Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines

LC-3 TRAP Mechanism

1. 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

2. Table of starting addresses.

– stored at x0000 through x00FF in memory

– called System Control Block in some architectures

3. TRAP instruction.

– used by program to transfer control to operating system

– 8-bit trap vector names one of the 256 service routines

4. A linkage back to the user program.

– want execution to resume immediately after the TRAP instruction

Page 9: Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines

TRAP Instruction

• Trap vector– identifies which system call to invoke– 8-bit index into table of service routine addresses

• in LC-3, this table is stored in memory at 0x0000 – 0x00FF• 8-bit trap vector is zero-extended into 16-bit memory address

• Where to go– lookup starting address from table; place in PC

• How to get back– save address of next instruction (current PC) in R7

Page 10: Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines

Data Path for the TRAP

NOTE: PC has already been incrementedduring instruction fetch stage.

Page 11: Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines

RET (JMP R7)• How do we transfer control back to

instruction following the TRAP?

• We saved old PC in R7.– JMP R7 gets us back to the user program at the right spot.

– LC-3 assembly language lets us use RET (return)in place of “JMP R7”.

• Must make sure that service routine does not change R7, or we won’t know where to return.

Page 12: Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines

9-12

TRAP Mechanism Operation

1. Lookup starting address.2. Transfer to service routine.3. Return (JMP R7).

Will form the basis for defining our own procedures

Page 13: Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines

String Input

• Usually terminated by a return character (ASCII 13)

• Use GETC to input and OUT to echo

• If not ASCII 13, store character in an array

• Otherwise, quit the loop

• Store a null character at the end of the characters in the array

Page 14: Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines

;; Register usage:; R0 = the input character; R1 = the newline character; R2 = base address of the array; R3 = temporary working storage

; Main program codeLEA R0, PROMPT ; Display the promptPUTSLD R1, RT ; Initialize the return characterLEA R2, ARRAY ; Get the base address of the array

WHILE GETC ; Read and echo a character (stored in R0)OUTADD R3, R0, R1 ; Quit if character = returnBRz ENDWHILESTR R0, R2, #0 ; Store that character in the arrayADD R2, R2, #1 ; Increment the address of the array cellBR WHILE ; Return to read another character

ENDWHILE STR R3, R2, #0 ; Store the null character after the last input

LEA R0, ARRAY ; Output the string PUTS

HALT

; Main program dataRT .FILL x-000D ; The return character (negated)PROMPT .STRINGZ "Enter your name: "ARRAY .BLKW 30 ; Array of 30 characters (including null)

.END

String input with sentinel-based loop

Problem: input could overflow the array, but no data are declared below it.