MIPs program design-recursive and non-Recursive Implementation of direct insertion sorting (SPIM simulation)

Source: Internet
Author: User

Non-recursive source code:

. Dataarray :. space 1024 # Open up the array space input_number_msg :. asciiz "Please input number of integers:" input_integer_msg :. asciiz "Please input Integers to be sorted:" output_integer_msg :. asciiz "the sorted integers are :". textmain: la $ A0, input_number_msg # prompt to enter the number of Integers to be sorted li $ v0, 4 syscall li $ v0, 5 syscall la $ T6, array move $ T7, $ zero # initialize T7 for cyclic counting move $ T8, $ V0 # T8 for storing the number of Integers to be sorted input: la $ A0, input_integer_msg # prompt to enter the integer to be sorted li $ v0, 4 syscall li $ v0, 5 syscall move $ T0, $ T7 Mul $ T0, $ T0, 4 Addu $ T1, $ T0, $ T6 SW $ v0, 0 ($ T1) addi $ T7, $ T7, 1 BLT $ T7, $ T8, input # enter an integer to be sorted until the given number in T8 moves $ T2, $ zero # t2: insertion_sort: addi $ T2, $ T2, 1 # I = I + 1 Bge $ T2, $ T8, output # if the number of cycles reaches the given integer, jump to the output Mul $ T1, $ T2, 4 Addu $ T1, $ T1, $ T6 LW $ T3, 0 ($ T1) # T3 = array [I] sub $ T4, $ T2, 1 Mul $ T4, $ T4, 4 Addu $ T4, $ T4, $ T6 LW $ T4, 0 ($ T4) # t4 = array [I-1] Bge $ T3, $ T4, insertion_sort # If array [I]> = array [I-1], check the next element value of the array. move $ T0, $ T3 # Otherwise, set the value of T3 to key and save it to t0 SW $ T4, 0 ($ T1) # array [I] = array [I-1] sub $ T3, $ T2, 2 # J = i-2inner_loop: Mul $ T4, $ T3, 4 Addu $ T4, $ T4, $ T6 LW $ T4, 0 ($ T4) # t4 = array [J] Bge $ T0, $ T4, backspace # If key> = array [J], array [J + 1] = Key addi $ T5, $ T3, 1 # Otherwise, array [J + 1] = array [J] Mul $ T5, $ T5, 4 Addu $ T5, $ T5, $ T6 SW $ T4, 0 ($ t5) sub $ T3, $ T3, 1 # J = J-1 beq $ T3,-1, backspace # If J =-1, array [J + 1] = Key B inner_loopbackspace: addi $ T3, $ T3, 1 Mul $ T3, $ T3, 4 Addu $ T3, $ T3, $ T6 SW $ T0, 0 ($ T3) # array [J + 1] = Key BLT $ T2, $ T8, insertion_sort # If I <T8, check the next element value output: la $ A0, output_integer_msg # output the sorted integer li $ v0, 4 syscall move $ T7, $ zero print_loop: move $ T0, $ T7 Mul $ T0, $ T0, 4 Addu $ T1, $ T0, $ T6 LW $ A0, 0 ($ T1) Li $ v0, 1 syscall addi $ T7, $ T7, 1 BLT $ T7, $ T8, print_loop Jr $ Ra

Source code of recursive form:

. Dataarray :. space 1024 # Open up the array space input_number_msg :. asciiz "Please input number of integers:" input_integer_msg :. asciiz "Please input Integers to be sorted:" output_integer_msg :. asciiz "the sorted integers are :". text # main function, calling the insert sorting function and output function main: subu $ sp, $ sp, 4 SW $ Ra, 0 ($ SP) # main return address into Stack la $ A0, input_number_msg # prompt to input number of Integers to be sorted li $ v0, 4 syscall li $ v0, 5 syscall la $ T6, array move $ T7, $ zero # initialize T7 for cyclic counting move $ T8, $ V0 # T8 for storing the number of Integers to be sorted input: la $ A0, input_integer_msg # prompt to enter the integer to be sorted li $ v0, 4 syscall li $ v0, 5 syscall move $ T0, $ T7 Mul $ T0, $ T0, 4 Addu $ T1, $ T0, $ T6 SW $ v0, 0 ($ T1) addi $ T7, $ T7, 1 BLT $ T7, $ T8, input # enter an integer to be sorted until the given number in T8 moves $ A0, $ T8 # Number of integers n as function parameters stored in A0 Jal insertion_sort # Call the insert sort function Jal output # Call the output function LW $ Ra, 0 ($ SP) # main return address addi $ sp, $ sp, 4 Jr $ Ra # Insert the sorting function insertion_sort (n) insertion_sort: subu $ sp, $ sp, 32 # Save on-site SW $ Ra, 28 ($ SP) SW $ FP, 24 ($ SP) SW $ S0, 20 ($ SP) addi $ FP, $ sp, 32 move $ S0, $ A0 # S0 = n-1 BLT $ S0, 1, sort_ret # If S0 <1, recursively end sub $ A0, $ S0, 1 # a0 = s0-1 Jal insertion_sort # insertion_sort (n-1) beq $ S0, $ T8, sort_ret # If S0 = T8, the function terminates Mul $ T1, $ S0, 4 Addu $ T1, $ T1, $ T6 LW $ T3, 0 ($ T1) # T3 = array [N] sub $ T4, $ S0, 1 Mul $ T4, $ T4, 4 Addu $ T4, $ T4, $ T6 LW $ T4, 0 ($ T4) # t4 = array [n-1] Bge $ T3, $ T4, sort_ret # If array [N]> = array [n-1], recursively move $ T0, $ T3 # Otherwise, set the value of T3 to key and store it to t0 SW $ T4, 0 ($ T1) # array [I] = array [I-1] sub $ T3, $ S0, 2 # J = n-2 beq $ T3,-1, backspace # If J =-1, array [J + 1] = keyinner_loop: Mul $ T4, $ T3, 4 Addu $ T4, $ T4, $ T6 LW $ T4, 0 ($ T4) # t4 = array [J] Bge $ T0, $ T4, backspace # If key> = array [J], array [J + 1] = Key addi $ T5, $ T3, 1 # Otherwise, array [J + 1] = array [J] Mul $ T5, $ T5, 4 Addu $ T5, $ T5, $ T6 SW $ T4, 0 ($ t5) sub $ T3, $ T3, 1 # J = J-1 beq $ T3,-1, backspace # If J =-1, array [J + 1] = Key B inner_loopbackspace: addi $ T3, $ T3, 1 Mul $ T3, $ T3, 4 Addu $ T3, $ T3, $ T6 SW $ T0, 0 ($ T3) # array [J + 1] = keysort_ret: LW $ Ra, 28 ($ SP) # restore site LW $ FP, 24 ($ SP) LW $ S0, 20 ($ SP) addi $ sp, $ sp, 32 Jr $ Ra # output the sorted integer output: subu $ sp, $ sp, 32 # Save on-site SW $ Ra, 28 ($ SP) SW $ FP, 24 ($ SP) SW $ T0, 20 ($ SP) SW $ T1, 16 ($ SP) SW $ T6, 12 ($ SP) SW $ T7, 8 ($ SP) addi $ FP, $ sp, 32 La $ A0, output_integer_msg li $ v0, 4 syscall move $ T7, $ zero print_loop: move $ T0, $ T7 Mul $ T0, $ T0, 4 Addu $ T1, $ T0, $ T6 LW $ A0, 0 ($ T1) Li $ v0, 1 syscall addi $ T7, $ T7, 1 BLT $ T7, $ T8, print_loop LW $ Ra, 28 ($ SP) # restore site LW $ FP, 24 ($ SP) LW $ T0, 20 ($ SP) LW $ T1, 16 ($ SP) LW $ T6, 12 ($ SP) LW $ T7, 8 ($ SP) addi $ sp, $ sp, 32 Jr $ Ra

The initial write of the MIPs program may be inefficient. It will be improved later.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.