Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. .data
  2.  
  3. enter_n: .asciiz "Enter N: "
  4. array_msg: .asciiz "Board:\n"
  5. newline: .asciiz "\n"
  6. space: .asciiz " "
  7. .align 2
  8. array: .space 100
  9. .text
  10.  
  11. la $s7, array
  12.  
  13.  
  14. #cout enter N
  15. li $v0, 4
  16. la $a0, enter_n
  17. syscall
  18. #take n as input
  19. li $v0, 5
  20. syscall
  21. move $t0, $v0 #$t0 = n
  22. move $t1, $t0 #$t1 = n
  23. move $t8, $v0 #$t8 = n
  24. #calculate done which is 2^n - 1
  25. li $t2, 2
  26. li $t3, 1
  27. calculate_done:
  28. mult $t3, $t2
  29. mflo $t3
  30. sub $t1, $t1, 1
  31. bne $t1, 0, calculate_done
  32.  
  33. mult $t0, $t0
  34. mflo $t1 #$t1 = $t0 * $t0
  35.  
  36. #$t3 = done = 2^n - 1
  37. sub $t3, $3, 1
  38.  
  39. #Now Initilaize ld, rd, col = 0
  40. li $s0, 0 #$s0 = ld
  41. li $s1, 0 #s1 = col
  42. li $s2, 0 #s2 = rd
  43.  
  44. li $t5, 0 #$t5 = row
  45. li $t6, 0 #$t6 = column
  46.  
  47. innerRecurse:
  48. beq $s1, $t3, allocate_queens #if col = done
  49. #poss = $s3
  50. or $s3, $s0, $s2
  51. or $s3, $s3, $s1
  52. xor $s3, $s3, 1
  53.  
  54. and $s4, $s3, $t3 #poss & done
  55. beq $s4, 1, while
  56. while:
  57. #bit = s6
  58. #-poss = s5
  59. sub $s5, $s3, $zero
  60. and $s6, $s3, $s5 #var bit = poss $ -poss
  61. sub $s3, $s3, $s6
  62. #ld | bit >> 21
  63. or $s0, $s0, $s6
  64. srl $s0, $s0, 1
  65. #col | bit
  66. or $s1, $s1, $s6
  67. #rd | bit << 1
  68. or $s2, $s2, $s6
  69. sll $s2, $s2, 1
  70.  
  71. #calculate and column
  72. #Reset row
  73. mult $t1, $t5 #t1 = n*n | #t5 = row
  74. mflo $t7
  75. add $s7, $s7, $t7
  76.  
  77. set_row:
  78. sw $zero, ($s7)
  79. add $s7, $s7, 4
  80. sub $t8, $t8, 1
  81. bnez $t8, set_row
  82.  
  83. la $s7, array
  84. li $t2, 0
  85. move $t9, $s1 #s1 = col
  86. shift_col:
  87. add $t2, $t2, 1
  88. srl $t9, $t9, 1
  89. bnez $t9, shift_col
  90. #subtract from n then $t2 = col
  91. sub $t2, $t0, $t2 #t2 = col
  92. mult $t5, $t0 #i * cols
  93. mflo $a1
  94. add $a1, $a1, $t2
  95. mult $a1, $t0
  96. mflo $a1
  97. li $a3, 1
  98. sw $a3, array($a1)
  99. #increment row
  100. add $t5, $t5, 1
  101. move $t8, $t0
  102. jal innerRecurse
  103.  
  104. and $s4, $s3, $t3 #poss & done
  105. beq $s4, 1, while
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. allocate_queens:
  115. j print_array_adjust
  116.  
  117. print_array_adjust:
  118. li $v0, 4
  119. la $a0, array_msg
  120. syscall
  121. mult $t0, $t0
  122. mflo $t3 #$t3 = n * n (counter)
  123. la $t5, array
  124. li $t4, 0
  125. move $t6, $t0
  126. j print_array
  127. print_array:
  128. li $v0, 1
  129. lw $a0, ($t5)
  130. syscall
  131. li $v0, 4
  132. la $a0, space
  133. syscall
  134. add $t4, $t4, 1
  135. beq $t4, $t6, new_line
  136. j continue
  137. new_line:
  138. li $v0, 4
  139. la $a0, newline
  140. syscall
  141. li $t4, 0
  142. continue:
  143. add $t5, $t5, 4
  144. sub $t3, $t3, 1
  145. bnez $t3, print_array
  146.  
  147.  
  148.  
  149. exit:
  150. li $v0, 10
  151. syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement