# merge.s -- Merge 2 sorted lists into one. The data are doubles. # Let's assume each list ends with the number 0.0. # $s1 = base address of list 1 # $s2 = base address of list 2 # $s3 = base address of list 3 # $s4 = base address of arbitrary list for print_list function # $t1 = total address of list 1 # $t2 = total address of list 2 # $t3 = total address of list 3 # $t4 = total address of arbitrary list for print_list function # $f20 = sentinel value indicating end of a list (set to 0.0) # $f22 = value from list 1 # $f24 = value from list 2 #-------------------------------------------------------------------- .data sentinel: .double 0.0 list1: .double 7.2 9.5 10.3 15.6 18.1 20.9 22.6 23.3 27.5 30.1 0.0 list2: .double 6.7 8.3 8.8 16.9 17.7 19.4 21.2 22.9 33.7 36.2 0.0 list3: .space 800 # make room for 100 doubles -- more than enough moving_str: .asciiz " is being moved into list 3.\n" announce_list1: .asciiz "Here is list 1:\n" announce_list2: .asciiz "Here is list 2:\n" announce_list3: .asciiz "Here is list 3:\n" newline: .asciiz "\n" #-------------------------------------------------------------------- .text main: la $gp, sentinel # initialize $f20 to the value 0.0 l.d $f20, 0($gp) la $s1, list1 # set base addresses of all 3 lists la $s2, list2 la $s3, list3 #------------------------------------- # Begin by showing user lists 1 and 2. #------------------------------------- li $v0, 4 # introduce list 1 la $a0, announce_list1 syscall move $s4, $s1 # pass base address of list 1 for printing jal print_list # print list 1 li $v0, 4 # introduce list 2 la $a0, announce_list2 syscall move $s4, $s2 # pass base address of list 2 for printing jal print_list #--------------------------------------------------- # Now, time to merge the lists together into list 3. #--------------------------------------------------- move $t1, $s1 # initialize total address to start at base move $t2, $s2 move $t3, $s3 loop: l.d $f22, 0($t1) # get number from list 1 l.d $f24, 0($t2) # get number from list 2 #----------------------------------------------------------- # ALGORITHM TO PERFORM MERGE: # # if the number from list 1 is the sentinel end value, # empty the rest of list 2 into list 3 # else if the number from list 2 is the sentinel end value, # empty the rest of list 1 into list 3 # else if the number from list 1 is smaller, # put it in list 3 and get next number from list 1 # else if the number from list 2 is smaller, # put it in list 3 and get next number from list 2 #---------------------------------------------------------- #--------------------------------- # Here we empty list 2 into list 3 #--------------------------------- #--------------------------------- # Here we empty list 1 into list 3 #--------------------------------- #-------------------------------------------------------------- # Here we copy the element from list 1 to list 3, and continue. #-------------------------------------------------------------- #-------------------------------------------------------------- # Here we copy the element from list 2 to list 3, and continue. #-------------------------------------------------------------- #---------------------------------- # Now we are ready to print list 3. #---------------------------------- after_loop: li $v0, 4 # introduce output la $a0, announce_list3 syscall move $s4, $s3 jal print_list li $v0, 10 # end of program syscall #--------------------------------------------------------------------- # notify_move function: Tell the user what number is being put into # list 3. We assume that the number to print is already in $f12. notify_move: li $v0, 3 # print what number is being moved syscall li $v0, 4 la $a0, moving_str syscall jr $ra #--------------------------------------------------------------------- # empty list function # $f26 = element loaded from either list 1 or list 2 # $t4 = address where we begin to load elements from empty_list: sw $ra, 0($sp) # save return address addi $sp, $sp, -4 l.d $f26, 0($t4) c.eq.d $f26, $f20 # is there nothing left in list? bc1t after_loop empty_list_loop: mov.d $f12, $f26 # tell user we're moving which element jal notify_move s.d $f26, 0($t3) # put list 1 (or list 2) element into list 3 addi $t4, $t4, 8 # point to next element addi $t3, $t3, 8 l.d $f26, 0($t4) # get next element --> $f26 c.eq.d $f26, $f20 bc1f empty_list_loop addi $sp, $sp, 4 # restore return address lw $ra, 0($sp) jr $ra #-------------------------------------------------------------------- # print list function # Since all we're really doing with the floating-point numbers # in the list is printing them, let's just load each one into $f12. print_list: #----------------------------------------- # Add code here to implement the function. #----------------------------------------- jr $ra