Advertisement
Guest User

Untitled

a guest
Apr 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .data
  2.     #############################
  3. datalen:
  4.     .word   0x0010  # 16
  5. data:
  6.     .word   0xffff7e81
  7.     .word   0x00000001
  8.     .word   0x00000002
  9.     .word   0xffff0001
  10.     .word   0x00000000
  11.     .word   0x00000001
  12.     .word   0xffffffff
  13.     .word   0x00000000
  14.     .word   0xe3456687
  15.     .word   0xa001aa88
  16.     .word   0xf0e159ea
  17.     .word   0x9152137b
  18.     .word   0xaab385a1
  19.     .word   0x31093c54
  20.     .word   0x42102f37
  21.     .word   0x00ee655b
  22.    
  23.     #########################
  24.     newline: .asciiz    "\n"
  25. .text
  26.  
  27.  
  28. main:
  29.  
  30.     jal printresult #Print
  31.     nop
  32.  
  33.     lw  $a0, datalen
  34.     la  $a1, data      
  35.  
  36.     jal insertionsort   #Call the Sort
  37.     nop
  38.  
  39.     jal printresult #Print
  40.     nop
  41.    
  42.     li  $v0, 10 # Prepare syscall to exit program cleanly
  43.     syscall     # Bye!
  44.  
  45.  
  46.  
  47.  
  48. printresult:
  49.     #This subroutine prints the contents of array2
  50.    
  51.     lw  $t1, datalen
  52.     la  $t0, data   #Create pointer to array2 in $t0
  53.    
  54.    
  55. prloop: lw  $a0, 0($t0) #Load value that $t0 points to in $a0
  56.  
  57.     beqz    $t1, prexit #Go to prexit if $t1 is equal to 0
  58.     nop
  59.    
  60.     addi    $t1, $t1,-1
  61.    
  62.     li  $v0, 1      #Prepare "print int" syscall (prints value in $a0)
  63.     syscall
  64.     la  $a0, newline
  65.     li  $v0, 4      #Prepare "print string syscall (prints string pointed to by $a0)
  66.     syscall
  67.    
  68.    
  69.     addi    $t0, $t0, 4 #Increase pointer by 4 to point to next word in array
  70.     j   prloop
  71.     nop
  72.    
  73. prexit:
  74.     la  $a0, newline
  75.     li  $v0, 4      #Prepare "print string syscall (prints string pointed to by $a0)
  76.     syscall
  77.  
  78.     jr  $ra
  79.     nop
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.    
  96. insertionsort:
  97.    
  98.     #a1 >> Adress to (Array of Words)
  99.     #a0 >> Size of array
  100.                    
  101.     add $s0, $zero, 1   #start at S0 = 1 #Starts at 1
  102.    
  103. iloop:  #Loop
  104.     beq $a0,$s0, iexit  # Check if s0 == a0 (max of array)
  105.     nop
  106.     ##
  107.     #Get a1[s0]
  108.     sll     $t0,$s0, 2  # s0 * 4 index of loop
  109.     add $s2,$a1,$t0     # add index offset to array adress = adress to (a1[s0])
  110.     lw  $s3,0($s2)  #get a1[s0]
  111.     #s2 = adress to s3
  112.     add $s1,$s0,-1
  113.    
  114.    
  115. iloop2: #Loop2
  116.     #Condition 1    "Standard loop"
  117.     blt $s1,-1,iexit2   # if (s1 < 0) then Exit (had problem)
  118.     nop
  119.     ##  GET 1 word from array
  120.     #
  121.     sll     $t0,$s1, 2  # s1 * 4 index of loop
  122.     add $s4,$a1,$t0     # add index offset to array adress = adress to (a1[s1])
  123.     #   s4 = adress of a1[s1]
  124.     lw  $t1,0($s4)  #get a1[s4]
  125.     ##
  126.     #Condition 2
  127.     blt $t1,$s3,iexit2  #if (a1[s4] < a1[s0]) then Exit
  128.     nop
  129.     ##
  130.    
  131.     #Push  
  132.     sw  $t1,4($s4)  #Store a1[s1] in a1[s1 + 1]
  133.     #
  134.    
  135.     #Loop2
  136.     subi    $s1,$s1, 1  #s1--;
  137.     j   iloop2
  138.     nop
  139.     ##
  140.    
  141. iexit2:
  142.  
  143.     sw  $s3,4($s4)  #Insert Word
  144.        
  145.     #Loop
  146.     addi    $s0, $s0, 1 # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  147.     j   iloop
  148.     nop
  149.     ##
  150.    
  151. iexit:  jr  $ra
  152.     nop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement