.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 # insert: # # Function insert(a[], length, elem, i)# # # # pointers: #$a0 = *a[] #$a1 = *length #$a2 = *elem li $a2,99 #$a3 = *i li $a3,2 #$ra = *return address # # 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 andi $t0, 0 # zet 0 in $t0 subi $t0, 1 # zet -1 in $t0 move $t1, $a1 #$t0 = low = -1 #