1

I'm working on a backtracking algorithm in MIPS Assembly and I'm getting lost in the dealing with scope, and structure of the code.

We have a few moving parts here, which I'm only confusing myself more with trying to figure out how to deal with them.

The end goal is a puzzle solver that uses backtracking.

The "moving parts".

We have the address of an array of indexes which have all of the indexes of the puzzle that we need to change. The rest of the board can not be altered.

We have the address of the puzzle board, simply an array of bytes.

We have the total size of the board, we're going to use this to check if we've looked at the entire board to know we're done.

We have a function is_Legal(arg1, arg2). Arg1 is the address of the board (array) to check, arg2 is the size of the board. Returns 0 if it's legal, 1 if not.

So we first load up

   initialize_guessing:
               .. # build up stack
               la   $s0, $a0         # s0 = address of board
               la   $s1, $a1         # s1 = address of index array
               la   $s2, $a2         # s2 = size of index array
               la   $s3, $a3         # s3 = size of board 
               li   $t0, 0           # t0 = values to test
               li   $t1, 0           # t1 = index counter
   start_guessing:
               beq  $t1, $s2, done   # if t1 = s2, we've used all indexes
               lb   $t2, 0($s1)      # get first index 
               add  $s0, $s0, $t2    # move to first index
               sb   $t0, 0($s0)      # put t0 into board
               move $a0, $s0         # a0 = board address for if_legal
               move $a1, $s3         # a1 = board size for if_legal
               jal  if_legal         # check if board is legal
               beq  $v0, $zero, continue_guessing   # if board is legal, continue
               j    new_guess        # try a new guess
   new_guess:
               addi $t0, 1           # increment t0 to try a new value
               jal  start_guessing
   continue_guessing:
               addi $s1, 1           # increment index array to next index
               jal  start_guessing
   done:
              # destroy stack
               jr   $ra

This is what I've got so far, I know it's incomplete I believe ultimately that the entire guessing function needs to return something but I don't know when to return that. Any hints would be much appreciated.

dinotom
  • 4,990
  • 16
  • 71
  • 139
Talen Kylon
  • 1,908
  • 7
  • 32
  • 60
  • This won't even assemble. `la $s0,$a0` is wrong (should be `move $s0,$a0`). The `jal start_guessing` means that `start_guessing` is a function. You probably want `j start_guessing` unless the algorithm is recursive. If it is recursive, you'll need to preserve things in a stack frame (notably `$ra`) – Craig Estey May 01 '16 at 21:05
  • @CraigEstey Yeah, this is just a "rough draft". I haven't even tried to assemble it, but thank you for pointing those out. – Talen Kylon May 01 '16 at 21:07
  • 1
    Another suggestion: Depending upon the [maximum] complexity of the algorithm [in terms of lines of code], it might be easier if you coded it in C first [or another high level] language. Debug the algorithm there. Then, creating the MIPS version is just a question of faithfully reproducing the algorithm [treating the C code as pseudo-code]. That way, you're not fighting both problems at the same time: (1) is algorithm correct? (2) did I just use a wrong reg or wrong instruction in MIPS. – Craig Estey May 01 '16 at 21:34
  • Definitely agree with @Craig. Optimized compiler output can be a good starting point for asm, if its overall strategy is anything like what you think is optimal. Once you start hand-modifying it, you can still go back to the compiler for ideas by tweaking the C and recompiling, but then you just manually copy the ideas, not the actual instructions. (If nothing else, register allocation probably changed.) – Peter Cordes May 02 '16 at 04:03
  • 1
    @PeterCordes I was mostly thinking about the C itself. Herein: http://stackoverflow.com/questions/36560231/how-do-i-connect-dots-in-midpoint-algorithm-in-mips-assembly/36818416#36818416 and http://stackoverflow.com/questions/36538325/mips-linked-list/36560575#36560575 have cases where the C code ends up in a top comment block above the MIPS function. The sidebar comments in the asm refer to the C code. The 2nd link also has a lot of [my] recommendations about how to write asm well [my own experience]. Also, one can write the boilerplate code while thinking about the algorithm. – Craig Estey May 02 '16 at 17:11

0 Answers0