Advertisement
Duoshot

gormansa3

Oct 24th, 2013
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. include(macro_defs.m)
  2.  
  3. define(SIZE,40)
  4. define(i_r,l0)
  5. define(j_r,l1)
  6. define(tmp_r,l2)
  7.  
  8. local_var
  9. var(v_s,4,4*SIZE)
  10.  
  11.  
  12. fma: .asciz "v[%d] = %d\n"
  13. .align 4
  14. fmb: .asciz "Random Array\n"
  15. .align 4
  16. fmc: .asciz "\nSorted Array\n"
  17. .align 4
  18.  
  19. begin_main
  20.  
  21. mov 0, %i_r !i = 0
  22.  
  23.  
  24. mov SIZE, %l3
  25.  
  26.  
  27. set fmb, %o0 !print "Random Array"
  28. call printf
  29. nop
  30.  
  31. fill_array:
  32.  
  33. cmp %i_r, SIZE !compare i and size
  34. bge next !branch to next if i>=size
  35. nop
  36.  
  37. call rand !random number generator
  38. nop
  39. and %o0, 0xff, %l7 !0-256. Random number stored in %l7
  40.  
  41. sll %i_r, 2, %l6 !the traversing thing from tutorial
  42. add %fp, %l6, %l6
  43. st %l7, [%l6+v_s] !store random number in stack
  44.  
  45. set fma, %o0 !prepare to print
  46. mov %i_r, %o1 !move i
  47. mov %l7, %o2 !move value if j[i]
  48. call printf !print
  49. nop
  50.  
  51.  
  52. add %i_r, 1, %i_r !increment i
  53.  
  54. ba fill_array !branch to fill_array
  55. nop
  56.  
  57. next:
  58. mov 1, %i_r !set i = 1
  59.  
  60. mov %i_r, %j_r !set j = i
  61.  
  62.  
  63. outer_loop:
  64.  
  65. cmp %i_r, %l3 !check if i < SIZE
  66. bge print !branch to printing the array if i >= SIZE
  67. nop
  68.  
  69. sll %i_r, 2, %l6 !traverse thing from tutorial
  70. add %fp, %l6, %l6
  71. ld [%l6+v_s], %tmp_r !set tmp = v[i]
  72.  
  73.  
  74. inner_loop:
  75. cmp %j_r, 0 !check if j > 0
  76.  
  77. ble outer_end !if j <= 0, skip to the end of the outer loop
  78. nop
  79. sub %j_r, 1, %l5 !get j-1
  80.  
  81. sll %l5, 2, %l6 !traverse thing from tutorial
  82. add %fp, %l6, %l6
  83. ld [%l6+v_s], %l4 !%l4 = v[j-1]
  84.  
  85.  
  86. cmp %tmp_r, %l4 !check if tmp < v[j-1]
  87. bge outer_end !go to end of outer loop if tmp >= v[j-1]
  88. nop
  89.  
  90. sll %j_r, 2, %l6 !traverse thing from tutorial
  91. add %fp, %l6, %l6
  92. st %l4, [%l6+v_s] !v[j] = v[j-1]
  93.  
  94. sub %j_r, 1, %j_r !decrement j
  95.  
  96. ba inner_loop
  97. nop
  98.  
  99. outer_end:
  100.  
  101.  
  102.  
  103. sll %j_r, 2, %l6 !traverse thing from tutorial
  104. add %fp, %l6, %l6
  105. st %tmp_r, [%l6+v_s] !v[j] = tmp
  106.  
  107. add %i_r, 1, %i_r !increase i by 1
  108.  
  109. mov %i_r, %j_r !set j=i
  110.  
  111. ba outer_loop
  112. nop
  113.  
  114.  
  115. print:
  116. mov 0, %i_r !i = 0
  117. set fmc, %o0 !set the string
  118. call printf !print out the label "Sorted Array"
  119.  
  120.  
  121.  
  122.  
  123. print_sorted:
  124.  
  125. cmp %i_r, %l3 !check if i < SIZE
  126. bge end !branch to end if i >=size
  127. nop
  128.  
  129. sll %i_r, 2, %l7 !%g2 = i * 4 (first loop instruction)
  130. add %fp, %l7, %l7 !%g2 = %fp + (i * 4)
  131. ld [%l7+v_s], %g2 !load v[i] into %g1
  132.  
  133. set fma, %o0 !set the correct string format
  134.  
  135. mov %i_r, %o1 !move i to %o1 for printing
  136. mov %g2, %o2 !move v[i] to %o2 for printing
  137. call printf !print v[i]
  138. nop
  139.  
  140. inc %i_r !i++
  141.  
  142. ba print_sorted
  143. nop
  144. end:
  145. end_main !end program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement