	.data

gen1:	.asciiz	"      ********      "
	.asciiz "    *          *    "
	.asciiz "  *              *  "
	.asciiz " *     *    *     * "
	.asciiz " *                * "
	.asciiz " *       *        * "
	.asciiz " *   *        *   * "
	.asciiz "  *   ********   *  "
	.asciiz "    *          *    "
	.asciiz "      ********      "

gen2:	.asciiz "                    "
	.asciiz "                    "
	.asciiz "                    "
	.asciiz "                    "
	.asciiz "                    "
	.asciiz "                    "
	.asciiz "                    "
	.asciiz "                    "
	.asciiz "                    "
	.asciiz "                    "

greet:	.asciiz "\n\nPlease enter number of generations or 0 to quit :   "
later:  .asciiz "\n\nGood Bye!  Thanks For Playing!  Come Again! \n "
newline:.asciiz "\n"


				     # i = number of rows
				     # j = number of columns
				     # g = number of generations

	.text

main:	
		la $s0, gen1	     # load starting address of gen1 into s0
		la $s1, gen2	     # load starting address of gen2 into s1
		li $a2, 10	     # load i=10 (rows) into a2
		li $a3, 20	     # load j=20 (columns) into a3
		jal showgener	     # jump to showgen		
begingame:	li $v0, 4	     # get ready to print first string
		la $a0, greet	     # print prompting string
		syscall
		li $v0, 5	     # read in number of generations g
		syscall
		move $s2, $v0	     # s2 = g
		beqz $s2, endgame    # end if user enters 0	
generloop:	jal nextgener	     # branch to nextgener
		move $s3, $s0	     # move gen1 into a temp loc.
		move $s0, $s1	     # move gen2 into gen1
		move $s1, $s3	     # put temp into gen1
		jal showgener	     # print out new generation
		addi $s2, $s2, -1    # decrement s2
		beqz $s2, begingame  # if g = 0 go back to start
		b generloop	     # go create next generation
endgame:	li $v0, 4	     # get ready to print later string
		la $a0, later	     # print later string
		syscall
                li      $v0, 10      # exit program service 
                syscall 


showgener:	
		addi $sp, $sp, -8    # create a new 8 byte stack
		sw $s0, ($sp)	     # save s0 onto stack
		sw $s1, 4($sp)	     # save s1 onto stack
		move $s1, $a2	     # load i into s1
		li $v0, 4	     # get ready to print out gen1
		la $a0, newline	     # load newline string into a0
		syscall		     # print a blank line
		syscall		     # print another blank line
		syscall		     # print another blank line
		syscall		     # final separation of generations
		move $a0, $s0	     # move first line of gen1 in a0
		syscall		     # print first line of gen1
		la $a0, newline	     # move cursor onto next line
		syscall		     # print newline
printgenerloop:	addi $s1, $s1, -1    # decrement s1
		beqz $s1, endgener   # exit once all 
				     # 10 rows have been printed
		addi $s0, $s0, 21    # increment s0 to next line of gen1
		move $a0, $s0	     # move next line of gen1 
				     # into a0 to be printed		
		syscall		     # print out next line of gen1
		la $a0, newline	     # move cursor onto next line
		syscall		     # print \n
		b printgenerloop     # go back to print next line
endgener:	lw $s0, ($sp)	     # load s0 from stack
		lw $s1, 4($sp)	     # load s1 from stack
		addi $sp, $sp, 8     # return stack pointer 
				     # to previous value
		jr $ra		     # return to calling program 



nextgener:			     # t0 = temp variable
		li $t1, 42	     # t1 = "*"
		li $t2, 21           # t2=21
	 	li $t3, 0	     # t3=i=0
		 	 	     # t4=j=0
				     # t5=l
				     # t6=r
	  		             # t7=u
				     # t8=d
				     # t9=count
iloop:		
		addi $t7, $t3, -1    # u = i - 1
		bnez $t3, inotzero   # i == 0 ?
		addi $t7, $a2, -1    # u = nRows - 1
inotzero:	addi $t8, $t3, 1     # d = i + 1
		bne $t8, $a2, dnotn  # d != nRows ?
		li $t8, 0	     # d = 0 
dnotn:		li $t4, 0	     # j = 0 ?
jloop:		addi $t5, $t4, -1    # l = j - 1
		bnez $t4, jnotzero   # j == 0
		addi $t5, $a3, -1    # l = nCol - 1
jnotzero:	addi $t6, $t4, 1     # r = j + 1
		bne $t6, $a3, uplft  # r != nCol
		li $t6, 0	     # r = 0	
