Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- #############################
- datalen:
- .word 0x0010 # 16
- data:
- .word 0xffff7e81
- .word 0x00000001
- .word 0x00000002
- .word 0xffff0001
- .word 0x00000000
- .word 0x00000001
- .word 0xffffffff
- .word 0x00000000
- .word 0xe3456687
- .word 0xa001aa88
- .word 0xf0e159ea
- .word 0x9152137b
- .word 0xaab385a1
- .word 0x31093c54
- .word 0x42102f37
- .word 0x00ee655b
- #########################
- newline: .asciiz "\n"
- .text
- main:
- jal printresult #Print
- nop
- lw $a0, datalen
- la $a1, data
- jal insertionsort #Call the Sort
- nop
- jal printresult #Print
- nop
- li $v0, 10 # Prepare syscall to exit program cleanly
- syscall # Bye!
- printresult:
- #This subroutine prints the contents of array2
- lw $t1, datalen
- la $t0, data #Create pointer to array2 in $t0
- prloop: lw $a0, 0($t0) #Load value that $t0 points to in $a0
- beqz $t1, prexit #Go to prexit if $t1 is equal to 0
- nop
- addi $t1, $t1,-1
- li $v0, 1 #Prepare "print int" syscall (prints value in $a0)
- syscall
- la $a0, newline
- li $v0, 4 #Prepare "print string syscall (prints string pointed to by $a0)
- syscall
- addi $t0, $t0, 4 #Increase pointer by 4 to point to next word in array
- j prloop
- nop
- prexit:
- la $a0, newline
- li $v0, 4 #Prepare "print string syscall (prints string pointed to by $a0)
- syscall
- jr $ra
- nop
- insertionsort:
- #a1 >> Adress to (Array of Words)
- #a0 >> Size of array
- add $s0, $zero, 1 #start at S0 = 1 #Starts at 1
- iloop: #Loop
- beq $a0,$s0, iexit # Check if s0 == a0 (max of array)
- nop
- ##
- #Get a1[s0]
- sll $t0,$s0, 2 # s0 * 4 index of loop
- add $s2,$a1,$t0 # add index offset to array adress = adress to (a1[s0])
- lw $s3,0($s2) #get a1[s0]
- #s2 = adress to s3
- add $s1,$s0,-1
- iloop2: #Loop2
- #Condition 1 "Standard loop"
- blt $s1,-1,iexit2 # if (s1 < 0) then Exit (had problem)
- nop
- ## GET 1 word from array
- #
- sll $t0,$s1, 2 # s1 * 4 index of loop
- add $s4,$a1,$t0 # add index offset to array adress = adress to (a1[s1])
- # s4 = adress of a1[s1]
- lw $t1,0($s4) #get a1[s4]
- ##
- #Condition 2
- blt $t1,$s3,iexit2 #if (a1[s4] < a1[s0]) then Exit
- nop
- ##
- #Push
- sw $t1,4($s4) #Store a1[s1] in a1[s1 + 1]
- #
- #Loop2
- subi $s1,$s1, 1 #s1--;
- j iloop2
- nop
- ##
- iexit2:
- sw $s3,4($s4) #Insert Word
- #Loop
- addi $s0, $s0, 1 # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
- j iloop
- nop
- ##
- iexit: jr $ra
- nop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement