.data str1: .asciiz "Insert the array size \n" str2: .asciiz "Insert the array elements,one per line \n" str3: .asciiz "The sorted array is : \n" str5: .asciiz "\n" .text main: la $a0,str1 # Print of str1 li $v0,4 # syscall # li $v0,5 # Get the array size(n) and syscall # and put it in $v0 move $s2,$v0 # $s2=n sll $s0,$v0,2 # $s0=n*4 sub $sp,$sp,$s0 # This instruction creates a stack frame # large enough to contain the array la $a0,str2 # li $v0,4 # Print of str2 syscall # move $s1,$zero # i=0 for_get: bge $s1,$s2,exit_get # if i>=n go to exit_for_get sll $t0,$s1,2 # $t0=i*4 add $t1,$t0,$sp # $t1=$sp+i*4 li $v0,5 # Get one element of the array syscall # sw $v0,0($t1) # The element is stored # at the address $t1 la $a0,str5 li $v0,4 syscall addi $s1,$s1,1 # i=i+1 j for_get exit_get: move $a0,$sp # $a0=base address af the array move $a1,$s2 # $a1=size of the array jal insert # isort(a,n) # In this moment the array has been # sorted and is in the stack frame la $a0,str3 # Print of str3 li $v0,4 syscall move $s1,$zero # i=0 for_print: bge $s1,$s2,exit_print # if i>=n go to exit_print sll $t0,$s1,2 # $t0=i*4 add $t1,$sp,$t0 # $t1=address of a[i] lw $a0,0($t1) # li $v0,1 # print of the element a[i] syscall # la $a0,str5 li $v0,4 syscall addi $s1,$s1,1 # i=i+1 j for_print exit_print: add $sp,$sp,$s0 # elimination of the stack frame li $v0,10 # EXIT syscall # #; #; Function insert(a[], length, elem, i)# #; #; #; pointers: #;$a0 = *a[] #;$a1 = *length #;$a2 = *elem li $a2,99 #;$a3 = *i li $a3,2 #;$ra = *return address # insert: # Zet 1 in $t3, nodig voor later andi $t3, 0 # 0 in $t3 ori $t3, 1 # 1 in $t3 move $t0, $a1 sub $t0, $t0, $t3 # trek 1 van $t0 af move $t1, $a2 move $t2, $a3 #; $t0 = j #; $t1 = elem #; $t2 = i #; $t3 = 1 # check for conditie insert1: slt $t4, $t0, $t2 # controleer of j kleiner is i beq $t4, $t3, insert2 # zo ja, ga naar label 2 # ga verder als alles klopt sll $t6, $t0, 2 add $t6, $a0, $t6 lw $t4, 0($t6) # zet in $t4 waarde uit array met element $t0 addi $t5, $t0, 1 # zet in $t5 de waarde van j + 1 sll $t6, $t0, 2 # zorg voor 32 bits verschuiving van de index add $t6, $a0, $t6 sw $t4, 0($t6) # stop in array met element $t0 + 1 de waarde uit $t4 sub $t0, $t0, $t3 # trek 1 van $t0 af j insert1 # ga terug naar 1 insert2: sll $t6, $t2, 2 # zorg voor 32 bits verchuiving van de index add $t6, $a0, $t6 # bereken juiste geheugen plek en zet die in $t6 sw $t1, 0($t6) # stop in geheugenplaats {a[] plus $t2} de waarde van $t1 jr $ra # keer terug uit functie #; binarySearch(int a[], int length, int elem); #; $a0 = *a[] #; $a1 = length #; $a2 = elem binarySearch: andi $t0, $t0, 0 # zet 0 in $t0 addi $t0, $t0, -1 # zet -1 in $t0 move $t1, $a1 addi $t3, $zero, 2 #;$t0 = low = -1 #;$t1 = hi <= length #;$t2 = gereserveerd voor "mid" #;$t3 waarde 2 gereserveerd voor divide functie binarySearch0: #;begin while-loop move $t4, $t1 sub $t4, $t4, 1 #; hi - 1 slt $t4, $t0, $t4 # $t3 <= 1 if (low < hi - 1); beq $t4, $zero, binarySearch1 #; zo nee, spring naar binarySearch1 #; ga verder add $t4, $t0, $t1 div $t2, $t4, $t3 # mid <= (low + hi) / 2 add $t4, $a0, $t2 lw $t4, 0($t4) # laad a[mid] in $t4 slt $t4, $t4, $a2 # $t4 <= 1 if (a[mid] < elem) bne $t4, $zero, binarySearch2 move $t1, $t2 # hi <= mid binarySearch2: beq $t4, $zero, binarySearch0 move $t0, $t2 # low <= mid j binarySearch0 binarySearch1: move $v0, $t1 #returnvalue 0 <= waarde hi jr $ra #return to caller insertionSort: isort: #; function insertionSort(int *a[], int length); #; $a0 = *a[] #; $a1 = length #; Actie: sorteert de tabel m.b.v. et Insertion Sort algoritme # #; stop s registers in stack addi $sp, $sp, -24 # ruimte creëren voor opslag s registers #; stop s registers op stack. sw $ra, 0($sp) sw $s0, 4($sp) sw $s1, 8($sp) sw $s2, 12($sp) sw $s3, 16($sp) sw $s4, 20($sp) andi $s0, $s0, 0 #; i = 0 move $s3, $a0 #; *a[] move $s4, $a1 #; length #; vraag geheugenblok voor int b (length bytes) move $a0, $a1 #; length in argument 0 addi $v0, 9 #; sbrk syscall move $s2, $v0 # pointer b # $s0 = i = 0 # $s1 = positie # $s2 = *b[] # $s3 = *a[] # $s4 = length insertionSort0: slt $t0, $s0, $s4 beq $t0, $zero, insertionSort1 add $t0, $s3, $s0 #; maak index voor array a lw $a2, 0($t0) move $a1, $s0 move $a0, $s2 jal binarySearch move $s1, $v0 move $a3, $s1 add $t0, $s3, $s0 #; maak index voor array a lw $a2, 0($t0) move $a1, $s0 move $a0, $s2 jal insert addi $t0, $t0, 1 # i++ j insertionSort0 insertionSort1: andi $s0, $s0, 0 insertionSort2: slt $t0, $s0, $s4 beq $t0, $zero, insertionSort3 add $t0, $s2, $s0 #; maak index voor array b lw $t1, 0($t0) add $t0, $s3, $s0 #; maak index voor array a sw $t1, 0($t0) addi $t0, $t0, 1 # i++ j insertionSort2 insertionSort3: lw $ra, 0($sp) lw $s0, 4($sp) lw $s1, 8($sp) lw $s2, 12($sp) lw $s3, 16($sp) lw $s4, 20($sp) addi $sp, $sp, 24 # ruimte creëren voor b jr $ra