In: Computer Science
Use MIPS write an assembly program which asks the user to enter
3 number. The program
then sorts these numbers, the prints them from largest to
smallest.
Code Screenshots:
Sample Output:
Code to Copy:
# Declare the required
# variables.
.data
prompt1: .asciiz "Enter the first number: "
prompt2: .asciiz "Enter the second number: "
prompt3: .asciiz "Enter the third number: "
msg: .asciiz "\nThe numbers in sorted order are as follows:\n"
sp: .asciiz " "
.text
.globl main
main:
# Prompt the user to
# enter the first number.
la $a0,prompt1
li $v0,4
syscall
# Read the number.
li $v0,5
syscall
# Store the number in
# the register t1.
move $t1,$v0
# Prompt the user to
# enter the second number.
la $a0,prompt2
li $v0,4
syscall
# Read the number.
li $v0,5
syscall
# Store the number
# in the register t2.
move $t2,$v0
# Prompt the user to
# enter the third number.
li $v0, 4
la $a0, prompt3
syscall
# Read the number.
li $v0,5
syscall
# Store the number
# in the register t3.
move $t3, $v0
# If the first number is
# greater than the
# second, jump to label L1.
bge $t1, $t2, L1
# Otherwise, swap the
# first and second number.
move $t0, $t1
move $t1, $t2
move $t2, $t0
L1:
# If the second number
# is greater than the third,
# jump to Label L2.
bge $t2, $t3, L2
# Otherwise, swap
# the numbers.
move $t0, $t2
move $t2, $t3
move $t3, $t0
L2:
# If the first number is
# still greater than
# the second, jump to the
# label result.
bge $t1, $t2, result
# Otherwise, swap
# the numbers.
move $t0, $t1
move $t1, $t2
move $t2, $t0
result:
# Now, the numbers are stored
# in descending order in the
# registers $t1, $t2, and $t3
# respectively.
# Display an
# informative message.
li $v0, 4
la $a0, msg
syscall
# Display the
# number present
# in register $t1.
li $v0, 1
la $a0, ($t1)
syscall
# Print a space.
li $v0, 4
la $a0, sp
syscall
# Display the
# number present
# in $t2.
li $v0, 1
la $a0, ($t2)
syscall
# Print a space.
li $v0, 4
la $a0, sp
syscall
# Display the number
# present in $t3.
li $v0, 1
la $a0, ($t3)
syscall
# End the program.
li $v0, 10
syscall
.end main