1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| .data puzzle:.space 256 # 8 * 8 * 4 vis:.space 256
.macro done li $v0, 10 syscall .end_macro
.macro push(%x) subi $sp, $sp, 4 sw %x, 0($sp) .end_macro
.macro pop(%x) lw %x, 0($sp) addi $sp, $sp, 4 .end_macro
.macro inputInt(%int) li $v0, 5 syscall move %int, $v0 .end_macro
.macro printInt(%int) li $v0, 1 move $a0, %int syscall .end_macro
.macro printStr(%str) li $v0, 4 la $a0, %str syscall .end_macro
.macro for_begin(%startLabel, %endLabel, %i, %start, %end) move %i, %start %startLabel: beq %i, %end, %endLabel .end_macro
.macro for_end(%startLabel, %endLabel, %i) addi %i, %i, 1 j %startLabel %endLabel: .end_macro
.macro load_arr(%arr, %i, %k, %value) sll $t9, %i, 3 add $t9, $t9, %k sll $t9, $t9, 2 lw %value, %arr($t9) .end_macro
.macro store_arr(%arr, %i, %k, %value) sll $t9, %i, 3 add $t9, $t9, %k sll $t9, $t9, 2 sw %value, %arr($t9) .end_macro
.eqv n, $s0 .eqv m, $s1 .eqv start_x, $s2 .eqv start_y, $s3 .eqv end_x, $s4 .eqv end_y, $s5 .eqv ans, $s6 .eqv one, $s7
.eqv i, $t0 .eqv k, $t1 .eqv temp, $t2 .eqv curr_x, $t3 .eqv curr_y, $t4 .eqv temp1, $t5
.text li one, 1 inputInt(n) inputInt(m) for_begin(start1, end1, i, $0, n) for_begin(start2, end2, k, $0, m) inputInt(temp) store_arr(puzzle, i, k, temp) for_end(start2, end2, k) for_end(start1, end1, i) inputInt(start_x) inputInt(start_y) inputInt(end_x) inputInt(end_y) subi start_x, start_x, 1 subi start_y, start_y, 1 subi end_x, end_x, 1 subi end_y, end_y, 1 move curr_x, start_x move curr_y, start_y store_arr(vis, curr_x, curr_y, one) jal solve
printInt(ans) done solve: push($ra) bne curr_x, end_x, skip bne curr_y, end_y, skip addi ans, ans, 1 pop($ra) jr $ra skip: beqz curr_x, brk1 subi temp1, curr_x, 1 load_arr(vis, temp1, curr_y, temp) beq temp, one, brk1 load_arr(puzzle, temp1, curr_y, temp) beq temp, one, brk1 store_arr(vis, temp1, curr_y, one) push(curr_x) push(curr_y) subi curr_x, curr_x, 1 jal solve pop(curr_y) pop(curr_x) subi temp1, curr_x, 1 store_arr(vis, temp1, curr_y, $0) brk1: addi temp1, curr_y, 1 beq temp1, m, brk2 load_arr(vis, curr_x, temp1, temp) beq temp, one, brk2 load_arr(puzzle, curr_x, temp1, temp) beq temp, one, brk2 store_arr(vis, curr_x, temp1, one) push(curr_x) push(curr_y) addi curr_y, curr_y, 1 jal solve pop(curr_y) pop(curr_x) addi temp1, curr_y, 1 store_arr(vis, curr_x, temp1, $0) brk2: addi temp1, curr_x, 1 beq temp1, n, brk3 load_arr(vis, temp1, curr_y, temp) beq temp, one, brk3 load_arr(puzzle, temp1, curr_y, temp) beq temp, one, brk3 store_arr(vis, temp1, curr_y, one) push(curr_x) push(curr_y) addi curr_x, curr_x, 1 jal solve pop(curr_y) pop(curr_x) addi temp1, curr_x, 1 store_arr(vis, temp1, curr_y, $0) brk3: beqz curr_y, brk4 subi temp1, curr_y, 1 load_arr(vis, curr_x, temp1, temp) beq temp, one, brk4 load_arr(puzzle, curr_x, temp1, temp) beq temp, one, brk4 store_arr(vis, curr_x, temp1, one) push(curr_x) push(curr_y) subi curr_y, curr_y, 1 jal solve pop(curr_y) pop(curr_x) subi temp1, curr_y, 1 store_arr(vis, curr_x, temp1, $0) brk4: pop($ra) jr $ra
|