Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Class 7.1 Integers and Characters Input/Output System Calls. Syscall Trap Handler Services for Integers and Characters Read Integer, Print Integer, Read Character, Print Character, Example Program No Input Line Buffer correction. Textbook: Appendix A (a43), System Calls Central Connecticut State University, MIPS Tutorial. Chapter 22. Integers and Characters Input / Output Up until now, your programs have been running with SPIM used as a computer with no machine code in it but your own. Organizing Input/Output by the user program is very costly process causing duplication of significant parts of the program code. In even the simplest computer, putting a character on the screen involves many instructions and a detailed knowledge of the video card. One of the Operating System’s task is to implement all these input output operations and allow the user programs to use the high level input/output services provided by the OS. Most computer systems run under the control of an operating system. Application programs use the services of the operating system to do input and output and other system tasks. User Program Direct I/O System Call Operating System Input/Output 1 Different Operating Systems provide different I/O services for user programs. SPIM does not have an operating system, but it can run a small trap handler — a set of services that is a great help in writing assembly programs. SPIM includes a "trap handler" that simulates a tiny operating system that can do input from the keyboard and output to the monitor. System Calls. syscall Assembly language programs request operating system services using the syscall instruction. The syscall instruction transfers control to the operating system which then performs the requested service. Then control (usually) returns to the program. (This description leaves out many details). syscall # ask the operating system to perform a service Current Program running on the Computer Service Call by program itself The Current Program is interrupted Current Program Continues from the interrupted point Interrupt Servicing Program in Computer Interrupt is Serviced Different operating systems use this instruction in different ways. For the SPIM trap handler integer and character services it is used like this: ori $2,$0,code # Load $2($v0) with the "code" number of an OS service. ....... # Put parameters for the service in register $4 ($a0) syscall # Invoke the operating system. # Return value (if any) is in $2 2 Different services use different registers, and not all services return values to the caller. Trap Handler Services for Integers and Characters Here are some of the services of the SPIM trap handler. The following pages explain how to use them. The print services write characters to the simulated monitor of SPIM. The read services read characters from the keyboard and (for numeric read services) convert character strings into the appropriate type. Service Code in $2 ($v0) print integer 1 read integer 5 print character 11 read character 12 Arguments Returned Value ($a0) $4 == integer to print $2 ($v0) <-- integer ($a0) $4 == character to print $2 ($v0) <-- character Read Integer, Print Integer The read integer service reads an entire line of input from your keyboard—all the characters you type up to the newline character. These characters are expected to be ASCII digits '0', '1', .., '9' with an optional leading '-' or '+'. The characters are converted into a 32-bit two's complement representation of the integer which is returned in $v0. ori $2,$0,5 syscall # # # # # code 5 == read integer Invoke the operating system. Read in one line of ascii characters. Convert them into a 32-bit integer. ($v0) $2 <-- two's comp. int. The terminal gives to computer only one character at a time. That is why to get any other than character type of information (integer, string) OS reads characters one by one and translates them to the type we need. $a0 to the SPIM terminal. Of course, there are many ways that the integer can be placed in $a0. The print integer service prints the integer represented by the 32 bits in ori $2,$0,1 # code 1 == print integer ori $4,$0,126 # $a0 == the integer syscall # Invoke the operating system. # Convert the 32-bit integer into characters. # Print the character to the monitor. 3 Read Character, Print Character The read character service reads one character from your keyboard. The character’s ASCII code is returned in $v0. The print character service prints the character represented in the lowest order byte of the 32 bits in $a0 to the SPIM terminal. Characters 0x0D and 0x0A could be used for moving the console cursor to the next line’s first position. This is the standard End Of Line “\n” character’s behavior. Example Program Below is the example program code with both integer/character input/output. ## integer/character input/output.asm ## .text .globl __start __start: ori ori syscall $4,$0,129 $2,$0,1 # prepare integer in $4 ($a0) # print integer from $4 ori ori syscall $4,$0,10 $2,$0,11 # prepare character EOL in $4 ($a0) # print character End Of line ori syscall $2,$0,5 # read integer to $2($v0) or ori syscall $4,$0,$2 $2,$0,1 # print integer after reading ori ori syscall $4,$0,13 $2,$0,11 # prepare character EOL in $4 ($a0) # print character End Of line ori syscall $2,$0,12 # read character to $2($v0) or ori syscall ## End of file $4,$0,$2 $2,$0,11 # copy read character from $2 to $4 # print the character which was read 4 No Input Line Buffer Correction The user might make a mistake in entering the character and try to correct it by hitting the "backspace" key to erase the mistake. But this does not work. The ascii value of the key 0x08 is included in the string just as any character value. Odd byte values show up (in SPIM) as a small black rectangle. Most operating system user interfaces allow the user to edit a string (with BS, DEL, arrow and other keys) before it is sent to an application program. The application program asks for a string from the user, but the OS does the actual input, complete with user editing. (This is sometimes called "cooked" input mode). The SPIM trap handler service does not do this. Characters are sent directly to the program as they were typed. If the user hits the backspace character, then that character is placed in the input string. (This is called "raw" input mode). You could, of course, write your program so that it looks at the raw characters in the buffer and edits the input string. This is a messy task, and often skipped, but should not be. 5