Advertisement
Duoshot

Untitled

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