SHARE
TWEET

Untitled

a guest Dec 6th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # This is the only file that will be considered for grading
  2.  
  3. .data
  4. # syscall constants
  5. PRINT_STRING            = 4
  6. PRINT_CHAR              = 11
  7. PRINT_INT               = 1
  8.  
  9. # memory-mapped I/O
  10. VELOCITY                = 0xffff0010
  11. ANGLE                   = 0xffff0014
  12. ANGLE_CONTROL           = 0xffff0018
  13.  
  14. BOT_X                   = 0xffff0020
  15. BOT_Y                   = 0xffff0024
  16.  
  17. TIMER                   = 0xffff001c
  18. ARENA_MAP               = 0xffff00dc
  19.  
  20. REQUEST_PUZZLE          = 0xffff00d0  ## Puzzle
  21. SUBMIT_SOLUTION         = 0xffff00d4  ## Puzzle
  22.  
  23. BONK_INT_MASK           = 0x1000
  24. BONK_ACK                = 0xffff0060
  25.  
  26. TIMER_INT_MASK          = 0x8000
  27. TIMER_ACK               = 0xffff006c
  28.  
  29. REQUEST_PUZZLE_INT_MASK = 0x800       ## Puzzle
  30. REQUEST_PUZZLE_ACK      = 0xffff00d8  ## Puzzle
  31.  
  32. GET_PAINT_BUCKETS       = 0xffff00e4
  33. SWITCH_MODE             = 0xffff00f0
  34.  
  35. ### Puzzle
  36. GRIDSIZE = 8
  37.  
  38. puzzle: .half 0:164
  39. heap: .half 0:50000
  40. PUZZLE_FLAG: .word 0
  41.  
  42. .text
  43. main:
  44.     # Construct interrupt mask
  45.     li      $t4, 0
  46.     or      $t4, $t4, BONK_INT_MASK # request bonk
  47.     or      $t4, $t4, REQUEST_PUZZLE_INT_MASK           # puzzle interrupt bit
  48.     or      $t4, $t4, 1 # global enable
  49.     mtc0    $t4, $12
  50.    
  51.     # Fill in your code here
  52.  
  53.     add $v0, $0, 1
  54.     sw $v0, SWITCH_MODE($0)
  55.  
  56.     j start_bot
  57.  
  58.     jr $ra
  59.  
  60. start_bot:
  61.     # check paint bucket
  62.     j get_paint
  63.  
  64. get_paint:
  65.  
  66.     # maybe add a check here to get a minimum of 100 tiles or smthn
  67.     li $t0, -1
  68.    
  69.     # get the number of paints
  70.     lw $t1, GET_PAINT_BUCKETS($0)
  71.  
  72.     bge $t1, $t0, start_moving
  73.  
  74.  
  75.     # check if not already requested a puzzle
  76.     la $t0, puzzle
  77.     sw $t0, REQUEST_PUZZLE($0)
  78.  
  79.     add $v0, $0, 10
  80.     sw $v0, VELOCITY($0)
  81.  
  82.     # now wait to solve it  
  83.    
  84.  
  85. solve_puzzle:
  86.     la $t0, PUZZLE_FLAG
  87.     lw $t0, PUZZLE_FLAG($0)
  88.     bne $t0, 1, solve_puzzle
  89.  
  90.     # set puzzle flag back to 0
  91.     li $t1, 0
  92.     sw $t1, PUZZLE_FLAG($0)
  93.  
  94.     # when puzzle is ready come down here and solve it
  95.     # solve(unsigned short *current_board, unsigned row, unsigned col, Puzzle* puzzle)
  96.     la      $a0, puzzle
  97.     la      $a1, heap
  98.     jal     copy_board    # Copy board to heap
  99.  
  100.     la $a0, heap
  101.     move $a1, $0
  102.     move $a2, $0
  103.     la $a3, puzzle
  104.     jal     solve
  105.  
  106.     la $t3, puzzle
  107.     sw $t3, SUBMIT_SOLUTION($0)
  108.  
  109.     j get_paint
  110.  
  111.  
  112. # this is probably gonna be where the dfs stuff happens
  113. start_moving:
  114.     add $v0, $0, 10
  115.     sw $v0, VELOCITY($0)
  116.  
  117.     li $v0, 42  # 42 is system call code to generate random int
  118.     li $a1, 4 # $a1 is where you set the upper bound
  119.     syscall     # your generated number will be at $a0
  120.  
  121.     li $v0, 1   # 1 is the system call code to show an int number
  122.     syscall     # as I said your generated number is at $a0, so it will be printed
  123.  
  124.     # mult by 4
  125.     li $t0, 4
  126.     mul $t0, $t0, $v0
  127.     sw $t0, ANGLE($0)
  128.  
  129.     li $t0, 0
  130.     sw $t0, ANGLE_CONTROL($0)
  131.  
  132.     # do this for 1000 cycles
  133.     li $t0, 0
  134.  
  135.  
  136. do_n_cycles:
  137.     li $t1, 1000
  138.     bge $t1, $t0, start_moving
  139.     add $t0, $t0, 1
  140.     j do_n_cycles
  141.  
  142.  
  143. infinite:
  144.     j start_moving
  145.  
  146.  
  147. ###################################################
  148. # This File Will Not Be Graded
  149. # Do Not Use this file as a part of exectution
  150. # DO NOT use this as apart of qtspimbot's execution
  151. # Copy the functions into your part2.s
  152. ###################################################
  153.  
  154. ### Put these in your data segment
  155.  
  156. # Given Puzzle Code
  157. # bool solve(unsigned short *current_board, unsigned row, unsigned col, Puzzle* puzzle) {
  158. #     if (row >= GRIDSIZE || col >= GRIDSIZE) {
  159. #         bool done = board_done(current_board, puzzle);
  160. #         if (done) {
  161. #             copy_board(current_board,puzzle->board);
  162. #         }
  163. #
  164. #         return done;
  165. #     }
  166. #     current_board = increment_heap(current_board, puzzle);
  167. #
  168. #     bool changed;
  169. #     do {
  170. #         changed = rule1(current_board);
  171. #         changed |= rule2(current_board);
  172. #         //changed |= rule3(board); //rule3 not done
  173. #     } while (changed);
  174.  
  175. #     //added:
  176. #     bool done = board_done(current_board, puzzle);
  177. #     if (done) {
  178. #         copy_board(current_board,puzzle->board);
  179. #     }
  180. #
  181. #
  182. #     short possibles = current_board[row*GRIDSIZE + col];
  183. #     for(char number = 0; number < GRIDSIZE; ++number) {
  184. #         if ((1 << number) & possibles) {
  185. #             current_board[row*GRIDSIZE + col] = 1 << number;
  186. #             unsigned next_row = ((col == GRIDSIZE-1) ? row + 1 : row);
  187. #             if (solve(current_board, next_row, (col + 1) % GRIDSIZE, puzzle)) {
  188. #                 return true;
  189. #             }
  190. #             current_board[row*GRIDSIZE + col] = possibles;
  191. #         }
  192. #     }
  193. #     return false;
  194. # }
  195. solve:
  196.     sub     $sp, $sp, 36
  197.     sw      $ra, 0($sp)
  198.     sw      $s0, 4($sp)
  199.     sw      $s1, 8($sp)
  200.     sw      $s2, 12($sp)
  201.     sw      $s3, 16($sp)
  202.     sw      $s4, 20($sp)
  203.     sw      $s5, 24($sp)
  204.     sw      $s6, 28($sp)
  205.     sw      $s7, 32($sp)
  206.     li   $s7, GRIDSIZE
  207.     move $s0, $a1     # row
  208.     move $s1, $a2     # col
  209.  
  210.     move $s2, $a0     # current_board
  211.     move $s3, $a3     # puzzle
  212.  
  213.     bge  $s0, $s7, solve_done_check  # row >= GRIDSIZE
  214.     bge  $s1, $s7, solve_done_check  # col >= GRIDSIZE
  215.     j solve_not_done
  216. solve_done_check:
  217.     move $a0, $s2     # current_board
  218.     move $a1, $s3     # puzzle
  219.     jal board_done
  220.  
  221.     beq $v0, $0, solve_done_false  # if (done)
  222.     move $s7, $v0     # save done
  223.     move $a0, $s2     # current_board
  224.     move $a1, $s3     # puzzle // same as puzzle->board
  225.     jal copy_board
  226.  
  227.     move $v0, $s7     # $v0: done
  228.  
  229.     j solve_done
  230.  
  231. solve_not_done:
  232.  
  233.     move $a0, $s2 # current_board
  234.     jal increment_heap
  235.     move $s2, $v0 # update current_board
  236.  
  237.     li  $v0, 0 # changed = false
  238. solve_start_do:
  239.  
  240.     move $a0, $s2
  241.  
  242.  
  243.     jal rule1          # changed = rule1(current_board);
  244.     move $s6, $v0      # done
  245.  
  246.     move $a0, $s2      # current_board
  247.     jal rule2
  248.  
  249.     or   $v0, $v0, $s6 # changed |= rule2(current_board);
  250.  
  251.     bne $v0, $0, solve_start_do # while (changed)
  252.  
  253.     move $a0, $s2     # current_board
  254.     move $a1, $s3     # puzzle
  255.     jal board_done
  256.  
  257.     beq $v0, $0, solve_board_not_done_after_dowhile  # if (done)
  258.     move $s7, $v0     # save done
  259.     move $a0, $s2     # current_board
  260.     move $a1, $s3     # puzzle // same as puzzle->board
  261.     jal copy_board
  262.  
  263.     move $v0, $s7     # $v0: done
  264.     j   solve_done
  265.  
  266. solve_board_not_done_after_dowhile:
  267.  
  268.  
  269.     mul $t0, $s0, $s7  # row*GRIDSIZE
  270.     add $t0, $t0, $s1  # row*GRIDSIZE + col
  271.     mul $t0, $t0, 2    # sizeof(unsigned short) * (row*GRIDSIZE + col)
  272.     add $s4, $t0, $s2  # &current_board[row*GRIDSIZE + col]
  273.     lhu $s6, 0($s4)    # possibles = current_board[row*GRIDSIZE + col]
  274.  
  275.     li $s5, 0 # char number = 0
  276. solve_start_guess:
  277.     bge $s5, $s7, solve_start_guess_end # number < GRIDSIZE
  278.     li $t0, 1
  279.     sll $t1, $t0, $s5 # (1 << number)
  280.     and $t0, $t1, $s6 # (1 << number) & possibles
  281.     beq $t0, $0, solve_start_guess_else
  282.     sh  $t1, 0($s4)   # current_board[row*GRIDSIZE + col] = 1 << number;
  283.    
  284.     move $a0, $s2     # current_board
  285.     move $a1, $s0     # next_row = row
  286.     sub  $t0, $s7, 1  # GRIDSIZE-1
  287.     bne  $s1, $t0, solve_start_guess_same_row # col < GRIDSIZE // col==GRIDSIZE-1
  288.     addi $a1, $a1, 1  # row + 1
  289. solve_start_guess_same_row:
  290.     move $a2, $s1     # col
  291.     addu $a2, $a2, 1  # col + 1
  292.     divu $a2, $s7
  293.     mfhi $a2          # (col + 1) % GRIDSIZE
  294.     move $a3, $s3     # puzzle
  295.     jal solve         # solve(current_board, next_row, (col + 1) % GRIDSIZE, puzzle)
  296.    
  297.     bne  $v0, $0, solve_done_true # if done {return true}
  298.     sh   $s6, 0($s4)  # current_board[row*GRIDSIZE + col] = possibles;
  299. solve_start_guess_else:
  300.     addi $s5, $s5, 1
  301.     j solve_start_guess
  302.  
  303. solve_done_false:
  304. solve_start_guess_end:
  305.     li  $v0, 0        # done = false
  306.  
  307. solve_done:
  308.     lw  $ra, 0($sp)
  309.     lw  $s0, 4($sp)
  310.     lw  $s1, 8($sp)
  311.     lw  $s2, 12($sp)
  312.     lw  $s3, 16($sp)
  313.     lw  $s4, 20($sp)
  314.     lw  $s5, 24($sp)
  315.     lw  $s6, 28($sp)
  316.     lw  $s7, 32($sp)
  317.     add $sp, $sp, 36
  318.     jr      $ra
  319.  
  320. solve_done_true:
  321.     li $v0, 1
  322.     j solve_done
  323.  
  324. # // bool rule1(unsigned short* board) {
  325. # //   bool changed = false;
  326. # //   for (int y = 0 ; y < GRIDSIZE ; y++) {
  327. # //     for (int x = 0 ; x < GRIDSIZE ; x++) {
  328. # //       unsigned value = board[y*GRIDSIZE + x];
  329. # //       if (has_single_bit_set(value)) {
  330. # //         for (int k = 0 ; k < GRIDSIZE ; k++) {
  331. # //           // eliminate from row
  332. # //           if (k != x) {
  333. # //             if (board[y*GRIDSIZE + k] & value) {
  334. # //               board[y*GRIDSIZE + k] &= ~value;
  335. # //               changed = true;
  336. # //             }
  337. # //           }
  338. # //           // eliminate from column
  339. # //           if (k != y) {
  340. # //             if (board[k*GRIDSIZE + x] & value) {
  341. # //               board[k*GRIDSIZE + x] &= ~value;
  342. # //               changed = true;
  343. # //             }
  344. # //           }
  345. # //         }
  346. # //       }
  347. # //     }
  348. # //   }
  349. # //   return changed;
  350. # // }
  351. #a0: board
  352. rule1:
  353.         sub     $sp, $sp, 36
  354.         sw      $ra, 0($sp)
  355.         sw      $s0, 4($sp)
  356.         sw      $s1, 8($sp)
  357.         sw      $s2, 12($sp)
  358.         sw      $s3, 16($sp)
  359.         sw      $s4, 20($sp)
  360.         sw      $s5, 24($sp)
  361.         sw      $s6, 28($sp)
  362.         sw      $s7, 32($sp)
  363.         li      $s0, GRIDSIZE                  # $s0: GRIDSIZE = 4
  364.         move    $s1, $a0                # $s1: board
  365.         li      $s2, 0                  # $s2: changed = false
  366.         li      $s3, 0                  # $s3: y = 0
  367. r1_for_y_start:
  368.         bge     $s3, $s0, r1_for_y_end  # for: y < GRIDSIZE
  369.         li      $s4, 0                  # $s4: x = 0
  370. r1_for_x_start:
  371.         bge     $s4, $s0, r1_for_x_end  # for: x < GRIDSIZE
  372.         mul     $a0, $s3, $s0           # $a0: y*GRIDSIZE
  373.         add     $a0, $a0, $s4           # $a0: y*GRIDSIZE + x
  374.         sll     $a0, $a0, 1             # $a0: 2*(y*GRIDSIZE + x)
  375.         add     $a0, $a0, $s1           # $a0: &board[y*GRIDSIZE+x]
  376.         lhu     $a0, 0($a0)             # $a0: value = board[y*GRIDSIZE+x]
  377.         move    $s6, $a0                # $s6: value
  378.         jal     has_single_bit_set
  379.         beq     $v0, 0, r1_for_x_inc    # if(has_single_bit_set(value))
  380.         li      $s5, 0                  # $s5: k = 0
  381. r1_for_k_start:
  382.         bge     $s5, $s0, r1_for_k_end  # for: k < GRIDSIZE
  383.         beq     $s5, $s4, r1_if_kx_end  # if (k != x)
  384.         mul     $t0, $s3, $s0           # $t0: y*GRIDSIZE
  385.         add     $t0, $t0, $s5           # $t0: y*GRIDSIZE + k
  386.         sll     $t0, $t0, 1             # $t0: 2*(y*GRIDSIZE + k)
  387.         add     $t0, $t0, $s1           # $t0: &board[y*GRIDSIZE+k]
  388.         lhu     $t1, 0($t0)             # $t1: board[y*GRIDSIZE + k]
  389.         and     $t2, $t1, $s6           # $t2: board[y*GRIDSIZE + k] & value
  390.         beq     $t2, 0, r1_if_kx_end    # if (board[y*GRIDSIZE + k] & value)
  391.         not     $t3, $s6                # $t3: ~value
  392.         and     $t1, $t1, $t3           # $t1:  board[y*GRIDSIZE + k] & ~value
  393.         sh      $t1, 0($t0)             # board[y*GRIDSIZE + k] &= ~value
  394.         li      $s2, 1                  # changed = true
  395. r1_if_kx_end:  
  396.         beq     $s5, $s3, r1_if_ky_end  # if (k != y)
  397.         mul     $t0, $s5, $s0           # $t0: k*GRIDSIZE
  398.         add     $t0, $t0, $s4           # $t0: k*GRIDSIZE + x
  399.         sll     $t0, $t0, 1             # $t0: 2*(k*GRIDSIZE + x)
  400.         add     $t0, $t0, $s1           # $t0: &board[k*GRIDSIZE+x]
  401.         lhu     $t1, 0($t0)             # $t1: board[k*GRIDSIZE + x]
  402.         and     $t2, $t1, $s6           # $t2: board[k*GRIDSIZE + x] & value
  403.         beq     $t2, 0, r1_if_ky_end    # if (board[k*GRIDSIZE + x] & value)
  404.         not     $t3, $s6                # $t3: ~value
  405.         and     $t1, $t1, $t3           # $t1:  board[k*GRIDSIZE + x] & ~value
  406.         sh      $t1, 0($t0)             # board[k*GRIDSIZE + x] &= ~value
  407.         li      $s2, 1                  # changed = true
  408. r1_if_ky_end:
  409.         add     $s5, $s5, 1             # for: k++
  410.         j       r1_for_k_start
  411. r1_for_k_end:
  412. r1_for_x_inc:
  413.         add     $s4, $s4, 1             # for: x++
  414.         j       r1_for_x_start  
  415. r1_for_x_end:          
  416. r1_for_y_inc:  
  417.         add     $s3, $s3, 1             # for: y++
  418.         j       r1_for_y_start
  419. r1_for_y_end:
  420.         move    $v0, $s2                # return changed
  421. r1_return:
  422.         lw      $ra, 0($sp)
  423.         lw      $s0, 4($sp)
  424.         lw      $s1, 8($sp)
  425.         lw      $s2, 12($sp)
  426.         lw      $s3, 16($sp)
  427.         lw      $s4, 20($sp)
  428.         lw      $s5, 24($sp)
  429.         lw      $s6, 28($sp)
  430.         lw      $s7, 32($sp)
  431.         add     $sp, $sp, 36
  432.         jr      $ra
  433.  
  434. # rule2 #####################################################
  435. #
  436. # argument $a0: pointer to current board
  437. rule2:
  438.     sub $sp, $sp, 4                       #Store ra onto stack and initialize GRIDSIZE
  439.     sw $ra, 0($sp)
  440.     li $t0, GRIDSIZE                               # GRIDSIZE
  441.     li $t1, 1
  442.     sll $t1, $t1, $t0
  443.     subu $t1, $t1, 1                         #int ALL_VALUES = (1 << GRIDSIZE) - 1;
  444.     li $v0, 0                               #bool changed = false
  445.     li $t2, 0                               #i = 0
  446. rule2iloopstart:
  447.     bge $t2, $t0, rule2iloopend
  448.     li $t3, 0                               #j = 0
  449.     rule2jloopstart:
  450.         bge $t3, $t0, rule2jloopend
  451.        
  452.         mul $t4, $t2, $t0
  453.         add $t4, $t4, $t3
  454.         mul $t4, $t4, 2                     #sizeof(unsigned short)*(i*GRIDSIZE + j)
  455.         add $t4, $a0, $t4                   #address of board[i*GRIDSIZE+j]
  456.         lhu $t4, 0($t4)                     #board[i*GRIDSIZE + j]
  457.        
  458.         sub $sp, $sp, 24                    # Allocate stack
  459.         sw $a0, 0($sp)
  460.         sw $t0, 4($sp)
  461.         sw $t1, 8($sp)
  462.         sw $t2, 12($sp)
  463.         sw $t3, 16($sp)
  464.         sw $v0, 20($sp)                     #Store all necessary variables on stack
  465.         move $a0, $t4
  466.         jal has_single_bit_set
  467.         lw $a0, 0($sp)
  468.         lw $t0, 4($sp)
  469.         lw $t1, 8($sp)
  470.         lw $t2, 12($sp)
  471.         lw $t3, 16($sp)
  472.         move $t4, $v0                       # Save $v0 into $t4
  473.         lw $v0, 20($sp)                     # Restore variables
  474.         add $sp, $sp, 24                    # Deallocate stack
  475.  
  476.         bne $t4, $0, rule2continuestatement #if (has_single_bit_set(value)) continue;
  477.        
  478.         li $t5, 0                           #isum = 0
  479.         li $t6, 0                           #jsum = 0
  480.         li $t4, 0                           #k = 0, t2 = i, t3 = j, t4 = k
  481.         rule2kloopstart:
  482.             bge $t4, $t0, rule2kloopend
  483.             beq $t4, $t3, rule2kequalsj
  484.                 mul $t7, $t2, $t0           #i*GRIDSIZE
  485.                 add $t7, $t7, $t4           #i*GRIDSIZE+k
  486.                 mul $t7, $t7, 2
  487.                 add $t7, $a0, $t7           #&board[i*GRIDSIZE + k]
  488.                 lhu $t7, 0($t7)
  489.                 or $t6, $t6, $t7            #jsum |= board[i*GRIDSIZE + k];
  490.         rule2kequalsj:
  491.             beq $t4, $t2, rule2kequalsi    
  492.                 mul $t7, $t4, $t0           #k*GRIDSIZE
  493.                 add $t7, $t7, $t3           #k*GRIDSIZE+j
  494.                 mul $t7, $t7, 2
  495.                 add $t7, $a0, $t7           #&board[k*GRIDSIZE + j]
  496.                 lhu $t7, 0($t7)
  497.                 or $t5, $t5, $t7            #isum |= board[k*GRIDSIZE + j];
  498.         rule2kequalsi:
  499.             add $t4, $t4, 1
  500.             j rule2kloopstart
  501.         rule2kloopend:
  502.         beq $t1, $t6, rule2allvalequalsjsum
  503.             not $t6, $t6                    # ~jsum
  504.             and $t6, $t1, $t6               #ALL_VALUES & ~jsum
  505.             mul $t7, $t0, $t2               # i*GRIDSIZE
  506.             add $t7, $t7, $t3               #[i*GRIDSIZE+j]
  507.             mul $t7, $t7, 2                 #(i*GRIDSIZE+j)*sizeof(unsigned short)
  508.             add $t7, $a0, $t7
  509.             sh $t6, 0($t7)                  #board[i*GRIDSIZE + j] = ALL_VALUES & ~jsum;
  510.             li $v0, 1
  511.             j rule2continuestatement
  512.         rule2allvalequalsjsum:
  513.         beq $t1, $t5, rule2continuestatement
  514.             not $t5, $t5                    # ~isum
  515.             and $t5, $t1, $t5               #ALL_VALUES & ~isum;
  516.             mul $t7, $t0, $t2               # i*GRIDSIZE
  517.             add $t7, $t7, $t3               #[i*GRIDSIZE+j]
  518.             mul $t7, $t7, 2                 #(i*GRIDSIZE+j)*sizeof(unsigned short)
  519.             add $t7, $a0, $t7
  520.             sh $t5, 0($t7)                  #board[i*GRIDSIZE + j] = ALL_VALUES & ~isum;
  521.             li $v0, 1
  522.     rule2continuestatement:
  523.         add $t3, $t3, 1
  524.         j rule2jloopstart                   #continue; iterates to next index of jloop
  525.     rule2jloopend:
  526.     add $t2, $t2, 1
  527.     j rule2iloopstart
  528. rule2iloopend:
  529.  
  530.     lw $ra, 0($sp)
  531.     add $sp, $sp, 4
  532.     jr $ra
  533.  
  534.  
  535. # board done ##################################################
  536. #
  537. # argument $a0: pointer to current board to check
  538. # argument $a1: pointer to puzzle struct
  539. board_done:
  540.     sub $sp, $sp, 36
  541.     sw  $ra, 0($sp)
  542.     sw  $s0, 4($sp)
  543.     sw  $s1, 8($sp)
  544.     sw  $s2, 12($sp)
  545.     sw  $s3, 16($sp)
  546.     sw  $s4, 20($sp)
  547.     sw  $s5, 24($sp)
  548.     sw  $s6, 28($sp)
  549.     sw  $s7, 32($sp)
  550.    
  551.     move    $s0, $a0        # s0 = current_board
  552.     move    $s1, $a1        # s1 = puzzle
  553.     li  $s2, GRIDSIZE              # s2 = GRIDSIZE
  554.     li  $t0, 1
  555.     sll $t0, $t0, $s2       # 1 << GRIDSIZE
  556.     sub $s3, $t0, 1     # s3 = ALL_VALUES = (1 << GRIDSIZE) - 1
  557.    
  558.     li  $s4, 0          # s4 = i = 0
  559. bd_i1_loop_start:
  560.     bge $s4, $s2, bd_i1_loop_end    # !(i < GRIDSIZE)
  561. bd_i1_loop_body:
  562.     li  $s5, 0          # s5 = acc = 0
  563.     li  $s6, 0          # s6 = j = 0
  564. bd_j1_loop_start:
  565.     bge $s6, $s2, bd_j1_loop_end    # !(j < GRIDSIZE)
  566. bd_j1_loop_body:
  567.     mul $t0, $s4, $s2       # i*GRIDSIZE
  568.     add $t0, $t0, $s6       # i*GRIDSIZE + j
  569.     mul $t0, $t0, 2         # sizeof(unsigned short)*(i*GRIDSIZE + j)
  570.     add $t0, $s0, $t0       # &current_board[i*GRIDSIZE + j]
  571.     lhu $s7, 0($t0)         # s7 = value = current_board[i*GRIDSIZE + j]
  572.    
  573.     move    $a0, $s7
  574.     jal has_single_bit_set
  575.     beq $v0, $0, bd_j1_loop_increment   # if (!hsbs(value)) continue
  576.     xor $s5, $s5, $s7
  577.    
  578. bd_j1_loop_increment:
  579.     add $s6, $s6, 1     # ++ j
  580.     j   bd_j1_loop_start
  581. bd_j1_loop_end:
  582.     bne $s5, $s3, bd_return_false   # if (acc != ALL_VALUES) return false
  583.    
  584.     li  $s5, 0          # s5 = acc = 0
  585.     li  $s6, 0          # s6 = j = 0
  586. bd_j2_loop_start:
  587.     bge $s6, $s2, bd_j2_loop_end    # !(j < GRIDSIZE)
  588. bd_j2_loop_body:
  589.     mul $t0, $s6, $s2       # j*GRIDSIZE
  590.     add $t0, $t0, $s4       # j*GRIDSIZE + i
  591.     mul $t0, $t0, 2
  592.     add $t0, $s0, $t0       # &current_board[j*GRIDSIZE + i]
  593.     lhu $s7, 0($t0)     # s7 = value = current_board[j*GRIDSIZE + i]
  594.    
  595.     move    $a0, $s7
  596.     jal has_single_bit_set
  597.     beq $v0, $0, bd_j2_loop_increment   # if (!hsbs(value)) continue
  598.     xor $s5, $s5, $s7
  599.    
  600. bd_j2_loop_increment:
  601.     add $s6, $s6, 1     # ++ j
  602.     j   bd_j2_loop_start
  603. bd_j2_loop_end:
  604.     bne $s5, $s3, bd_return_false   # if (acc != ALL_VALUES) return false
  605.    
  606. bd_i1_loop_increment:
  607.     add $s4, $s4, 1     # ++ i
  608.     j   bd_i1_loop_start
  609. bd_i1_loop_end:
  610.     li  $s4, 0          # s4 = i = 0
  611. bd_i2_loop_start:
  612.     bge $s4, $s2, bd_i2_loop_end    # !(i < GRIDSIZE)
  613. bd_i2_loop_body:
  614.     li  $t0, 2          # sizeof(short)
  615.     mul $t0, $t0, $s2
  616.     mul $t0, $t0, $s2       # sizeof(unsigned short board[GRIDSIZE*GRIDSIZE])
  617.     add $s3, $s1, $t0       # s3 = &(puzzle->constraints)
  618.    
  619.     add $t0, $s4, 1     # i+1
  620.     add $t1, $s2, 2     # GRIDSIZE+2
  621.     mul $t0, $t0, $t1       # (i+1)*(GRIDSIZE+2)
  622.     mul $t0, $t0, 2
  623.     add $t0, $t0, $s3       # &puzzle->constraints[(i+1)*(GRIDSIZE+2) + 0]
  624.     lhu $t9, 0($t0)     # t9 = left_constraint = puzzle->constraints[(i+1)*(GRIDSIZE+2) + 0]
  625.     li  $s5, 0          # s5 = count = 0
  626.     li  $s6, 0          # s6 = last = 0
  627.    
  628.     li  $s7, 0          # s7 = j = 0
  629. bd_j3_loop_start:
  630.     bge $s7, $s2, bd_j3_loop_end    # !(j < GRIDSIZE)
  631. bd_j3_loop_body:
  632.     mul $t0, $s4, $s2       # i*GRIDSIZE
  633.     add $t0, $t0, $s7       # i*GRIDSIZE + j
  634.     mul $t0, $t0, 2
  635.     add $t0, $s0, $t0       # &current_board[i*GRIDSIZE + j]
  636.     lhu $t0, 0($t0)     # t0 = current = current_board[i*GRIDSIZE + j]
  637.     ble $t0, $s6, bd_j3_loop_increment  # !(current > last)
  638.     add $s5, $s5, 1     # count += 1
  639.     move    $s6, $t0        # last = current
  640. bd_j3_loop_increment:
  641.     add $s7, $s7, 1     # ++ j
  642.     j   bd_j3_loop_start
  643. bd_j3_loop_end:
  644.     bne $s5, $t9, bd_return_false   # if (count != left_constraint) return false
  645.    
  646.     add $t0, $s4, 1     # i+1
  647.     add $t1, $s2, 2     # GRIDSIZE+2
  648.     mul $t0, $t0, $t1       # (i+1)*(GRIDSIZE+2)
  649.     add $t0, $t0, $s2       # (i+1)*(GRIDSIZE+2) + GRIDSIZE
  650.     add $t0, $t0, 1     # (i+1)*(GRIDSIZE+2) + GRIDSIZE + 1
  651.     mul $t0, $t0, 2
  652.     add $t0, $t0, $s3       # &puzzle->constraints[(i+1)*(GRIDSIZE+2) + GRIDSIZE + 1]
  653.     lhu $t9, 0($t0)     # t9 = right_constraint = puzzle->constraints[(i+1)*(GRIDSIZE+2) + GRIDSIZE + 1]
  654.     li  $s5, 0          # s5 = count = 0
  655.     li  $s6, 0          # s6 = last = 0
  656.    
  657.     sub $s7, $s2, 1     # s7 = j = GRIDSIZE - 1
  658. bd_j4_loop_start:
  659.     blt $s7, $0, bd_j4_loop_end # !(j >= 0)
  660. bd_j4_loop_body:
  661.     mul $t0, $s4, $s2       # i*GRIDSIZE
  662.     add $t0, $t0, $s7       # i*GRIDSIZE + j
  663.     mul $t0, $t0, 2
  664.     add $t0, $s0, $t0       # &current_board[i*GRIDSIZE + j]
  665.     lhu $t0, 0($t0)     # t0 = current = current_board[i*GRIDSIZE + j]
  666.     ble $t0, $s6, bd_j4_loop_increment  # !(current > last)
  667.     add $s5, $s5, 1     # count += 1
  668.     move    $s6, $t0        # last = current
  669. bd_j4_loop_increment:
  670.     sub $s7, $s7, 1     # -- j
  671.     j   bd_j4_loop_start
  672. bd_j4_loop_end:
  673.     bne $s5, $t9, bd_return_false   # if (count != right_constraint) return false
  674.     add $t0, $s4, 1     # i+1
  675.     mul $t0, $t0, 2
  676.     add $t0, $t0, $s3       # &puzzle->constraints[i + 1]
  677.     lhu $t9, 0($t0)     # t9 = top_constraint = puzzle->constraints[i + 1]
  678.     li  $s5, 0          # s5 = count = 0
  679.     li  $s6, 0          # s6 = last = 0
  680.    
  681.     li  $s7, 0          # s7 = j = 0
  682. bd_j5_loop_start:
  683.     bge $s7, $s2, bd_j5_loop_end    # !(j < GRIDSIZE)
  684. bd_j5_loop_body:
  685.     mul $t0, $s7, $s2       # j*GRIDSIZE
  686.     add $t0, $t0, $s4       # j*GRIDSIZE + i
  687.     mul $t0, $t0, 2
  688.     add $t0, $s0, $t0       # &current_board[j*GRIDSIZE + i]
  689.     lhu $t0, 0($t0)     # t0 = current = current_board[j*GRIDSIZE + i]
  690.     ble $t0, $s6, bd_j5_loop_increment  # !(current > last)
  691.     add $s5, $s5, 1     # count += 1
  692.     move    $s6, $t0        # last = current
  693. bd_j5_loop_increment:
  694.     add $s7, $s7, 1     # ++ j
  695.     j   bd_j5_loop_start
  696. bd_j5_loop_end:
  697.     bne $s5, $t9, bd_return_false   # if (count != top_constraint) return false
  698.    
  699.     add $t0, $s2, 1     # GRIDSIZE+1
  700.     add $t1, $s2, 2     # GRIDSIZE+2
  701.     mul $t0, $t0, $t1       # (GRIDSIZE+1)*(GRIDSIZE+2)
  702.     add $t0, $t0, $s4       # (GRIDSIZE+1)*(GRIDSIZE+2) + i
  703.     add $t0, $t0, 1     # (GRIDSIZE+1)*(GRIDSIZE+2) + i + 1
  704.     mul $t0, $t0, 2
  705.     add $t0, $t0, $s3       # &puzzle->constraints[(GRIDSIZE+1)*(GRIDSIZE+2) + i + 1]
  706.     lhu $t9, 0($t0)     # t9 = bottom_constraint = puzzle->constraints[(GRIDSIZE+1)*(GRIDSIZE+2) + i + 1]
  707.     li  $s5, 0          # s5 = count = 0
  708.     li  $s6, 0          # s6 = last = 0
  709.    
  710.     sub $s7, $s2, 1     # s7 = j = GRIDSIZE - 1
  711. bd_j6_loop_start:
  712.     blt $s7, $0, bd_j6_loop_end # !(j >= 0)
  713. bd_j6_loop_body:
  714.     mul $t0, $s7, $s2       # j*GRIDSIZE
  715.     add $t0, $t0, $s4       # j*GRIDSIZE + i
  716.     mul $t0, $t0, 2
  717.     add $t0, $s0, $t0       # &current_board[j*GRIDSIZE + i]
  718.     lhu $t0, 0($t0)     # t0 = current = current_board[j*GRIDSIZE + i]
  719.     ble $t0, $s6, bd_j6_loop_increment  # !(current > last)
  720.     add $s5, $s5, 1     # count += 1
  721.     move    $s6, $t0        # last = current
  722. bd_j6_loop_increment:
  723.     sub $s7, $s7, 1     # -- j
  724.     j   bd_j6_loop_start
  725. bd_j6_loop_end:
  726.     bne $s5, $t9, bd_return_false   # if (count != bottom_constraint) return false
  727. bd_i2_loop_increment:
  728.     add $s4, $s4, 1
  729.     j   bd_i2_loop_start
  730. bd_i2_loop_end:
  731.     li  $v0, 1          # return true
  732.     j   bd_return
  733. bd_return_false:
  734.     li  $v0, 0          # return false
  735. bd_return:
  736.     lw  $ra, 0($sp)
  737.     lw  $s0, 4($sp)
  738.     lw  $s1, 8($sp)
  739.     lw  $s2, 12($sp)
  740.     lw  $s3, 16($sp)
  741.     lw  $s4, 20($sp)
  742.     lw  $s5, 24($sp)
  743.     lw  $s6, 28($sp)
  744.     lw  $s7, 32($sp)
  745.     add $sp, $sp, 36
  746.     jr $ra
  747.  
  748. # has single bit set ###########################################
  749. #
  750. # argument $a0: bit mask
  751. has_single_bit_set:
  752.     beq     $a0, $0, has_single_bit_set_iszero
  753.     sub     $v0, $a0, 1             # $v0: b-1
  754.     and     $v0, $a0, $v0           # $v0: b & (b-1)
  755.     not     $v0, $v0                # $v0: !(b & (b-1))
  756.     # if $v0 is zero, return zero
  757.     bne     $v0, -1, has_single_bit_set_iszero
  758.     li      $v0, 1
  759.     j       has_single_bit_set_done
  760. has_single_bit_set_iszero:
  761.     li      $v0, 0
  762. has_single_bit_set_done:
  763.     jr      $ra
  764.  
  765.  
  766.  
  767. # increment heap ###############################################
  768. #
  769. # argument $a0: pointer to current board to check
  770. increment_heap:
  771.     sub $sp, $sp, 4
  772.     sw  $ra, 0($sp) # save $ra on stack
  773.  
  774.     li  $t0, GRIDSIZE
  775.     mul $t0, $t0, $t0               # GRIDSIZE * GRIDSIZE
  776.     mul $a1, $t0, 2
  777.     add $a1, $a0, $a1               # new_board = old_board + GRIDSIZE*GRIDSIZE
  778.  
  779.     jal copy_board
  780.  
  781.     move $v0, $v0                   # // output the output of copy_board
  782.     lw  $ra, 0($sp)
  783.     add $sp, $sp, 4
  784.     jr $ra
  785.  
  786.  
  787. # copy board ###################################################
  788. #
  789. # argument $a0: pointer to old board
  790. # argument $a1: pointer to new board
  791. copy_board:
  792.     li  $t0, GRIDSIZE
  793.     mul $t0, $t0, $t0               # GRIDSIZE * GRIDSIZE
  794.     li  $t1, 0                      # i = 0
  795. ih_loop:
  796.     bge $t1, $t0, ih_done           # i < GRIDSIZE*GRIDSIZE
  797.  
  798.     mul $t2, $t1, 2                 # i * sizeof(unsigned short)
  799.     add $t3, $a0, $t2               # &old_board[i]
  800.     lhu $t3, 0($t3)                 # old_board[i]
  801.  
  802.     add $t4, $a1, $t2               # &new_board[i]
  803.     sh  $t3, 0($t4)                 # new_board[i] = old_board[i]
  804.  
  805.     addi $t1, $t1, 1                # i++
  806.     j    ih_loop
  807. ih_done:
  808.     move $v0, $a1
  809.     jr $ra
  810.  
  811. .kdata
  812. chunkIH:    .space 32
  813. non_intrpt_str:    .asciiz "Non-interrupt exception\n"
  814. unhandled_str:    .asciiz "Unhandled interrupt type\n"
  815. .ktext 0x80000180
  816. interrupt_handler:
  817. .set noat
  818.         move      $k1, $at        # Save $at
  819. .set at
  820.         la        $k0, chunkIH
  821.         sw        $a0, 0($k0)        # Get some free registers
  822.         sw        $v0, 4($k0)        # by storing them to a global variable
  823.         sw        $t0, 8($k0)
  824.         sw        $t1, 12($k0)
  825.         sw        $t2, 16($k0)
  826.         sw        $t3, 20($k0)
  827.         sw $t4, 24($k0)
  828.         sw $t5, 28($k0)
  829.  
  830.         mfc0      $k0, $13             # Get Cause register
  831.         srl       $a0, $k0, 2
  832.         and       $a0, $a0, 0xf        # ExcCode field
  833.         bne       $a0, 0, non_intrpt
  834.  
  835.  
  836.  
  837. interrupt_dispatch:            # Interrupt:
  838.     mfc0       $k0, $13        # Get Cause register, again
  839.     beq        $k0, 0, done        # handled all outstanding interrupts
  840.  
  841.     and        $a0, $k0, BONK_INT_MASK    # is there a bonk interrupt?
  842.     bne        $a0, 0, bonk_interrupt
  843.  
  844.     and        $a0, $k0, TIMER_INT_MASK    # is there a timer interrupt?
  845.     bne        $a0, 0, timer_interrupt
  846.  
  847.     and     $a0, $k0, REQUEST_PUZZLE_INT_MASK
  848.     bne     $a0, 0, request_puzzle_interrupt
  849.  
  850.     li        $v0, PRINT_STRING    # Unhandled interrupt types
  851.     la        $a0, unhandled_str
  852.     syscall
  853.     j    done
  854.  
  855. bonk_interrupt:
  856.     sw      $0, BONK_ACK
  857.     # Fill in your code here
  858.  
  859.     li $t0, 270
  860.     # sw $t0, ANGLE($0)
  861.  
  862.     li $t0, 0
  863.     # sw $t0, ANGLE_CONTROL($0)
  864.  
  865.     # sw $v0, VELOCITY($0)
  866.  
  867.     # set paint to 1 again
  868.     add $v0, $0, 1
  869.     sw $v0, SWITCH_MODE($0)
  870.  
  871.     j       interrupt_dispatch    # see if other interrupts are waiting
  872.  
  873. request_puzzle_interrupt:
  874.     sw      $0, REQUEST_PUZZLE_ACK
  875.     # Fill in your code here
  876.     # move the puzzle data somewhere
  877.  
  878.     li $t0, 1 # tell them that the puzzle is ready
  879.     # la $t1, PUZZLE_FLAG
  880.     sw $t0, PUZZLE_FLAG($0)
  881.    
  882.     j   interrupt_dispatch
  883.  
  884. timer_interrupt:
  885.     sw      $0, TIMER_ACK
  886.     #Fill in your code here
  887.     j        interrupt_dispatch    # see if other interrupts are waiting
  888.  
  889. non_intrpt:                # was some non-interrupt
  890.     li        $v0, PRINT_STRING
  891.     la        $a0, non_intrpt_str
  892.     syscall                # print out an error message
  893.     # fall through to done
  894.  
  895. done:
  896.     la      $k0, chunkIH
  897.     lw      $a0, 0($k0)        # Restore saved registers
  898.     lw      $v0, 4($k0)
  899.     lw      $t0, 8($k0)
  900.     lw      $t1, 12($k0)
  901.     lw      $t2, 16($k0)
  902.     lw      $t3, 20($k0)
  903.     lw $t4, 24($k0)
  904.     lw $t5, 28($k0)
  905. .set noat
  906.     move    $at, $k1        # Restore $at
  907. .set at
  908.     eret
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top