Bubble sort algortiması optimizasyonu

James Gosling

Kilopat
Katılım
21 Şubat 2016
Mesajlar
985
Makaleler
9
Çözümler
8
Daha fazla  
Cinsiyet
Erkek
Kod:
.text
main:
    # Menu prompt
    li $v0, 4
    la $a0, menu
    syscall  
    li $v0, 5
    syscall
    beq $v0, 1, generationStart
    beq $v0, 2, exit
    j main

    generationStart:
        li $v0, 4
        la $a0, enterIntegers
        syscall
        jal generateLinkedList
        move $t0, $v0        # head of the unsorted list is in t0
        move $t1, $v1        # size of the list is in t1
        move $a0, $v0
        #jal printLinkedList
        #li $v0, 1
        #move $a0, $v1
        #syscall
    dupeStart:
        move $a0, $t0
        move $a1, $t1
        jal duplicateLinkedList
    sortStart:
        move $a0, $v0
        move $a1, $t1
        jal sortLinkedList
    weAreDone:
        move $t2, $v0
        li $v0, 4
        la $a0, Orig
        syscall
        move $a0, $t0
        jal printLinkedList
        li $v0, 4
        la $a0, SortedLi
        syscall
        move $a0, $t2
        jal printLinkedList
        j main
sortLinkedList:
    addi    $sp, $sp, -32
    sw    $s0, 28($sp)
    sw    $s1, 24($sp)
    sw    $s2, 20($sp)
    sw    $s3, 16($sp)
    sw    $s4, 12($sp)
    sw    $s5, 8($sp)
    sw    $s6, 4($sp)
    sw    $ra, 0($sp)
    move $s0, $a0
    move $s1, $a1
    move $s6, $a1
    li $v0, 9
    li $a0, 8
    syscall
    move $s2, $v0     # always head
    move $s3, $v0    # always end
   
    loopSort:
        beq $s1, 0, sortingDone
        #li $v0, 4
        #la $a0, seper
        #syscall
        #move $a0, $s0
        #jal printLinkedList
        move $a0, $s0
        jal findMin
        move $s4, $v0    # current min
        sw $s4, 4($s3)    # value in a node
        move $a0, $s0
        move $a1, $s4
        jal DeleteValue
        move $s0, $v0
        li $v0, 9
        li $a0, 8
        syscall
        sw $v0, 0($s3)
        move $s3, $v0
        addi $s1, $s1, -1
        j loopSort
sortingDone:
    li $s6, 0
    sw $s6, -8($s3)
    move $v0, $s2
    lw    $ra, 0($sp)
    lw    $s6, 4($sp)
    lw    $s5, 8($sp)
    lw    $s4, 12($sp)
    lw    $s3, 16($sp)
    lw    $s2, 20($sp)
    lw    $s1, 24($sp)
    lw    $s0, 28($sp)
    addi    $sp, $sp, 32
   
    jr    $ra
       
findMin:
#a0 head of the linked list
#v0 min value of the linkedlist
    addi    $sp, $sp, -20
    sw    $s0, 16($sp)
    sw    $s1, 12($sp)
    sw    $s2, 8($sp)
    sw    $s3, 4($sp)
    sw    $ra, 0($sp)
    move $s0, $a0    # head of the linkedlist
    lw $s2, 4($s0)
    lw $s3, 4($s0)
    loop:
        blt $s2, $s3, changeMin     #08-4, 16-3,00-7
    cont:
        beq $s0, 0, found
        lw $s2, 4($s0)    # cur data
        lw $s1, 0($s0)    # next address        # head = head -> next
        move $s0, $s1
        j loop
    changeMin:
        move $s3, $s2    # new min
        j cont
    found:
        move $v0, $s3
        lw    $ra, 0($sp)
        lw    $s3, 4($sp)
        lw    $s2, 8($sp)
        lw    $s1, 12($sp)
        lw    $s0, 16($sp)
        addi    $sp, $sp, 20
        jr    $ra
   
generateLinkedList:
    #s0 is the current integer
    #s1 is the head of the linkedlist
    #s2 is the end of the linkedlist
    #s3 is the size of the linkedlist
    addi    $sp, $sp, -20
    sw    $s0, 16($sp)
    sw    $s1, 12($sp)
    sw    $s2, 8($sp)
    sw    $s3, 4($sp)
    sw    $ra, 0($sp)     # Save $ra just in case we may want to call a subprogram
    li $v0, 5
    syscall
    move $s0, $v0     #  current integer content stored at $s0
    beq $s0, -32767, allDone
    li $a0, 8     # node allocation
    li $v0, 9
    syscall
    li $s3, 0
    move $s1, $v0    # head
    move $s2, $s1    # end
    addi $s3, $s3, 1    # size++
    sw $s0, 4($s2)    # data is stored at
   
addNode:
    li $v0, 5
    syscall
    move $s0, $v0     #  current integer content stored at $s0
    beq $s0, -32767, allDone
    li $a0, 8     # node allocation
    li $v0, 9
    syscall
    sw $v0, 0($s2)
    move    $s2, $v0    # $s2 now points to the new node.
    sw $s0, 4($s2)
    addi $s3, $s3, 1
    j addNode
   
allDone:
    sw    $zero, 0($s2)
    move    $v0, $s1    # Now $v0 points to the list head ($s3).
    move      $v1, $s3 # Now $v1 is the size
    lw    $ra, 0($sp)
    lw    $s3, 4($sp)
    lw    $s2, 8($sp)
    lw    $s1, 12($sp)
    lw    $s0, 16($sp)
    addi    $sp, $sp, 20
   
    jr    $ra
   
duplicateLinkedList:
#a0 head of list1
#a1 size of the list
#v0 new head of the duplicated list
    addi    $sp, $sp, -24
    sw    $s0, 20($sp)
    sw    $s1, 16($sp)
    sw    $s2, 12($sp)
    sw    $s3, 8($sp)
    sw    $s4, 4($sp)
    sw    $ra, 0($sp)
    move $s0, $a0
    move $s1, $a1
    beq $s1, 0, dupeDone
    li $v0, 9
    li $a0, 8
    syscall        #generate head
    move $s2, $v0    # head is in s2
    move $s3, $s2    # end is in s3
    lw $s4, 4($s0)
    sw $s4, 4($s3)
    addi $s1, $s1, -1
    addi $s0, $s0, 8
   
keepAdding:
    beq $s1, 0, dupeDone
    li $v0, 9
    li $a0, 8
    syscall
    sw $v0, 0($s3)
    move $s3, $v0
    lw $s4, 4($s0)
    sw $s4, 4($s3)
    addi $s1, $s1, -1
    addi $s0, $s0, 8
    j keepAdding  

dupeDone:
    move $v0, $s2
    lw    $ra, 0($sp)
    lw    $s4, 4($sp)
    lw    $s3, 8($sp)
    lw    $s2, 12($sp)
    lw    $s1, 16($sp)
    lw    $s0, 20($sp)
    addi    $sp, $sp, 24
   
    jr    $ra
   
DeleteValue:
#a0 head of the linkedlist
#a1 the value that is gonna be deleted from the linkedlist
    addi $sp, $sp, -20
    sw $s0, 0($sp)
    sw $s1, 4($sp)
    sw $s2, 8($sp)
    sw $s3, 12($sp)
    sw $s4, 16($sp)
    li $v0, -1
   
    # check if head valid
    beq $a0, $zero, headIsNull
    j headIsNotNull  
headIsNull:
    j done
headIsNotNull:  
    move $v1, $a0
   
    move $s0, $a0 # previous
    move $s1, $a0 # current
    lw $s4, 4($a0)
    beq $s4, $a1, removeHead
   
deleteLoop:    beq $s1, $zero, done
    lw $s2, 4($s1) # cur value  
    # if statement
    beq $s2, $a1, remove
    move $s0, $s1 # traverse
    lw $s1, 0($s1) # traverse
    j deleteLoop
remove:
    beq $s1, $a0, removeHead
    lw $s3, 0($s1)
    sw $s3, 0($s0)
    lw $s1, 0($s1)
    j done
removeHead:
    lw $s4, 0($a0)
    move $a0, $s4
done:  
    move $v0, $a0
    lw $s0, 0($sp)
    lw $s1, 4($sp)
    lw $s2, 8($sp)
    lw $s3, 12($sp)
    lw $s4, 16($sp)
    addi $sp, $sp, 20

    jr $ra

   
printLinkedList:        # taken from the example asm code in moodle lab03
# Print linked list nodes in the following format
# --------------------------------------
# Node No: xxxx (dec)
# Address of Current Node: xxxx (hex)
# Address of Next Node: xxxx (hex)
# Data Value of Current Node: xxx (dec)
# --------------------------------------

# Save $s registers used
    addi    $sp, $sp, -20
    sw    $s0, 16($sp)
    sw    $s1, 12($sp)
    sw    $s2, 8($sp)
    sw    $s3, 4($sp)
    sw    $ra, 0($sp)     # Save $ra just in case we may want to call a subprogram

# $a0: points to the linked list.
# $s0: Address of current
# s1: Address of next
# $2: Data of current
# $s3: Node counter: 1, 2, ...
    move $s0, $a0    # $s0: points to the current node.
    li   $s3, 0
    printNextNode:
        beq    $s0, $zero, printedAll
                # $s0: Address of current node
        lw    $s1, 0($s0)    # $s1: Address of  next node
        lw    $s2, 4($s0)    # $s2: Data of current node
        addi    $s3, $s3, 1
    # $s0: address of current node: print in hex.
    # $s1: address of next node: print in hex.
    # $s2: data field value of current node: print in decimal.
        la    $a0, line
        li    $v0, 4
        syscall        # Print line seperator
   
        la    $a0, nodeNumberLabel
        li    $v0, 4
        syscall
       
        move    $a0, $s3    # $s3: Node number (position) of current node
        li    $v0, 1
        syscall
   
        la    $a0, addressOfCurrentNodeLabel
        li    $v0, 4
        syscall
   
        move    $a0, $s0    # $s0: Address of current node
        li    $v0, 34
        syscall

        la    $a0, addressOfNextNodeLabel
        li    $v0, 4
        syscall
        move    $a0, $s1    # $s0: Address of next node
        li    $v0, 34
        syscall  
   
        la    $a0, dataValueOfCurrentNode
        li    $v0, 4
        syscall
       
        move    $a0, $s2    # $s2: Data of current node
        li    $v0, 1      
        syscall  

    # Now consider next node.
        move    $s0, $s1    # Consider next node.
        j    printNextNode
    printedAll:
    # Restore the register values
        lw    $ra, 0($sp)
        lw    $s3, 4($sp)
        lw    $s2, 8($sp)
        lw    $s1, 12($sp)
        lw    $s0, 16($sp)
        addi    $sp, $sp, 20
        jr    $ra

exit:
    li $v0, 10
    syscall

.data
menu: .asciiz "\n1-)Generate Linked List after that display original and sorted list\n2-)Exit\n"
line:  
    .asciiz "\n --------------------------------------"
Orig: .asciiz "\nOriginal List is being displayed:"
SortedLi: .asciiz "\nSorted List is being displayed:"
nodeNumberLabel:
    .asciiz    "\n Node No.: "
addressOfCurrentNodeLabel:
    .asciiz    "\n Address of Current Node: "
   
addressOfNextNodeLabel:
    .asciiz    "\n Address of Next Node: "
   
dataValueOfCurrentNode:
    .asciiz    "\n Data Value of Current Node: "
enterIntegers: .asciiz "Please enter the integers one by one (-32767 to stop): \n"

Merhabalar, @356463 @One Face @oynozan hocalarımın yardımıyla kodlamaya başladım ama bu algoirtmayı optimize edemiyorum. Yardımcı olur musunuz? Sizin için kolaydır, sonuçta tecrübelisiniz.
 

Yeni konular

Geri
Yukarı