Advertisement
Guest User

Untitled

a guest
May 12th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Copyright Maxwell Cudlitz
  2. # If you steal this for a final project I will find you
  3.  
  4. .data ## Message + temp & main cell boards
  5.  
  6. # bitmap viewer requires word sizes
  7. main_array:         .space 0x10000      # initial allocation for the main array (32 * 32 words)
  8. temp_array:         .space 0x10000      # initial allocation for temporary array (32 * 32 words)
  9. welcome_message:    .asciiz "[Make sure the bitmap display is open, with the following settings]\nUnit Width in Pixels:\t 1 x 1\nDisplay Size:\t\t 128 x 128\nBase Address:\t\t 0x10010000"
  10.  
  11. .text
  12.  
  13. la  $s1, main_array         # get main_array address
  14. la  $s2, temp_array         # get temp_array address
  15. li  $s3, 0x0            # used for neighbor counts
  16. li  $s4, 0x0            # used mem address offsets in check
  17.  
  18. GAMELOOP:
  19.         # print instructions for viewing board
  20.         li  $v0, 4
  21.         la  $a0, welcome_message
  22.         syscall
  23.  
  24.         # generate random board
  25.         jal RANDOMIZEBOARD
  26.     gameTick:
  27.         # load next frame of game state into temporary array
  28.         la  $a0, CHECKCELL
  29.         jal LOOPBOARD
  30.        
  31.         # load next frame of game state into temporary array
  32.         la  $a0, COPYCELL
  33.         jal LOOPBOARD
  34.        
  35.         # loop
  36.         j   gameTick       
  37.  
  38. # loops over board, JALR to address $a0
  39. # used for loading the temporary board, as well as copying
  40. LOOPBOARD:
  41.         addi    $sp, $sp, -0x4          # ra to stack
  42.         sw  $ra, 0x0($sp)  
  43.        
  44.         move    $t3, $a0            # store param
  45.  
  46.         li  $t0, 0x0            # iterator- iterate by 32 (bits in a word)
  47.     loopBoard: 
  48.         bgeu    $t0, 0x10000, returnBoard   # break loop when iterator reaches end of memory segment
  49.         add $t1, $t0, $s1           # store address + offset
  50.            
  51.         addi    $sp, $sp, -0xC          # push locals to stack
  52.         sw  $t0, 0($sp)
  53.         sw  $t1, 4($sp)
  54.         sw  $t3, 8($sp)
  55.        
  56.         move    $a0, $t0            # pass iterator as param
  57.         jalr    $t3             # jump to passed address
  58.                            
  59.         lw  $t0, 0($sp)         # pop locals from stack
  60.         lw  $t1, 4($sp)
  61.         lw  $t3, 8($sp)
  62.         addi    $sp, $sp, 0xC          
  63.        
  64.         addi    $t0, $t0, 0x4           # iterate outer iterator by 4 (for word-offset cells)
  65.                
  66.         j   loopBoard           # continue loop
  67.         returnBoard:
  68.             lw  $ra, 0($sp)         # pop ra from stack
  69.         add     $sp, $sp, 0x4      
  70.        
  71.             jr  $ra             # return
  72.  
  73. # fill board with random bits, one every word
  74. RANDOMIZEBOARD:
  75.         addi    $sp, $sp, -0x4          # ra to stack
  76.         sw  $ra, 0($sp)
  77.        
  78.         li  $t0, 0x0            # iterator; iterates by 32 (bits in a word)
  79.     loopBoardRnd:  
  80.         bgtu    $t0, 0x4000, returnBoardRnd # return if board filled
  81.         add $t1, $t0, $s1           # add to mem offset to obtain array location
  82.         li      $v0, 41             # random int code
  83.         li      $a1, 0xFFFFFFFF         # max bound = word max (will set 32 elements of array at a time)
  84.         syscall                 # generate rand
  85.         move    $t3, $a0            # store rand result
  86.        
  87.         addi    $sp, $sp, -0x8          # push locals to stack
  88.         sw  $t0, 0($sp)
  89.         sw  $t1, 4($sp)
  90.        
  91.         move    $a0, $t3            # load random result into arg
  92.         mul $a1, $t0, 0x4           # load iterator byte offset into arg (group of 32 words)
  93.         jal     INSERTRANDWORD          # insert 32 random cell values
  94.        
  95.         lw  $t0, 0($sp)         # pop locals from stack
  96.         lw  $t1, 4($sp)
  97.         addi    $sp, $sp, 0x8  
  98.        
  99.         addi    $t0, $t0, 0x20          # iterate by 32
  100.         j   loopBoardRnd            # continue loop
  101.         returnBoardRnd:
  102.             lw  $ra, 0($sp)         # pop ra from stack
  103.         add     $sp, $sp, 0x4      
  104.             jr  $ra             # return
  105.            
  106. # inserts the word at $a0 into the board at offset $a1, setting 64 cells at once    
  107. # makes rand insertion more than 64 times faster
  108. INSERTRANDWORD:
  109.         addi    $sp, $sp, -0x4          # push ra to stack
  110.         sw  $ra, 0($sp)
  111.        
  112.         li  $t0, 0x0            # outer iterator; iterate over bits of word
  113.         li  $t2, 0x80000000         # store [10000000000000000000000000000000b] to serve as bitmask
  114.     loopInsRnd:
  115.         bge     $t0, 0x20, returnInsRnd     # after iterating 32 bits (1 word) return
  116.        
  117.         and $t4, $t2, $a0           # will be nonzero if bit at this element
  118.         sne $t4, $t4, 0x0           # sets if bit was nonzero
  119.         sll $t4, $t4, 0x7           # shift to max value
  120.        
  121.         mul $t1, $t0, 0x4           # mul iterator to get word offset
  122.         add $t1, $t1, $s1           # add to mem offset to obtain array location
  123.         add $t1, $t1, $a1           # offset by passed mem offset
  124.         sw  $t4, 0($t1)         # store byte in array
  125.        
  126.         addi    $t0, $t0, 0x1           # iterate
  127.         srl $t2, $t2, 0x1           # shift mask
  128.         jal     loopInsRnd          # continue loop
  129.         returnInsRnd:
  130.             lw  $ra, 0($sp)         # pop + jump to ra
  131.             addi    $sp, $sp, 0x4
  132.             jr  $ra
  133.            
  134. # checks the cell at index $a0
  135. CHECKCELL:
  136.         # Checks are done in a clockwise direction
  137.         # 0 1 2
  138.         # 7 x 3
  139.         # 6 5 4
  140.        
  141.         add $s4, $s1, $a0   # store address + index offset
  142.        
  143.         li  $s3, 0x0    # reset neighbor count
  144.         move    $t0, $a0    # copy cell index
  145.         addi    $t0, $t0, 0x1   # add 1 to index to prevent div by zero, and align index.
  146.         li  $t1, 0x80   # load 128 * 4 -  (column count (bytes)) for div
  147.         srl $t0, $t0, 0x2   # normalize
  148.         div $t1, $t0, $t1   # div to obtain position on row
  149.         mfhi    $t1     # get position on row from remainder
  150.  
  151.         seq $t3, $t1, 0x1   # flag if leftmost column
  152.         seq $t4, $t1, 0x200 # flag if rightmost column 
  153.  
  154.     checkCell_0:   
  155.         blt $t0, 0x200, cellCheck_3 # skip upper checks (0 - 2) if index is on first row
  156.         bne $t3, 0x0, checkCell_1   # skip if left-aligned
  157.         lw  $t5, -0x204($s4)    # load top-left byte (-128 - 1) * 4
  158.         add $s3, $s3, $t5       # add value to neighbor counter
  159.     checkCell_1:
  160.         lw  $t5, -0x200($s4)    # load top-mid byte (-128) * 4
  161.         add $s3, $s3, $t5       # add value to neighbor counter
  162.     cellCheck_2:
  163.         bne $t3, 0x0, cellCheck_5   # early-exit 2-4 if right-aligned
  164.         lw  $t5, -0x1FC($s4)    # load top-right byte (-128 + 1) * 4
  165.         add $s3, $s3, $t5       # add value to neighbor counter
  166.     cellCheck_3:
  167.         lb  $t5, 0x4($s4)       # load mid-right byte 1 * 4
  168.         add $s3, $s3, $t5       # add value to neighbor counter
  169.     checkCell_4:
  170.         # (128 * 128) - 64 - 1 = 0x3FBF
  171.         # skip lower checks (0 - 2) if index is on first row [default case]
  172.         ####### possible problem area as is not BGTE #######
  173.         bgt $t1, 0x3FBF, checkCell_7
  174.         lw  $t5, 0x204($s4)     # load bot-right byte (128 + 1) * 4
  175.         add $s3, $s3, $t5       # add value to neighbor counter
  176.     cellCheck_5:
  177.         lw  $t5, 0x200($s4)     # load mid-bot byte (128) * 4
  178.         add $s3, $s3, $t5       # add value to neighbor counter
  179.     cellCheck_6:
  180.         bne $t4, 0x0, finishCheck   # early-exit 6-7 if left-aligned
  181.         lw  $t5, 0x1FC($s4)     # load bot-left byte (128 - 1) * 4
  182.         add $s3, $s3, $t5       # add value to neighbor counter
  183.     checkCell_7:
  184.         lw  $t5, -0x4($s4)      # load mid-left byte -1 * 4
  185.         add $s3, $s3, $t5       # add value to neighbor counter
  186.     finishCheck:
  187.         lw  $t0, 0x0($s4)       # load val if no change
  188.         srl $s3, $s3, 0x7       # normalize counter
  189.         bgt $s3, 0x3, kill      # overpopulation
  190.         blt $s3, 0x2, kill      # underpopulation
  191.         beq $s3, 0x3, breeding  # breeding
  192.         j   finCheck   
  193.     kill:
  194.         li  $t0, 0x0        # new cell will be = 0
  195.         j   finCheck
  196.     breeding:
  197.         li  $t0, 0x80       # new cell will be = 1
  198.     finCheck:
  199.         add $s4, $s2, $a0       # store address + index offset of temp array
  200.         sw  $t0, 0x0($s4)       # store in temp array
  201.             jr  $ra         # return to loop
  202.        
  203. COPYCELL:
  204.         add $t0, $s1, $a0       # store address + index offset (main board)
  205.         add $t1, $s2, $a0       # store address + index offset (temp board)
  206.         lw  $t1, 0($t1)     # load value from temp board
  207.         sw  $t1, 0($t0)     # store value in main board from temp
  208.         jr  $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement