I’m trying to make a 2d array, store numbers then print them in MIPS assembly.
Here is the code to store:
initialise_game__body:
li $t1, 0 # int row = 0;
li $t4, 0 # int i = 0;
lw $t0, grid_width # $t0 = grid_width;
bricks_loop_row:
bge $t1, GRID_HEIGHT, bricks_loop_row_end # for (int row = 0; row < GRID_HEIGHT; {
li $t2, 0 # int col = 0;
bricks_loop_col:
bge $t2, $t0, bricks_loop_col_end # for (int col = 0; col < grid_width; {
blt $t1, BRICK_ROW_START, row_top_or_bot # if not (BRICK_ROW_START <= row
bgt $t1, BRICK_ROW_END, row_top_or_bot # && row <= BRICK_ROW_END) {
li $t3, 0 # $t3 = 0;
mul $t2, $t2, 4
mul $t1, $t1, 4
mul $t1, $t1, $t0
add $t1, $t1, $t2
sw $t3, bricks($t1) # bricks[row][col] = $t3;
sub $t1, $t1, $t2
div $t1, $t1, $t0
div $t1, $t1, 4
div $t2, $t2, 4
addi $t2, $t2, 1 # col++)
j bricks_loop_col # }
row_top_or_bot: # else{
div $t3, $t2, BRICK_WIDTH # $t3 = col / BRICK_WIDTH;
rem $t3, $t3, 10 # $t3 = $t3 % 10;
addi $t3, $t3, 1 # $t3 = $t3 + 1;
mul $t2, $t2, 4
mul $t1, $t1, 4
mul $t1, $t1, $t0
add $t1, $t1, $t2
sw $zero, bricks($t1) # bricks[row][col] = 0;
sub $t1, $t1, $t2
div $t1, $t1, $t0
div $t1, $t1, 4
div $t2, $t2, 4
addi $t2, $t2, 1 # col++
j bricks_loop_col # }
bricks_loop_col_end: # }
addi $t1, $t1, 1 # row++)
j bricks_loop_row
where $t0, grid_width, is the scanned input which is between 6 and 60, and is multiple of 3.
The array bricks is:
bricks: .byte 0:GRID_HEIGHT*MAX_GRID_WIDTH
and the below is the code for printing:
print_game__prologue:
begin
push $ra
push $s0
push $s1
push $s2
print_game__body:
lw $t0, score # $t0 = score;
li $s0, -1 # int row = -1;
lw $s2, grid_width # $s2 = grid_width;
la $a0, str_print_game_score # printf(" SCORE:
li $v0, 4
syscall
move $a0, $t0 # score
li $v0, 1
syscall
la $a0, 'n' # n");
li $v0, 11
syscall
print_game_row:
li $s1, -1 # int col = -1;
bge $s0, GRID_HEIGHT, print_game_end
print_game_col:
bgt $s1, $s2, print_game_col_end
beq $s0, -1, top_grid # if (row == -1)
beq $s1, -1, side_grid # else if (col == -1
beq $s1, $s2, side_grid # || col == grid_width)
move $a0, $s0
move $a1, $s1
jal print_cell
move $t1, $v0
move $a0, $t1 # putchar(print_cell(row, col));
li $v0, 11
syscall
add $s1, $s1, 1 # col++;
j print_game_col
print_game_col_end:
add $s0, $s0, 1 # row++;
la $a0, 'n' # putchar('n');
li $v0, 11
syscall
j print_game_row
top_grid:
la $a0, GRID_TOP_CHAR # putchar(GRID_TOP_CHAR);
li $v0, 11
syscall
add $s1, $s1, 1 # col++;
j print_game_col
side_grid:
la $a0, GRID_SIDE_CHAR # putchar(GRID_SIDE_CHAR);
li $v0, 11
syscall
add $s1, $s1, 1 # col++;
j print_game_col
print_game_end:
print_game__epilogue:
pop $s2
pop $s1
pop $s0
pop $ra
end
jr $ra
print_cell:
print_cell__prologue:
begin
push $ra
push $s0
push $s1
push $s2
push $s3
print_cell__body:
move $s0, $a0
move $a0, $s0
move $s1, $a1
move $a1, $s1
jal count_balls_at_coordinate
move $s2, $v0
bgt $s2, 1, count_greater
beq $s2, 1, count_equal
bne $s0, PADDLE_ROW, not_empty_cell
lw $s3, paddle_x
bgt $s3, $s1, not_empty_cell
add $s3, $s3, PADDLE_WIDTH
bge $s1, $s3, not_empty_cell
li $v0, PADDLE_CHAR
j print_cell__epilogue
count_greater:
li $v0, MANY_BALL_CHAR
j print_cell__epilogue
count_equal:
li $v0, ONE_BALL_CHAR
j print_cell__epilogue
not_empty_cell:
lw $t0, grid_width
mul $s0, $s0, 4
mul $s1, $s1, 4
mul $s0, $s0, $t0
add $s0, $s0, $s1
lw $s3, bricks($s0) # $s3 = bricks[row][col];
beqz $s3, empty_cell
sub $s3, $s3, 1
move $v0, $s3
j print_cell__epilogue
empty_cell:
li $v0, EMPTY_CHAR
print_cell__epilogue:
pop $s3
pop $s2
pop $s1
pop $s0
pop $ra
end
jr $ra
I am expecting to have something like this when the input is 12:
SCORE: 0
==============
| |
| |
|000111222333|
|000111222333|
|000111222333|
|000111222333|
|000111222333|
|000111222333|
| |
| |
| * |
| ------ |
but what I’m actually getting is:
SCORE: 0
==============
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| ------ |
So it’s missing the numbers, which should be stored in the array bricks.
Which part did it go wrong?
ivegal op is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.