uplft:		li $t9, 0	     # count = 0
		mul $t0, $t2, $t7    # temp = 21 * nRow
		add $t0, $t0, $t5    # temp = 21 * nRow + l
		add $t0, $t0, $s0    # temp = gen1 + offset
		lb $t0, ($t0)	     # load byte in [u][l]
		bne $t1, $t0, above  # check if *
		addi $t9, $t9, 1     # increment count if *
above:		mul $t0, $t2, $t7    # temp = 21 * nRow
		add $t0, $t0, $t4    # temp = 21 * nRow + j
	        add $t0, $t0, $s0    # temp = gen1 + offset
	        lb $t0, ($t0)	     # load byte in [u][j]
                bne $t1, $t0, uprt   # check if *
	        addi $t9, $t9, 1     # increment count if *
uprt:		mul $t0, $t2, $t7    # temp = 21 * nRow
		add $t0, $t0, $t6    # temp = 21 * nRow + r
	        add $t0, $t0, $s0    # temp = gen1 + offset
	        lb $t0, ($t0)	     # load byte in [u][r]
                bne $t1, $t0, left   # check if *
	        addi $t9, $t9, 1     # increment count if *
left:		mul $t0, $t2, $t3    # temp = 21 * nRow
		add $t0, $t0, $t5    # temp = 21 * nRow + l
	        add $t0, $t0, $s0    # temp = gen1 + offset
	        lb $t0, ($t0)	     # load byte in [i][l]
                bne $t1, $t0, right  # check if *
	        addi $t9, $t9, 1     # increment count if *
right:		mul $t0, $t2, $t3    # temp = 21 * nRow
		add $t0, $t0, $t6    # temp = 21 * nRow + r
	        add $t0, $t0, $s0    # temp = gen1 + offset
	        lb $t0, ($t0)	     # load byte in [i][r]
                bne $t1, $t0, undlft # check if *
	        addi $t9, $t9, 1     # increment count if *
undlft:		mul $t0, $t2, $t8    # temp = 21 * nRow
		add $t0, $t0, $t5    # temp = 21 * nRow + l
	        add $t0, $t0, $s0    # temp = gen1 + offset
	        lb $t0, ($t0)	     # load byte in [d][l]
                bne $t1, $t0, under  # check if *
	        addi $t9, $t9, 1     # increment count if *
under:		mul $t0, $t2, $t8    # temp = 21 * nRow
		add $t0, $t0, $t4    # temp = 21 * nRow + j
	        add $t0, $t0, $s0    # temp = gen1 + offset
	        lb $t0, ($t0)	     # load byte in [d][j]
                bne $t1, $t0, undrt  # check if *
	        addi $t9, $t9, 1     # increment count if *
undrt:		mul $t0, $t2, $t8    # temp = 21 * nRow
		add $t0, $t0, $t6    # temp = 21 * nRow + r
	        add $t0, $t0, $s0    # temp = gen1 + offset
	        lb $t0, ($t0)	     # load byte in [d][r]
                bne $t1, $t0, assess # check if *
	        addi $t9, $t9, 1     # increment count if *
assess:		mul $t0, $t2, $t3    # temp = 21 * nRow
		add $t0, $t0, $t4    # temp = 21 * nRow + j
	        add $t0, $t0, $s0    # temp = gen1 + offset
	        lb $t0, ($t0)	     # load byte in i,j
		bne $t0, $t1, nolife # check if gen(i,j) = "*"
		li $t0, 2	     # load 2 into $t0 for
				     # comparison
		blt $t9, $t0, dead   # branch to dead if 
				     # count < 2
		li $t0,	3	     # load 3 into $t0 for
				     # comparison
		bgt $t9, $t0, dead   # branch to dead if 
				     # count > 3
		b create	     # branch to create
nolife:		li $t0, 3	     # load 3 into $t0 for
				     # comparison
		beq $t9, $t0, create # go to dead if count != 3
		b dead		     # go to create new life
create:		mul $t0, $t2, $t3    # temp = 21 * nRow
		add $t0, $t0, $t4    # temp = 21 * nRow + j
		add $t0, $s1, $t0    # temp = gen2 + offset
		sb $t1, ($t0)	     # store a "*" in array
		b increment	     # go to next j
dead:		mul $t0, $t2, $t3    # temp = 21 * nRow
		add $t0, $t0, $t4    # temp = 21 * nRow + j
	        add $t0, $s1, $t0    # temp = gen1 + offset
	        li $t2, 32	     # load a space into $t2
		sb $t2, ($t0)        # store an " " in array
		li $t2, 21	     # put value of 21 back in $t2
increment:	addi $t4, $t4, 1     # increment j
		blt $t4, $a3, jloop  # if j < col, goto jloop
		addi $t3, $t3, 1     # increment i
		blt $t3, $a2, iloop  # if i < rows, goto iloop
		jr $ra
