In: Computer Science
MIPS Program to get two Numbers and Output the sum, the difference between two number (num1-num2) and (num2-num1) :
.data #start of data segment
# defining prompt message to be displayed.
prompt1: .asciiz "Enter First Number "
prompt2: .asciiz "Enter Second Number "
result_prompt1: .asciiz "\nThe sum of two number is "
result_prompt2: .asciiz "\nThe difference (num1 - num2) is "
result_prompt3: .asciiz "\nThe difference (num2 - num1) is "
.text # start of code segment
main: #main
li $v0,4 # this instruction is used to print string
la $a0,prompt1 # preparing string to be displayed.
syscall # issue system call to display string
li $v0,5 #this instruction is used to get integer input.
syscall #issue system call to get user input.
move $t0,$v0 #move entered value to register $t0
li $v0,4 # this instruction is used to print string
la $a0,prompt2 # preparing string to be displayed.
syscall # issue system call to display string
li $v0,5 #this instruction is used to get integer input.
syscall #issue system call to get user input.
move $t1,$v0 #move entered value to register $t1
add $s0,$t0,$t1 # add to entered values and save result in $s0 register
li $v0,4 # this instruction is used to print string
la $a0,result_prompt1 # preparing string to be displayed.
syscall # issue system call to display string
li $v0,1 #prepare to display variable value
move $a0,$s0 # move addition result to $a0
syscall #issue system call
sub $s1,$t0,$t1 # substract two values entered by user (num1-num2) and store in register $s1
li $v0,4 # this instruction is used to print string
la $a0,result_prompt2 # preparing string to be displayed.
syscall # issue system call to display message
li $v0,1 # prepare to display variable value
move $a0,$s1 # move substraction result to $a0
syscall # issue system call
sub $s2,$t1,$t0 # substract two values entered by user (num2-num1) and store in register $s2
li $v0,4 # this instruction is used to print string
la $a0,result_prompt3 # preparing string to be displayed.
syscall # issue system call to display message
li $v0,1 # prepare to display variable value
move $a0,$s2 # move substraction result to $a0
syscall # issue system call
Sample Output :
Please refer to the screenshot of code below to understand indentation of code :