简体   繁体   中英

MIPS storing bytes from an immediate into an array of bytes

I am trying to make a recursive sudoku solver in MIPS assembly. We have a mars tool that stores byte for each cell in the addresses 0xFFFF8000, 0xFFFF8001, and so on. I am trying to store a byte saved in each address into an array but I'm not sure how. This is what I tried but it doenst compile at the sb call saying it's out of bounds. Code is very unfinished; _printBoard just prints the board to console; although end goal is to display the board back to the mars tool by saving the solved bytes back into the addresses I got the unsolved board from.

    .data
        newLine:    .asciiz "\n"
        threeLines:     .asciiz "\n\n\n"
        buffer:     .space 100
        array:      .byte

    .text
    _startProgram:
        jal _printBoard #prints unfinished board

    _sudokuSolver:
        la   $a0, threeLines
        addi $v0, $zero, 4
        syscall # print new Line
        li $t0, 0xFFFF8000
        lb $t1, array
        sb $t0, 0($t1) # DOESNT COMPILE
        li $v0, 1
        add $a0, $t1, $zero 
        syscall #prints int in $t0
        jal _printBoard #prints solved board

Ok so I solved my problem; however since I am a beginner there may be a better or alternative way to do this.

so in .data I declared the array

 array:.byte        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
       .byte        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
       .byte        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

which holds exactly enough spots for the values I want to store (81 in total). Then I created a simple loop where I loaded the address of the first value I wanted and the first value in the array I want to save it into.

li $t0, 0xFFFF8000
la $t1, array

and the loop contained

lb $t2, 0($t0)
sb $t2, 0($t1)

Added t0 and t1 by 1 in the loop to increment the position and broke the loop after it had saved the value 81 times. lb loads the byte I want in 0xFFFF8000 ($t0) and sb saves it in the desired address in array ($t1).

And when I wanted to save the solved data back to the 0xFFFF8000 and so on addresses/immediates (don't know how to refer to this differently) You just reversed what was being loaded/saved. I couldn't do a loop for it for some reason so I just hard coded the saving which is probably not very efficient so I probably will post another question about how that works since I'm curious why my loop wouldn't work for that instance.

Don't want to post the whole code since this was for a class; I only asked since I wasn't sure how to deal with immediates and saving them to an array. Turns out I just needed to not think of it that differently from a buffer/string and I was able to figure it out pretty easily. At the time was just really confused why I was getting an out of bounds error with what I had before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM