# # Slow assembly code. # Do not use as a starting point: start by thinking # about the problem and considering various approaches # then write out your approach in C or pseudo-code, # and then translate into assembly. If you try to # do it directly in assembly first, you're likely to # run into errors and waste time. # # on frame: # ra,a0,a1,a2,a3, living_neighbors # 24 20 16 12 8 4 .globl life_xform life_xform: subu $sp,$sp,24 sw $ra,24($sp) sw $a0,20($sp) sw $a1,16($sp) sw $a2,12($sp) sw $a3,8($sp) li $a1,0 b life_xform_r_loop_test life_xform_r_loop: li $a2,0 b life_xform_c_loop_test life_xform_c_loop: lw $a0,20($sp) subu $a1,$a1,1 # temporary munge index to get r-1 subu $a2,$a2,1 # c-1 jal cell_alive # (grid,r-1,c-1) sw $v0,4($sp) addu $a2,$a2,1 jal cell_alive # (grid,r-1,c) lw $t0,4($sp) addu $v0,$v0,$t0 sw $v0,4($sp) addu $a2,$a2,1 jal cell_alive # (grid,r-1,c+1) lw $t0,4($sp) addu $v0,$v0,$t0 sw $v0,4($sp) addu $a1,$a1,1 jal cell_alive # (grid,r,c+1) lw $t0,4($sp) addu $v0,$v0,$t0 sw $v0,4($sp) addu $a2,$a2,-2 jal cell_alive # (grid,r,c-1) lw $t0,4($sp) addu $v0,$v0,$t0 sw $v0,4($sp) addu $a1,$a1,1 jal cell_alive # (grid,r+1,c-1) lw $t0,4($sp) addu $v0,$v0,$t0 sw $v0,4($sp) addu $a2,$a2,1 jal cell_alive # (grid,r+1,c) lw $t0,4($sp) addu $v0,$v0,$t0 sw $v0,4($sp) addu $a2,$a2,1 jal cell_alive # (grid,r+1,c+1) subu $a1,$a1,1 subu $a2,$a2,1 # fix up indices lw $t0,4($sp) addu $v0,$v0,$t0 bne $v0,3,life_xform_not_birth lw $a0,16($sp) # grid2 li $a3,1 jal set_cell # (grid2,r,c,1) b life_xform_c_loop_done life_xform_not_birth: bne $v0,2,life_xform_not_keep jal cell_alive # (grid,r,c) move $a3,$v0 lw $a0,16($sp) # grid2 jal set_cell # (grid2,r,c,cell_alive(...)) b life_xform_c_loop_done life_xform_not_keep: lw $a0,16($sp) # grid2 li $a3,0 jal set_cell # (grid2,r,c,0) life_xform_c_loop_done: addu $a2,$a2,1 life_xform_c_loop_test: lw $t0,width blt $a2,$t0,life_xform_c_loop addu $a1,$a1,1 life_xform_r_loop_test: lw $t0,height blt $a1,$t0,life_xform_r_loop lw $ra,24($sp) lw $a0,20($sp) lw $a1,16($sp) lw $a2,12($sp) lw $a3,8($sp) addu $sp,$sp,24 jr $ra