# iodemo.s - Demonstrate the various I/O system calls in MIPS. # # Performing a system call requires several instructions: # 1. Use li to set the $v0 to the function number. # 2. Use move or la to initialize the argument(s), if necessary. # 3. Call syscall. # 4. If input, move the data out of the default register. # # The system call code numbers we specify in $v0 are as follows. # 1 = output the integer in $a0 # 2 = output the float in $f12 # 3 = output the double in $f12 # 4 = output the string whose address is in $a0 # 5 = input an integer into $v0 # 6 = input a float into $f0 # 7 = input a double into $f0 # 8 = input a string of length $a1 into space pointed by $a0 (verify) # 10 = quit the program .data prompt_int: .asciiz "Please enter an integer: " prompt_float: .asciiz "Enter a single-precision real number: " prompt_double: .asciiz "Enter a double-precision real number: " prompt_string: .asciiz "Enter a string: " max_length: .word 20 users_string: .space 20 # Includes the null character intro_int: .asciiz "Your integer is " intro_float: .asciiz "Your float is " intro_double: .asciiz "Your double is " intro_string: .asciiz "Your string is " newline: .asciiz "\n" # Rather than having a separate string for newline, # we could have alternatively included the \n at the beginning # of each introductory phrase. That would have yielded less code, # but the presence of all those \n may have been less intuitive. # Register use # $s0 = The user's integer value # $f2 = The user's float value # $f4 = The user's double value # $s1 = Address of the user's string # $s2 = Maximum length of the user's string .text main: la $s1, users_string # Initialize constants useful later. lw $s2, max_length # Use lw to load data VALUE into register. # ------------------------------------------------------------------- li $v0, 4 # Ask for integer la $a0, prompt_int syscall li $v0, 5 # Put user's integer into $s0 syscall move $s0, $v0 # ------------------------------------------------------------------- li $v0, 4 # Ask for float la $a0, prompt_float syscall li $v0, 6 # Put user's float into $f2 syscall mov.s $f2, $f0 # ------------------------------------------------------------------- li $v0, 4 # Ask for double la $a0, prompt_double syscall li $v0, 7 # Put user's double into $f4 syscall mov.d $f4, $f0 # ------------------------------------------------------------------- li $v0, 4 # Ask for string la $a0, prompt_string syscall li $v0, 8 # User's string -> pointed to by $s1 move $a0, $s1 # and users_string move $a1, $s2 # Specify maximum size to accept. syscall # No additional moving necessary. # =================================================================== # Output # =================================================================== li $v0, 4 # Introduce integer la $a0, intro_int syscall li $v0, 1 # Print the integer contained in $s0 move $a0, $s0 syscall li $v0, 4 # Print newline la $a0, newline syscall # ------------------------------------------------------------------- li $v0, 4 # Introduce float la $a0, intro_float syscall li $v0, 2 # Print the float contained in $f2 mov.s $f12, $f2 syscall li $v0, 4 # Print newline la $a0, newline syscall # ------------------------------------------------------------------- li $v0, 4 # Introduce double la $a0, intro_double syscall li $v0, 3 # Print the double contained in $f4 mov.d $f12 $f4 syscall li $v0, 4 # Print newline la $a0, newline syscall # ------------------------------------------------------------------- li $v0, 4 # Introduce string la $a0, intro_string syscall li $v0, 4 # Print the string pointed to by $s1 move $a0, $s1 syscall li $v0, 4 # Print newline la $a0, newline syscall # =================================================================== li $v0, 10 # End of program syscall