#This program demonstrates the use of syscalls. See the MARS online help for further information. #Simulators like MARS usually provide a small set of "service routines", which emulate the services usually #provided by an operating system, and accessible via the syscall instruction. #author: Hornick #CS2710 - 1/5/13 .data welcome_msg: .asciiz "Hello World!" #defines an ascii string terminated with a null (0) character error_msg: .asciiz "Oh no! An error occurred!" goodbye_msg: .asciiz "Bye!" .text #.text directive (default) just tells the assembler that the following instructions are for the .text segment of memory main: #this label is assigned the value of the address of the next instruction # The following instructions demonstrate how to prepare to invoke a syscall. Every syscall # requires $v0 to be set to a value corresponding to the service that will be invoked by the # syscall. Depending on the particular syscall, other registers may also have to be set to # specific values. li $v0, 4 #function code for "print string" service la $a0, welcome_msg #load the address of the welcome message string into $a0 syscall #invoke the service routine # The following instructions demonstrate how to use procedures to invoke various syscalls. la $a0, error_msg #load the address of the string to be printed jal ErrorMessageDialog #call the ErrorMessageDialog procedure la $a0, goodbye_msg #load the address of the string to be printed jal print_msg #call the print_msg procedure jal InfoMessageDialog #call the InfoMessageDialog procedure jal exit #call the procedure that causes the program to exit ################################################################################################## # This is procedure print_msg. It encapsulates the syscall to the "print string" service routine. # Note that this procedure assumes that the calling routine has loaded the address of the string # to be printed into $a0 print_msg: sw $ra, ($sp) # push the return address onto the stack addi $sp, $sp, -4 # decrement $sp (by 4 bytes) to point at the next available stack location li $v0, 4 # function code for "print string" system service syscall # invoke the service routine addi $sp, $sp, 4 # increment $sp (by 4 bytes) to point to the return address lw $ra, ($sp) # pop the return address from the stack jr $ra # return to the instruction referenced by $ra (return address register) # end procedure print_msg ################################################################################################## ################################################################################################## # This is procedure InfoMessageDialog. It encapsulates the syscall to the "MessageDialog" service routine, # specifying that an informational dialog should be displayed. # Note that this procedure assumes that the calling routine has loaded the address of the string # to be printed into $a0. InfoMessageDialog: sw $ra, ($sp) # push the return address onto the stack addi $sp, $sp, -4 # decrement $sp (by 4 bytes) to point at the next available stack location li $v0, 55 # function code for "print string" system service li $a1, 1 # function code for informational message syscall # invoke the service routine addi $sp, $sp, 4 # increment $sp (by 4 bytes) to point to the return address lw $ra, ($sp) # pop the return address from the stack jr $ra # return to the instruction referenced by $ra (return address register) # end procedure InfoMessageDialog ################################################################################################## ################################################################################################## # This is procedure ErrorMessageDialog. It encapsulates the syscall to the "MessageDialog" service routine, # specifying that an error dialog should be displayed. # Note that this procedure assumes that the calling routine has loaded the address of the string # to be printed into $a0. ErrorMessageDialog: sw $ra, ($sp) # push the return address onto the stack addi $sp, $sp, -4 # decrement $sp (by 4 bytes) to point at the next available stack location li $v0, 55 # function code for "print string" system service li $a1, 0 # function code for error message syscall # invoke the service routine addi $sp, $sp, 4 # increment $sp (by 4 bytes) to point to the return address lw $ra, ($sp) # pop the return address from the stack jr $ra # return to the instruction referenced by $ra (return address register) # end procedure ErrorMessageDialog ################################################################################################## ################################################################################################## # This is procedure exit. It encapsulates the syscall to the "terminate execution" service routine. # Note that this procedure never returns, so it is not necessary to implement stack management. exit: li $v0, 10 # function code for "terminate execution" syscall # invoke the service routine # end procedure exit ##################################################################################################