Presentation is loading. Please wait.

Presentation is loading. Please wait.

The SPIM Trap Handler Syscall Trap handler services String operations File operations Memory allocation Central Connecticut State University, MIPS Tutorial.

Similar presentations


Presentation on theme: "The SPIM Trap Handler Syscall Trap handler services String operations File operations Memory allocation Central Connecticut State University, MIPS Tutorial."— Presentation transcript:

1 The SPIM Trap Handler Syscall Trap handler services String operations File operations Memory allocation Central Connecticut State University, MIPS Tutorial. Chapter 22.

2 syscall - ask the operating system to perform a service Syscall - review li $v0,code # Load $v0 with the "code" number # of an OS service........ # Put parameters for the service in....... # registers $a0, $a1 or $f12. syscall # Invoke the operating system. # Return value (if any) is in $v0 or $f0

3 Print String Code in $v0 ArgumentsOutput or Returned Value 4 $a0 == address of first character in null terminated string to print

4 li $v0,4 # code 4 == print string la $a0,string # $a0 == address of the string syscall # Ask the operating system to # perform the service......data string:.asciiz "Hello SPIM!\n" Print string example The string should be null terminated. The 0x00 character signals to trap handler to stop printing on the console That is why the string is declared as.asciiz What if we forget to end the string by NULL character ? Trap handler will print until reaching any NULL character.

5 Read String 8 $a0 == buffer address to place string $a1 == buffer length (max number of characters to read) Reads standard input into address in $a0 Code in $v0 ArgumentsOutput or Returned Value

6  $a1 contains the length (in bytes) of the input buffer.  Up to ($a1)-1 characters are read from the keyboard and placed in buffer as a null terminated string  The last byte of the buffer is filled with null 0x00.  The "enter" character appears in the buffer as the byte 0x0a.  This byte is followed by the null byte 0x00. Read String li $v0,8 # code 8 == read string la $a0,buffer # $a0==address of buffer li $a1,16 # $a1 == buffer length syscall......data buffer:.space 16 # reserve 16 bytes

7 File operations  Open File  For writing  Create, Truncate(erase), Append  For reading  Read, Write  Close File User program OS (Trap Handler) syscall – Open File Open File Create (Tr., App. if needed) File Name, Flags File Descriptor or Error Message syscall – Read, Write File Do Read, Write File Descriptor Information or Error Message End of File or End of Operation syscall – Close File Do Close File Descriptor

8 13 $a0 = file name address (zero terminated string with no line feed) $a1 = flags, $a2 = UNIX octal file mode (0644 for rw-r--r--) $v0 = file descriptor Open file Code in $v0 ArgumentsOutput or Returned Value Flags for file operations: Read = 0x0, Write = 0x1, Read/Write = 0x2 OR Create = 0x100, Truncate = 0x200, Append = 0x8 OR Text = 0x4000, Binary = 0x8000

9 Rd 14 $a0 = file descriptor, $a1 = buffer address, $a2 = amount to read in bytes $v0 = number of characters read from file (-1 = error, 0 = end of file) Read / Write File Code in $v0 ArgumentsOutput or Returned Value Wr 15 $a0 = file descriptor, $a1 = buffer address, $a2 = amount to write in bytes $v0 = number of characters written to file (-1 = error)

10 Close the file Code in $v0 ArgumentsOutput or Returned Value 16$a0 = file descriptor

11 Write File.data fout:.asciiz "testout.txt" # filename for output buffer:.asciiz "The quick brown fox jumps over the lazy dog.".text # Open (for writing) a file that does not exist li $v0, 13 # system call for open file la $a0, fout # output file name li $a1, 1 # Open for writing (flags are 0: read, 1: write) li $a2, 0 # mode is ignored syscall # open a file (file descriptor returned in $v0) move $s6, $v0 # save the file descriptor # Write to file just opened li $v0, 15 # system call for write to file move $a0, $s6 # file descriptor la $a1, buffer # address of buffer from which to write li $a2, 44 # hardcoded buffer length syscall # write to file # Close the file li $v0, 16 # system call for close file move $a0, $s6 # file descriptor to close syscall # close file

12 Read File.data fin:.asciiz "testin.txt" # filename for input buffer:.space 100.text # Open (for reading) a file. li $v0, 13 # system call for open file la $a0, fin # file name li $a1, 0x0 # Open for reading (flags are 0: read, 1: write) li $a2, 0 # mode is ignored syscall # open a file (file descriptor returned in $v0) move $s6, $v0 # save the file descriptor # Read the file just opened li $v0, 14 # system call for read to file move $a0, $s6 # file descriptor la $a1, buffer # address of buffer to read from file li $a2, 100 # hardcoded buffer length syscall # read from file # Close the file li $v0, 16 # system call for close file move $a0, $s6 # file descriptor to close syscall # close file

13 Allocate Memory Code in $v0 ArgumentsOutput or Returned Value 9$a0 == number of bytes to allocate $v0 <-- address of allocated memory


Download ppt "The SPIM Trap Handler Syscall Trap handler services String operations File operations Memory allocation Central Connecticut State University, MIPS Tutorial."

Similar presentations


Ads by Google