Question

Printing 2D array in MIPS

I have been using a few other posts on here to try and figure out what I am doing wrong but I am stuck and could use some help. I am making a game that has an 8x8 "board" (2d array) similar to what you would see in battleship, with a ship (denoted as a V for now) in the center to start. While stepping through the code one line at a time I found that my code iterated through the inner loop once, made a new line and then skipped over the inner loop to just print a whole bunch of new lines. I tried resetting $t3 to 0 at the start of the outer loop to correct this issue but it didn't work now I have somehow messed it up even more.

Below is my code:

.data
board:  .ascii " ABCDEFG"
    .ascii "1        "
    .ascii "2        "
    .ascii "3        "
    .ascii "4    V   "
    .ascii "5        "
    .ascii "6        "
    .ascii "7        "
    

.text

main:
    
#Build the board
    li  $t0, 8
    li $t1, 8
    la $t6, board
    li $t2, 0   # set i to 0 for outerloop
    li $t3, 0   #set j = 0
    jal, boardOuterLoop #jump to building board loops
    
    
    boardOuterLoop:
    bge $t1, $t2, endOuterLoop # basically...for (int i = 0; i < row; i++) 
    
    li $t3, 0   #reset j = 0 for next ineration through innerloop
    
    boardInnerLoop:
    bge $t3, $t0, endInnerLoop  #  basically...for(int j = 0; j<col; j++)
    #find address of each index
    mul $t4, $t2, $t1       # col* i
        add $t4, $t4, $t3       # (col* i) + j
        sll $t4, $t4, 2         # 2^2 * (width * i + j)
        add $t4, $t6, $t4   #bass add + $t4
    
        addi $t3, $t3, 1    # j++
    
    #print index
    li $v0, 11
    sw  $a0, 0($t4)
        syscall 
        
    b boardInnerLoop    #branch back to beginning of inner loop
    
    endInnerLoop:
    li $v0, 4
        la $a0, newline
        syscall
        
        addi $t2, $t2, 1    #i++
        
    b boardOuterLoop
    
    endOuterLoop:
    jr $ra

while trying to work out printing the board I got the output: '''''''' and nothing more, now as I have been trying to fix that I am getting nothing except new lines. The desired output is:

 ABCDEFG
1
2
3
4   V
5
6
7
 2  30  2
1 Jan 1970

Solution

 0

I figured it out in case anyone else could use the help, however I had to change the number labels to letters as well, couldn't get that to work for me. Big thing was I had .ascii strings instead of .word characters so when I tried to print characters it was trying to print the whole string.

.data board: .word ' ','A','B','C','D','E','F','G', '\n' .word 'A',' ',' ',' ',' ',' ',' ',' ', '\n' .word 'B',' ',' ',' ',' ',' ',' ',' ', '\n' .word 'C',' ',' ',' ',' ',' ',' ',' ', '\n' .word 'D',' ',' ',' ','V',' ',' ',' ', '\n' .word 'E',' ',' ',' ',' ',' ',' ',' ', '\n' .word 'F',' ',' ',' ',' ',' ',' ',' ', '\n' .word 'G',' ',' ',' ',' ',' ',' ',' ', '\n'

.text buildBoard: #Build the board li $t0, 9 #col li $t1, 8 #row la $t6, board li $t2, 0 # set i to 0 for outerloop j boardOuterLoop

boardOuterLoop: bge $t2, $t1, endOuterLoop # basically...for (int i = 0; i < row; i++) li $t3, 0 #reset j = 0 for next ineration through innerloop boardInnerLoop: bge $t3, $t0, endInnerLoop # basically...for(int j = 0; j<col; j++) #find address of each index mul $t4, $t2, $t0 # col* i add $t4, $t4, $t3 # (col* i) + j sll $t4, $t4, 2 # 2^2 * (col * i + j) add $t4, $t6, $t4 #bass add + $t4

#print index li $v0, 11 lb $a0, ($t4) #print the index syscall

      addiu     $t3, $t3, 1     # j++
    
        

endInnerLoop:

    addiu   $t2, $t2, 1     #i++
    
    j   boardOuterLoop

endOuterLoop: jr $ra

2024-07-25
Sara Malos