Guest User

Untitled

a guest
Apr 29th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.     .data
  3.     .align  0       # string data doesn't have to be aligned
  4. space:  .asciiz " "
  5. lf: .asciiz "\n"
  6. before: .asciiz "Before: "
  7. after:  .asciiz "After: "
  8. prompt: .asciiz "Enter Number: "
  9.  
  10.     .align  2       # word data must be on word boundaries
  11. array:  .word   -1,-2,-5,-6,-9,-0,-7,-8,-3,-4
  12.  
  13. SIZE    = 10            # number of array elements
  14. PRINT_STRING = 4            # arg for syscall to tell it to write
  15. PRINT_INT = 1
  16.  
  17.     .text           # this is program code
  18.     .align  2       # instructions must be on word boundaries
  19.  
  20.     .globl  main        # main is a global label
  21.  
  22. #
  23. # EXECUTION BEGINS HERE
  24. #
  25. main:
  26.     addi    $sp,$sp,-4  # allocate space for the return address
  27.     sw  $ra,0($sp)  # store the ra on the stack
  28.  
  29.     la  $a0,array   # set up to read in an array of SIZE ints
  30.     li  $a1,SIZE    # and put them at label array
  31.     jal readarray
  32.  
  33.     li  $v0,PRINT_STRING    # print a "Before:"
  34.     la  $a0,before
  35.     syscall
  36.  
  37.     la  $a0,array   # print out the original array
  38.     li  $a1,SIZE
  39.     jal parray
  40.  
  41.         la      $a0, array
  42.         li      $a1, SIZE
  43.         jal     sort
  44.  
  45.     li  $v0,PRINT_STRING        # print "After: "
  46.     la  $a0,after
  47.     syscall
  48.  
  49.     la  $a0,array   # print the array again (now sorted)
  50.     li  $a1,SIZE
  51.     jal parray
  52.  
  53.     lw  $ra,0($sp)  # restore the ra
  54.     addi    $sp,$sp,-4  # deallocate stack space
  55.     jr  $ra     # return from main and exit spim
  56.  
  57.  
  58. parray:
  59.     li  $t0,0       # i=0;
  60.     move    $t1,$a0     # t1 is pointer to array
  61. pa_loop:
  62.     beq $t0,$a1,done    # done if i==n
  63.  
  64.     lw  $a0,0($t1)  # get a[i]
  65.     li  $v0,PRINT_INT
  66.     syscall         # print one int
  67.  
  68.     li  $v0,PRINT_STRING
  69.     la  $a0,space
  70.     syscall         # print a space
  71.  
  72.     addi    $t1,$t1,4   # update pointer
  73.     addi    $t0,$t0,1   # and count
  74.     j   pa_loop
  75. done:
  76.     li  $v0,PRINT_STRING
  77.     la  $a0,lf
  78.     syscall         # print a newline
  79.  
  80.     jr  $ra
  81.  
  82.  
  83. readarray:
  84.     li  $t0,0       # i=0;
  85.     move    $t1,$a0     # t1 is pointer to array
  86. ra_loop:
  87.     beq $t0,$a1,ra_done # done if i==n
  88.  
  89.     li  $v0,PRINT_STRING
  90.     la  $a0,prompt
  91.     syscall         # print one int
  92.  
  93.     li  $v0,5
  94.     syscall
  95.     sw  $v0,0($t1)
  96.  
  97.     addi    $t1,$t1,4   # update pointer
  98.     addi    $t0,$t0,1   # and count
  99.     j   ra_loop
  100. ra_done:
  101.     li  $v0,PRINT_STRING
  102.     la  $a0,lf
  103.     syscall         # print a message
  104.  
  105.     jr  $ra
  106.  
  107. sort:
  108.     addi    $sp,$sp,-20 # set up our stack frame
  109.     sw  $ra, 16($sp)    # save return address
  110.     sw  $s3, 12($sp)    # save s0 through s3
  111.     sw  $s2, 8($sp)
  112.     sw  $s1, 4($sp)
  113.     sw  $s0, 0($sp)
  114.  
  115.     move    $s2,$a0     # s2 points to the array element
  116.     move    $s3,$a1     # s3 is the length
  117.  
  118.  
  119.     move    $s0,$zero   # s0 is the outer loop control variable
  120. for1tst:
  121.     slt $t0,$s0,$s3 # have we reached the end?
  122.     beq $t0,$zero,exit1 # yes - leave
  123.  
  124.  
  125.     addi    $s1,$s0,-1  # s1 is the inner loop control variable
  126. for2tst:
  127.     slti    $t0,$s1,0   # j >= 0?
  128.     bne $t0,$zero,exit2 # no - leave the inner loop
  129.  
  130.     add $t1,$s1,$s1 # scale the loop control variable
  131.     add $t1,$t1,$t1 # t1 = s1 * 4
  132.     add $t2,$s2,$t1 # t2 now points to v[j]
  133.  
  134.     lw  $t3,0($t2)  # get v[j]
  135.     lw  $t2,4($t2)  # get v[j+1]
  136.     slt $t0,$t2,$t3 # v[j] < v[j+1]?
  137.     beq $t0,$zero,exit2 # yes - leave the inner loop
  138.  
  139.     move    $a0,$s2     # we found a pair of entries to swap
  140.     move    $a1,$s1
  141.     jal swap
  142.  
  143.     addi    $s1,$s1,-1
  144.     j   for2tst
  145. exit2:
  146.  
  147.     addi    $s0,$s0,1
  148.     j   for1tst
  149. exit1:
  150.  
  151.     lw  $s0, 0($sp) # restore our saved registers
  152.     lw  $s1, 4($sp)
  153.     lw  $s2, 8($sp)
  154.     lw  $s3, 12($sp)
  155.     lw  $ra, 16($sp)    # and return address
  156.     addi    $sp,$sp,20  # destroy the stack frame
  157.     jr  $ra     # and return to the caller
  158.        
  159. swap:
  160.         sll     $t1, $a1, 2
  161.         add     $t1, $a0, $t1
  162.         lw      $t0, 0($t1)
  163.         lw      $t2, 4($t1)
  164.         sw      $t2, 0($t1)
  165.         sw      $t0, 4($t1)
  166.         jr      $ra
Add Comment
Please, Sign In to add comment