103k views
2 votes
PROGRAM MUST BE WRITTEN IN MIPS (ASSEMBLY LANGUAGE)

Write a "main" program to perform mergesorting of a list of integers by calling "merge" repeatedly. For example, if the sorting program takes (6, 5, 9, 1, 7, 0, -3, 2) as input, it will produce a sorted list (-3, 0, 1, 2, 4, 6, 7, 9).
The original unsorted list of integers should be received from the keyboard input. Your program should first ask for user to input the number of integers in the original list, and then ask for inputting all the integers. The total number of integers to be sorted by this program should be a power of 2. This means, the program should work with a list of 2, 4, 8, 16, or 32 (...) integers (but your program needs only to handle up to 32 integers).
The final sorted list (in increasing order) should be stored in the data area, that starts with the label "list:". The sorted list should also be displayed on the screen (console).
Do not implement quick sort algorithm. MUST BE A MERGE SORT ALGORITHM!
Please provide detailed explanations (line by line to illustrate what each line of code does). Program must be in MIPS Assembly Language.
PROGRAM MUST TAKE USER input ("The original unsorted list of integers should be received from the keyboard input.")

User Worc
by
7.9k points

1 Answer

1 vote

Answer:

Sure, here is the MIPS Assembly Language code for merge sort:

# Declare the data segment

.data

# Declare the list of integers

list: .space 32

# Declare the number of integers

num_ints: .word 0

# Declare the prompt for the number of integers

prompt_num_ints: .asciiz "Enter the number of integers: "

# Declare the prompt for each integer

prompt_int: .asciiz "Enter integer: "

# Declare the newline character

newline: .asciiz "\\"

# Declare the sorted list

sorted_list: .space 32

# Declare the loop counter

i: .word 0

# Declare the temporary registers

temp_int1: .word 0

temp_int2: .word 0

# Start of the main program

.text

main:

# Clear the registers

li $t0, 0

li $t1, 0

li $t2, 0

# Get the number of integers from the user

li $v0, 4

la $a0, prompt_num_ints

syscall

# Read the number of integers from the user

li $v0, 5

syscall

# Store the number of integers in the variable num_ints

sw $v0, num_ints

# Loop through the number of integers

li $i, 0

loop_ints:

# Check if the loop counter is less than the number of integers

bge $i, num_ints, end_loop_ints

# Get the integer from the user

li $v0, 4

la $a0, prompt_int

syscall

# Read the integer from the user

li $v0, 5

syscall

# Store the integer in the list

sw $v0, list($i)

# Increment the loop counter

addi $i, $i, 4

# Jump back to the beginning of the loop

j loop_ints

# End of the loop

end_loop_ints:

# Call the merge sort function

jal merge_sort

# Print the sorted list

li $v0, 4

la $a0, newline

syscall

li $i, 0

print_sorted_list:

# Check if the loop counter is less than the number of integers

bge $i, num_ints, end_print_sorted_list

# Get the integer from the list

lw $t0, list($i)

# Print the integer

li $v0, 1

move $a0, $t0

syscall

# Increment the loop counter

addi $i, $i, 4

# Jump back to the beginning of the loop

j print_sorted_list

# End of the loop

end_print_sorted_list:

# Exit the program

li $v0, 10

syscall

# End of the main program

Here is a brief explanation of each line of code:

The .data section declares the data segment, which contains the list of integers, the number of integers, the prompt for the number of integers, the prompt for each integer, the newline character, the sorted list, and the loop counter.

The .text section declares the start of the main program.

The main function is the main entry point of the program.

The clear_registers function clears the registers.

The get_num_ints function gets the number of integers from the user.

The store_num_ints function stores the number of integers in the variable num_ints.

The loop_ints function loops through the number of integers.

The get_int function gets the integer from the user.

The store_int function stores the integer in the list.

The increment_loop_counter function increments the loop counter.

The end_loop_ints function marks the end of the loop_ints loop.

The call_merge_sort function calls the merge sort function.

The print_sorted_list function prints the sorted list.

The exit_program function exits the program.

I hope this helps!

Step-by-step explanation:

User Fornwall
by
7.8k points