Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # board.s ... Game of Life on a 10x10 grid
- .data
- N: .word 10 # gives board dimensions
- board:
- .byte 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
- .byte 1, 1, 0, 0, 0, 0, 0, 0, 0, 0
- .byte 0, 0, 0, 1, 0, 0, 0, 0, 0, 0
- .byte 0, 0, 1, 0, 1, 0, 0, 0, 0, 0
- .byte 0, 0, 0, 0, 1, 0, 0, 0, 0, 0
- .byte 0, 0, 0, 0, 1, 1, 1, 0, 0, 0
- .byte 0, 0, 0, 1, 0, 0, 1, 0, 0, 0
- .byte 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
- .byte 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
- .byte 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
- newboard: .space 100
- # prog.s ... Game of Life on a NxN grid
- #
- # Needs to be combined with board.s
- # The value of N and the board data
- # structures come from board.s
- #
- # Written by <<YOU>>, August 2017
- .data
- number_1:
- .word 1
- main_ret_save:
- .space 4
- print_scan_msg1:
- .asciiz "# Iterations: "
- true:
- .word 1
- eol:
- .asciiz "\n"
- msg_hash:
- .asciiz "#",
- msg_dot:
- .asciiz "."
- msg_result_1:
- .asciiz "=== After iteration "
- msg_result_2:
- .asciiz " ==="
- .text
- .globl main
- ################################################################
- #
- # int maxiters;
- # printf("# Iterations: ");
- # scanf("%d", &maxiters);
- # for (int n = 1; n <= maxiters; n++) {
- # for (int i = 0; i < N; i++) {
- # for (int j = 0; j < N; j++) {
- # int nn = neighbours(i,j);
- # if (board[i][j] == 1) {
- # if (nn < 2)
- # newboard[i][j] = 0;
- # else if (nn ==2 || nn == 3)
- # newboard[i][j] = 1;
- # else
- # newboard[i][j] = 0;
- # }
- # else if (nn == 3)
- # newboard[i][j] = 1;
- # else
- # newboard[i][j] = 0;
- # }
- # }
- # printf("=== After iteration %d ===\n", n);
- # copyBackAndShow();
- # }
- # return 0;
- #}
- ################################################################
- main:
- sw $ra, main_ret_save
- la $a0, print_scan_msg1 # printf("# Iterations: ");
- li $v0, 4
- syscall
- li $v0, 5 # scanf("%d", &maxiters);
- syscall
- move $s0, $v0 #Now s0 has the value of maxiters
- li $s1, 1 #n = 1
- lw $s7, N #s7 = N
- lw $s6, true #s6 = 1
- for_loop1:
- bgt $s1 , $s0, end_main #maxiters > n, branch
- move $s2, $0 #i = 0
- for_loop2:
- beq $s2, $s7, end_forloop2 #i == N, branch
- move $s3, $0 # j = 0
- for_loop3:
- beq $s3, $s7, end_forloop3 #j == N, branch
- move $a0, $s2 #arguments for neighbours
- move $a1, $s3
- jal neighbours
- move $t0, $v0 #t0 has the value nn
- add $t1, $s7, -1
- mul $t1, $s2, $t1 #offset by i
- add $s4, $t1, $s3 #total offset is in s4
- lb $t1, board($s4)
- bne $t1, $s6, else_main_1 #x != 1, branch to Else
- li $t3, 2
- li $t4, 3
- bge $t3, $t0, else_if_main_1 #2 >= nn, then branch
- sb $0, newboard($s4) #newboard[i][j] = 0;
- j continued
- else_if_main_1:
- beq $t0, $t3, n_b_1
- beq $t0, $t4, n_b_1
- sb $0, newboard($s4) #newboard[i][j] = 0;
- j continued
- n_b_1:
- sb $s6, newboard($s4) #newboard[i][j] = 1;
- j continued
- else_main_1:
- bne $t0, $t4, final_Else #branch if nn != 3
- sb $s6, newboard($s4) #newboard[i][j] = 1;
- j continued
- final_Else:
- sb $0, newboard($s4) #newboard[i][j] = 0;
- continued:
- addi $s3, $s3,1 #j++;
- j for_loop3
- end_forloop3:
- addi $s2, $s2, 1 #i++;
- j for_loop2
- end_forloop2:
- la $a0, msg_result_1
- li $v0, 4
- syscall #printf === After iteration
- move $a0, $s1
- li $v0, 1
- syscall #print n
- la $a0, msg_result_2
- li $v0, 4
- syscall #print ===
- la $a0, eol
- li $v0, 4 # print the '\n'
- syscall
- jal copyBackAndShow
- addi $s1, $s1, 1 #n++;
- j for_loop1
- end_main:
- move $v0, $0 #return 0;
- lw $ra, main_ret_save
- jr $ra
- #-###############################################################
- #
- # int neighbours(int i, int j)
- #{
- # int nn = 0;
- # for (int x = -1; x <= 1; x++) {
- # for (int y = -1; y <= 1; y++) {
- # if (i + x < 0 || i + x > N-1) continue;
- # if (j + y < 0 || j + y > N-1) continue; N-1 < j+ u
- # if (x == 0 && y == 0) continue;
- # if (board[i+x][j+y] == 1) nn++;
- # }
- # }
- # return nn;
- ################################################################
- neighbours: #arguments i == $a0, y = $a1, $s6 = 1, $s7 == N
- li $t8, 0
- li $t0, -1 #x = -1 -> $t0
- addi $t6, $s7, -1q #N - 1 -> t6
- #----------------------------
- n_forloop1:
- bgt $t0, $s6, n_end_forloop1 #till 1 >= x
- li $t1, -1 #y = -1 -> $t1
- n_forloop2:
- bgt $t1, $s6, n_end_forloop2 #till 1 >= y
- add $t3, $a0, $t0 # i + x -> t3
- bltz $t3, incrementing_loop2
- blt $t6, $t3, incrementing_loop2 #(N - 1) < i + x
- n_if_2:
- add $t5, $a1, $t1 # j + y -> t5
- bltz $t5, incrementing_loop2 # (j + y < 0)
- blt $t6, $t5, incrementing_loop2 #N - 1 < j + y
- n_if_3:
- bnez $t0, n_if_4 #branch if any is non zero
- bnez $t1, n_if_4
- j incrementing_loop2
- n_if_4:
- mul $t4, $t3, $s7 #(N - 1 * (i + x)) -> t4
- add $t4, $t4, $t5 #offset calculation
- lb $t3, board($t4) #board[offset]
- bne $t3, $s6, incrementing_loop2 #(byte on board == 1), branch
- addi $t8, $t8, 1 #add 1 to v0 which is nn and the output of the program
- incrementing_loop2:
- add $t1,$t1, 1 #nn++;
- j n_forloop2
- #--------------------------
- n_end_forloop2:
- add $t0, $t0, 1 #incrementing x
- j n_forloop1
- n_end_forloop1:
- move $v0, $t8
- move $a0, $v0
- li $v0, 1
- syscall
- move $v0, $a0
- jr $ra
- #---------------------------------------------------------------------------------------------------------------------------------------------------
- #void copyBackAndShow()
- #{
- # for (int i = 0; i < N; i++) {
- # for (int j = 0; j < N; j++) {
- # board[i][j] = newboard[i][j];
- # if (board[i][j] == 0)
- # putchar('.');
- # else
- # putchar('#');
- # }
- # putchar('\n');
- # }
- #}
- #---------------------------------------------------------------------------------------------------------------------------------------------------
- copyBackAndShow:
- move $t0, $0 #i = 0 -> t0
- addi $t6, $s7, -1
- c_forloop1:
- beq $t0, $s7, c_end_forloop1 #branch when i = N
- move $t1 ,$0 #j = 0 -> t1
- c_forloop2:
- beq $t1, $s7, c_end_forloop2 #branch when j = N
- mul $t2, $t0, $t6
- add $t2, $t2, $t1 #offset calculation
- lb $t3, newboard($t2)
- sb $t3, board($t2) #board[i][j] = newboard[i][j];
- lb $t3, newboard($t2)
- beq $t3, $s6 , c_ifelse #(board[i][j] != 0), then branch
- la $a0, msg_dot # print the '.'
- li $v0, 4
- syscall
- j c_incrementing_j
- c_ifelse:
- la $a0, msg_hash # print the '#'
- li $v0, 4
- syscall
- c_incrementing_j:
- add $t1, $t1, 1 #incrementing j
- j c_forloop2
- c_end_forloop2:
- la $a0, eol
- li $v0, 4 # print the '\n'
- syscall
- addi $t0, $t0, 1 #incrementing i
- j c_forloop1
- c_end_forloop1:
- jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement