Advertisement
Guest User

Untitled

a guest
Sep 9th, 2017
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.31 KB | None | 0 0
  1. # board.s ... Game of Life on a 10x10 grid
  2.  
  3. .data
  4.  
  5. N: .word 10 # gives board dimensions
  6.  
  7. board:
  8. .byte 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
  9. .byte 1, 1, 0, 0, 0, 0, 0, 0, 0, 0
  10. .byte 0, 0, 0, 1, 0, 0, 0, 0, 0, 0
  11. .byte 0, 0, 1, 0, 1, 0, 0, 0, 0, 0
  12. .byte 0, 0, 0, 0, 1, 0, 0, 0, 0, 0
  13. .byte 0, 0, 0, 0, 1, 1, 1, 0, 0, 0
  14. .byte 0, 0, 0, 1, 0, 0, 1, 0, 0, 0
  15. .byte 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
  16. .byte 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
  17. .byte 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
  18.  
  19. newboard: .space 100
  20. # prog.s ... Game of Life on a NxN grid
  21. #
  22. # Needs to be combined with board.s
  23. # The value of N and the board data
  24. # structures come from board.s
  25. #
  26. # Written by <<YOU>>, August 2017
  27. .data
  28.  
  29. number_1:
  30. .word 1
  31.  
  32. main_ret_save:
  33. .space 4
  34.  
  35. print_scan_msg1:
  36. .asciiz "# Iterations: "
  37.  
  38. true:
  39. .word 1
  40.  
  41.  
  42. eol:
  43. .asciiz "\n"
  44.  
  45. msg_hash:
  46. .asciiz "#",
  47.  
  48. msg_dot:
  49. .asciiz "."
  50.  
  51. msg_result_1:
  52. .asciiz "=== After iteration "
  53.  
  54. msg_result_2:
  55. .asciiz " ==="
  56.  
  57. .text
  58. .globl main
  59.  
  60. ################################################################
  61. #
  62. # int maxiters;
  63. # printf("# Iterations: ");
  64. # scanf("%d", &maxiters);
  65. # for (int n = 1; n <= maxiters; n++) {
  66. # for (int i = 0; i < N; i++) {
  67. # for (int j = 0; j < N; j++) {
  68. # int nn = neighbours(i,j);
  69. # if (board[i][j] == 1) {
  70. # if (nn < 2)
  71. # newboard[i][j] = 0;
  72. # else if (nn ==2 || nn == 3)
  73. # newboard[i][j] = 1;
  74. # else
  75. # newboard[i][j] = 0;
  76. # }
  77. # else if (nn == 3)
  78. # newboard[i][j] = 1;
  79. # else
  80. # newboard[i][j] = 0;
  81. # }
  82. # }
  83. # printf("=== After iteration %d ===\n", n);
  84. # copyBackAndShow();
  85. # }
  86. # return 0;
  87. #}
  88. ################################################################
  89.  
  90.  
  91. main:
  92. sw $ra, main_ret_save
  93.  
  94. la $a0, print_scan_msg1 # printf("# Iterations: ");
  95. li $v0, 4
  96. syscall
  97.  
  98. li $v0, 5 # scanf("%d", &maxiters);
  99. syscall
  100. move $s0, $v0 #Now s0 has the value of maxiters
  101.  
  102. li $s1, 1 #n = 1
  103. lw $s7, N #s7 = N
  104. lw $s6, true #s6 = 1
  105.  
  106. for_loop1:
  107. bgt $s1 , $s0, end_main #maxiters > n, branch
  108. move $s2, $0 #i = 0
  109.  
  110. for_loop2:
  111. beq $s2, $s7, end_forloop2 #i == N, branch
  112. move $s3, $0 # j = 0
  113.  
  114. for_loop3:
  115. beq $s3, $s7, end_forloop3 #j == N, branch
  116.  
  117. move $a0, $s2 #arguments for neighbours
  118. move $a1, $s3
  119. jal neighbours
  120.  
  121. move $t0, $v0 #t0 has the value nn
  122. add $t1, $s7, -1
  123. mul $t1, $s2, $t1 #offset by i
  124. add $s4, $t1, $s3 #total offset is in s4
  125.  
  126. lb $t1, board($s4)
  127. bne $t1, $s6, else_main_1 #x != 1, branch to Else
  128.  
  129. li $t3, 2
  130. li $t4, 3
  131. bge $t3, $t0, else_if_main_1 #2 >= nn, then branch
  132. sb $0, newboard($s4) #newboard[i][j] = 0;
  133. j continued
  134. else_if_main_1:
  135. beq $t0, $t3, n_b_1
  136. beq $t0, $t4, n_b_1
  137. sb $0, newboard($s4) #newboard[i][j] = 0;
  138. j continued
  139. n_b_1:
  140. sb $s6, newboard($s4) #newboard[i][j] = 1;
  141. j continued
  142. else_main_1:
  143. bne $t0, $t4, final_Else #branch if nn != 3
  144. sb $s6, newboard($s4) #newboard[i][j] = 1;
  145. j continued
  146.  
  147. final_Else:
  148. sb $0, newboard($s4) #newboard[i][j] = 0;
  149. continued:
  150. addi $s3, $s3,1 #j++;
  151. j for_loop3
  152.  
  153. end_forloop3:
  154.  
  155. addi $s2, $s2, 1 #i++;
  156. j for_loop2
  157.  
  158. end_forloop2:
  159. la $a0, msg_result_1
  160. li $v0, 4
  161. syscall #printf === After iteration
  162.  
  163. move $a0, $s1
  164. li $v0, 1
  165. syscall #print n
  166.  
  167. la $a0, msg_result_2
  168. li $v0, 4
  169. syscall #print ===
  170.  
  171. la $a0, eol
  172. li $v0, 4 # print the '\n'
  173. syscall
  174.  
  175. jal copyBackAndShow
  176.  
  177. addi $s1, $s1, 1 #n++;
  178. j for_loop1
  179.  
  180. end_main:
  181. move $v0, $0 #return 0;
  182. lw $ra, main_ret_save
  183. jr $ra
  184.  
  185. #-###############################################################
  186. #
  187. # int neighbours(int i, int j)
  188. #{
  189. # int nn = 0;
  190. # for (int x = -1; x <= 1; x++) {
  191. # for (int y = -1; y <= 1; y++) {
  192. # if (i + x < 0 || i + x > N-1) continue;
  193. # if (j + y < 0 || j + y > N-1) continue; N-1 < j+ u
  194. # if (x == 0 && y == 0) continue;
  195. # if (board[i+x][j+y] == 1) nn++;
  196. # }
  197. # }
  198. # return nn;
  199. ################################################################
  200.  
  201. neighbours: #arguments i == $a0, y = $a1, $s6 = 1, $s7 == N
  202. li $t8, 0
  203. li $t0, -1 #x = -1 -> $t0
  204. addi $t6, $s7, -1q #N - 1 -> t6
  205. #----------------------------
  206. n_forloop1:
  207. bgt $t0, $s6, n_end_forloop1 #till 1 >= x
  208. li $t1, -1 #y = -1 -> $t1
  209.  
  210. n_forloop2:
  211. bgt $t1, $s6, n_end_forloop2 #till 1 >= y
  212.  
  213. add $t3, $a0, $t0 # i + x -> t3
  214. bltz $t3, incrementing_loop2
  215.  
  216. blt $t6, $t3, incrementing_loop2 #(N - 1) < i + x
  217.  
  218. n_if_2:
  219.  
  220. add $t5, $a1, $t1 # j + y -> t5
  221. bltz $t5, incrementing_loop2 # (j + y < 0)
  222.  
  223. blt $t6, $t5, incrementing_loop2 #N - 1 < j + y
  224.  
  225. n_if_3:
  226.  
  227. bnez $t0, n_if_4 #branch if any is non zero
  228. bnez $t1, n_if_4
  229. j incrementing_loop2
  230.  
  231. n_if_4:
  232. mul $t4, $t3, $s7 #(N - 1 * (i + x)) -> t4
  233. add $t4, $t4, $t5 #offset calculation
  234. lb $t3, board($t4) #board[offset]
  235.  
  236. bne $t3, $s6, incrementing_loop2 #(byte on board == 1), branch
  237. addi $t8, $t8, 1 #add 1 to v0 which is nn and the output of the program
  238.  
  239. incrementing_loop2:
  240. add $t1,$t1, 1 #nn++;
  241. j n_forloop2
  242. #--------------------------
  243. n_end_forloop2:
  244. add $t0, $t0, 1 #incrementing x
  245. j n_forloop1
  246.  
  247. n_end_forloop1:
  248. move $v0, $t8
  249. move $a0, $v0
  250. li $v0, 1
  251. syscall
  252. move $v0, $a0
  253. jr $ra
  254. #---------------------------------------------------------------------------------------------------------------------------------------------------
  255. #void copyBackAndShow()
  256. #{
  257. # for (int i = 0; i < N; i++) {
  258. # for (int j = 0; j < N; j++) {
  259. # board[i][j] = newboard[i][j];
  260. # if (board[i][j] == 0)
  261. # putchar('.');
  262. # else
  263. # putchar('#');
  264. # }
  265. # putchar('\n');
  266. # }
  267. #}
  268. #---------------------------------------------------------------------------------------------------------------------------------------------------
  269. copyBackAndShow:
  270.  
  271. move $t0, $0 #i = 0 -> t0
  272. addi $t6, $s7, -1
  273. c_forloop1:
  274. beq $t0, $s7, c_end_forloop1 #branch when i = N
  275. move $t1 ,$0 #j = 0 -> t1
  276.  
  277. c_forloop2:
  278. beq $t1, $s7, c_end_forloop2 #branch when j = N
  279.  
  280. mul $t2, $t0, $t6
  281. add $t2, $t2, $t1 #offset calculation
  282.  
  283. lb $t3, newboard($t2)
  284. sb $t3, board($t2) #board[i][j] = newboard[i][j];
  285. lb $t3, newboard($t2)
  286.  
  287. beq $t3, $s6 , c_ifelse #(board[i][j] != 0), then branch
  288.  
  289. la $a0, msg_dot # print the '.'
  290. li $v0, 4
  291. syscall
  292.  
  293. j c_incrementing_j
  294.  
  295. c_ifelse:
  296. la $a0, msg_hash # print the '#'
  297. li $v0, 4
  298. syscall
  299.  
  300. c_incrementing_j:
  301. add $t1, $t1, 1 #incrementing j
  302. j c_forloop2
  303.  
  304. c_end_forloop2:
  305. la $a0, eol
  306. li $v0, 4 # print the '\n'
  307. syscall
  308.  
  309. addi $t0, $t0, 1 #incrementing i
  310. j c_forloop1
  311.  
  312. c_end_forloop1:
  313. jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement