I am trying to convert the following code from C to MIPS
struct Puzzle {
int NUM_ROWS;
int NUM_COLS;
char** board;
};
char floodfill (Puzzle* puzzle, char marker, int row, int col) {
if (row < 0 || col < 0) {
return marker;
}
if (row >= puzzle->NUM_ROWS || col >= puzzle->NUM_COLS) {
return marker;
}
char ** board = puzzle->board;
if (board[row][col] != ’#’) {
return marker;
}
board[row][col] = marker;
floodfill(puzzle, marker, row + 1, col + 1);
return marker + 1;
}
I am accessing char** board by doing one load word get the address of the array of pointers out of the struct, a second load word to get the address of the array from the array of pointers, and a third load word to get the actual data.
To get board[row][col] I am adding 4 * row to where char ** board is stored, loading the data at that address, then adding col to that address.
How do I actually change the value at board[row][col] to marker? When I use sb I get an exception.
Exception occurred at PC=0x0040027c
Unaligned address in store: 0x00000023
PC=0x0040027c is this line sw $a1, 0($s3) # board[row][col] = marker
lw $t0, 8($a0) # lw - address of the array of pointers out of the struct
lw $t0, 0($t0) # char ** board = puzzle->board;
mul $t3, $a2, 4 # t3 = 4 * row
add $t3, $t0, $t3 # t3 = t0 + t3
lbu $s3, 0($t3) # load the data at t3
add $s3, $s3, $a3 # add col to the $s3 address
sb $a1, 0($s3) # board[row][col] = marker;
User contributions licensed under CC BY-SA 3.0