Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Reading and Printing SPIM provides a small set of operating system-like services

ID: 3870410 • Letter: R

Question


Reading and Printing SPIM provides a small set of operating system-like services through the system call instruction, named syscall. To request a service, a program loads the system call code (it can be found in MIPS System calls uploaded in the Useful Resources folder in Sakai) into register $v0 and arguments into register file registers $a0-$a3 (or $f12 for floating-point values). System calls that return values put their results in the register $v0 (or $f0 for floating-point results) [1] Reading and printing operations can be done with syscall. In order to become familiar with syscall, read the following example first. # Registers used: $to -used to hold the first number # t1 - used to hold the second number # $t2 - used to hold the difference of the contents of $t1 and $to # $v0 - syscall parameter and return value # $a0 - syscall argument . text main: ## Get first number from the user, store it into $to li $v0, 5 syscall move $to, $v # load syscall read-int into $v0 make the syscall. # move the number read into Sto ## Get second number from the user, put into $t1 li $v0, 5 # load syscall read-int into $v0 syscall # make the syscall move $t1, $v0 # move the number read into register $t1 In this example, the syscall instruction is first used to read input. The system code for this operation is 5. Then we use system code 1 to print this input. Notice that we also use code 10 to terminate the program.

Explanation / Answer

Given below is the code and output. Please rate if it helped . Thank you.


.data
name: .space 15
prompt1: .asciiz " Please enter your name: "
prompt2: .asciiz " Hello "
prompt3: .asciiz "please input an integer and press Enter "
output1: .asciiz "The integer is odd "
output2: .asciiz "The integer is even"
msg1: .asciiz " Goodbye "
msg2: .asciiz "thank you for your input"
.text
# prompt and get input name
li $v0, 4
la $a0, prompt1
syscall
li $v0, 8
la $a0, name #address to store string input
li $a1, 15 #max length of string
syscall
#prompt and get int input
li $v0, 4
la $a0, prompt2
syscall

li $v0, 4
la $a0, name
syscall
li $v0, 4
la $a0, prompt3
syscall
#read int and save to $t0
li $v0, 5
syscall
move $t0, $v0
#number is even if remainder of division is 0 and odd otherwise
li $t1, 2 #load the constant 2 into $t1
div $t0, $t1 #remainder in hi
mfhi $t1 #get the remainder into t1
beqz $t1, even
#display odd
li $v0, 4
la $a0, output1
syscall
b done
even:
#display even
li $v0, 4
la $a0, output2
syscall
done:
#say good bye and exit
li $v0, 4
la $a0, msg1
syscall
li $v0, 4
la $a0, name
syscall
li $v0, 4
la $a0, msg2
syscall
#exit
li $v0, 10
syscall

output


Please enter your name: John

Hello John
please input an integer and press Enter 5
The integer is odd
Goodbye John
thank you for your input
-- program is finished running --


Please enter your name: John

Hello John
please input an integer and press Enter 8
The integer is even
Goodbye John
thank you for your input
-- program is finished running --