Вы находитесь на странице: 1из 4

.

text
.text
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
);
##
##
##
##
##
##

struct Puzzle {
int size;
Cell *grid;
};
// Given the assignment at current position, removes all inconsistent values
// for cells in the same row, column, and cage.
int
forward_checking(int position, Puzzle *puzzle) {
int size = puzzle->size;
// Removes inconsistent values in the row.
for (int col = 0; col < size; col++) {
if (col != position % size) {
int row_pos = position / size * size + col;
puzzle->grid[row_pos].domain &= ~puzzle->grid[position].domain;
if (!puzzle->grid[row_pos].domain) {
return 0;
}
}
}
// Removes inconsistent values in the column.
for (int row = 0; row < size; row++) {
if (row != position / size) {
int col_pos = row * size + position % size;
puzzle->grid[col_pos].domain &= ~puzzle->grid[position].domain;
if (!puzzle->grid[col_pos].domain) {
return 0;
}
}
}
// Removes inconsistent values in the cage.
for (int i = 0; i < puzzle->grid[position].cage->num_cell; i++) {
int cage_pos = puzzle->grid[position].cage->positions[i];
puzzle->grid[cage_pos].domain &= get_domain_for_cell(cage_pos, puzzle
if (!puzzle->grid[cage_pos].domain) {
return 0;
}
}
return 1;
}

.globl forward_checking
forward_checking:
# Your code goes here :)
sub
$sp,
sw
$ra,
sw
$s0,
sw
$s1,
sw
$s2,
sw
$s3,
sw
$s4,
sw
$s5,
sw
$s6,
sw
$s7,
move

$sp, 36
0($sp)
4($sp)
8($sp)
12($sp)
16($sp)
20($sp)
24($sp)
28($sp)
32($sp)

$s0, $a0

# $s0 = position

move

$s1, $a1

# $s1 = *puzzle

lw
li

$s2, 0($s1)
$s3, 0

# $s0 = size
# $s3 = col

##already using $s0, $s1, $s2, $s3...


fc_loop:
bge
$s3, $s2, fc_lp_nxt
loop}
div
$s0, $s2
mfhi
$s4
/ size (position % size)
beq
$s3, $s4, fc_lp_skip
ize) {skip body of loop}
mflo
$s4
sition / size
mul
$s4, $s4, $s2
add
$s4, $s4, $s3

# if(col >= size) {skip


# position / size
# remainder of position
# if(col == position % s
# integer quotient of po
# position / size * size
# $s4 = row_pos

lw
e->grid[0]

$s5, 4($s1)

# $s5 = address of puzzl

mul
add
->grid[row_pos]
lw
_pos].domain

$s6, $s4, 8
$s4, $s5, $s6

# row_pos offset
# $s4 = offset of puzzle

$s6, 0($s4)

# $s6 = puzzle->grid[row

mul
add
[position]
lw
ition].domain

$s7, $s0, 8
$s7, $s5, $s7

# position offset
# offset of puzzle->grid

$s7, 0($s7)

# $s7 = puzzle->grid[pos

xor
$s7, $s7, -1
and
$s6, $s6, $s7
sw
$s6, 0($s4)
set of puzzle->grid[row_pos]

# $s7 = ~$s7
# $s6 = $s6 & $s7
# store $s6 into the off

bne
$s6, 0, fc_lp_skip
on].domain) {skip body of if}
li
$v0, 0
j
end

# if(puzzle->grid[positi

fc_lp_skip:
add
j

$s3, $s3, 1
fc_loop

##already using $s0, $s1, $s2....


fc_lp_nxt:
li
$s3, 0
fc_loop_2:
bge
loop}
div
mflo

# col++

# $s3 = row

$s3, $s2, fc_lp_nxt2

# if(row >= size) {skip

$s0, $s2
$s4

# position / size
# integer quotient of po

sition / size
beq
$s3, $s4, fc_lp_skip2
ize) {skip body of loop}
mfhi
$s4
mul
$s5, $s3, $s2
add
$s4, $s5, $s4

# if(row == position / s
# position % size
# row * size
# $s4 = col_pos

lw

$s5, 4($s1)

# $s5 = puzzle->&grid[0]

mul
add

$s6, $s4, 8
$s4, $s5, $s6

# col_pos offset
# $s4 = puzzle->&grid[co

lw
_pos].domain

$s6, 0($s4)

# $s6 = puzzle->grid[col

mul
add
d[position]
lw
ition].domain

$s7, $s0, 8
$s7, $s5, $s7

# position offset
# "index" of puzzle->gri

$s7, 0($s7)

# $s7 = puzzle->grid[pos

xor
and
sw
>&grid[col_pos]

$s7, $s7, -1
$s6, $s6, $s7
$s6, 0($s4)

# $s7 = ~$s7
# $s6 = $s6 & $s7
# store $s6 into puzzle-

l_pos]

bne
$s6, 0, fc_lp_skip2
on].domain) {skip body of if}
li
$v0, 0
j
end
fc_lp_skip2:
add
j

$s3, $s3, 1
fc_loop_2

##already using $s0, $s1, $s2....


fc_lp_nxt2:
li
$s3, 0
fc_loop_3:
lw
$s4, 4($s1)
mul
$s5, $s0, 8
add
$s4, $s4, $s5
]
lw
$s4, 4($s4)
.&cage
lw
$s5, 8($s4)
.cage->num_cell
bge
$s3, $s5, end_
osition].cage->num_cell) {skip loop}
lw
$s4, 12($s4)
.cage->&positions[0]
mul
$s5, $s3, 4
add
$s4, $s4, $s5
].cage->&positions[i]
lw
$s4, 0($s4)
lw
mul

$s5, 4($s1)
$s6, $s4, 8

# if(puzzle->grid[positi

# row++

# $s3 = i
# puzzle->&grid[0]
# offset of position
# puzzle->&grid[position
# puzzle->grid[position]
# puzzle->grid[position]
# if(i >= puzzle->grid[p
# puzzle->grid[position]
# offset of i
# puzzle->grid[positions
# $s4 = cage_pos
# puzzle->&grid[0]
# offset of cage_pos

add

$s5, $s5, $s6

# puzzle->&grid[cage_pos

lw
e_pos].domain

$s5, 0($s5)

# $s5 = puzzle->grid[cag

move
jal
move
move
move
ell(cage_pos, puzzle)
and

$a0, $s4
get_domain_for_cell
$a0, $s0
$a1, $s1
$s6, $v0

# $a0 = cage_pos

$s5, $s5, $s6

# $s5 = $s5 & $s6

bne
$s5, 0, fc_lp_skip3
os].domain) {skip body of if}
li
$v0, 0
j
end
fc_lp_skip3:
add
j

$s3, $s3, 1
fc_loop_3

end_:
li

$v0, 1

lw
lw
lw
lw
lw
lw
lw
lw
lw
add
jr

$ra,
$s0,
$s1,
$s2,
$s3,
$s4,
$s5,
$s6,
$s7,
$sp,
$ra

end:
0($sp)
4($sp)
8($sp)
12($sp)
16($sp)
20($sp)
24($sp)
28($sp)
32($sp)
$sp, 36

# restore $a0
# restore $a1
# $s6 = get_domain_for_c

# if(puzzle->grid[cage_p

# i++