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

Slides:



Advertisements
Similar presentations
Catch up on previous topics Summary and putting together first four classes.
Advertisements

MIPS Assembly Language CPSC 321 Computer Architecture Andreas Klappenecker.
5Z032 Processor Design SPIM, a MIPS simulator Henk Corporaal
I/O: SPARC Assembly Department of Computer Science Georgia State University Georgia State University Updated Spring 2014.
SPIM and MIPS programming
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 2)
Syscall in MIPS Xinhui Hu Yuan Wang.
MIPS Function Continued
MIPS Assembly Language Programming
Input and Output CS 215 Lecture #20.
1 Computer Architecture MIPS Simulator and Assembly language.
Assembly Language Working with the CPU.
1 COMS 361 Computer Organization Title: Instructions Date: 9/28/2004 Lecture Number: 10.
ECE 0142 Recitation #5.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction.
Computer Organization - Syscalls David Monismith Jan. 28, 2015 Based on notes from Patterson and Hennessy Text and from Dr. Bill Siever.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Input and Output Computer Organization and Assembly Language: Module 9.
MIPS I/O and Interrupt. .data val1:.float 0.6 val2:.float 0.8 msg_done:.asciiz "done\n".text.globl main main: li.s $f0, mfc1 $a0, $f0 jal calsqrt.
Strings in MIPS. Chapter 2 — Instructions: Language of the Computer — 2 Character Data Byte-encoded character sets – ASCII: 128 characters 95 graphic,
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
READING AND WRITING FILES. READING AND WRITING FILES SEQUENTIALLY  Two ways to read and write files  Sequentially and RA (Random Access  Sequential.
MIPS I/O and Interrupt.
Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM.
All code must be commented! Each problem part (1,2,3a,3b,…) will be in a separate file: problem_1.s …. You may be asked to demonstrate your program. You.
Nachos Lecture 2 Xiaorui Sun. Phase 2 You have got one machine (machine package) You have to implements the incomplete OS (userprog package) Run programs.
MIPS coding. slt, slti slt $t3, $t1, $t2 – set $t3 to be 1 if $t1 < $t2 ; else clear $t3 to be 0. – “Set Less Than.” slti $t3, $t1, 100 – set $t3 to be.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
Erik Jonsson School of Engineering and Computer Science FEARLESS Engineeringwww.utdallas.edu/~pervin EE/CE 2310 – HON/002 Introduction to Digital Systems.
Integers/Characters Input/Output Integers and Characters Input/Output System Calls. syscall Trap Handler Services for Integers and Characters Read Integer,
MIPS Architecture Topics –What resources MIPS assembly manipulates –CPU (Central Processing Unit) –ALU (Arithmetic & Logical Unit), Registers –Memory –I/O.
Files A collection of related data treated as a unit. Two types Text
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
File I/O. I/O Flags Flags are passed to give some information about how the file is to be used. – Read only file – flag=0x0 – Write only file – flag=0x1.
CS 312 Computer Architecture & Organization
System Calls & Arithmetic
MIPS Assembly Language Programming
MIPS Instruction Set Advantages
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Computer Science 210 Computer Organization
Integers/Characters Input/Output
Protection of System Resources
Chapter 7 Text Input/Output Objectives
Computer Science 210 Computer Organization
Plan for the Day: I/O (beyond scanf and printf)
File Input/Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
MIPS I/O and Interrupt.
Introduction to Assembly Language Programming
Week 5 Computers are like Old Testament gods; lots of rules and no mercy. Joseph Campbell.
Ken D. Nguyen Department of Computer Science Georgia State University
Computer Science 210 Computer Organization
Chapter 14 - Advanced C Topics
Computer Science 210 Computer Organization
MIPS coding.
TRAP Routines Subroutines Privileged Instructions
Компьютерийн зохион байгуулалт, ассемблер CS201
File Input and Output.
Компьютерийн зохион байгуулалт, ассемблер CS201
Chapter 7 Assembly Language
Ken D. Nguyen Department of Computer Science Georgia State University
CS 286 Computer Architecture & Organization
Week 5 Computers are like Old Testament gods; lots of rules and no mercy. Joseph Campbell.
Introduction to SPIM Simulator
Interrupts & Syscalls.
Presentation transcript:

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

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

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

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.

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

 $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

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

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

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)

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

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

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

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