1

Like the title states, I have one "template" string that I want to copy to another string so as to "reset" the string. Here is an example of what I'm trying to do:

I have these two strings at the start:

.data
blank:        .asciiz "-/-/-/-/-/"  
gameBoard:    .asciiz "-/-/-/-/-/"

I start off with these two strings, but then I modify gameBoard so it might look something like this:

"X/X/-/O/X/"

But now I want to reset the string so to speak, so it will look like 'blank', but I can't seem to properly copy the string over. Here's the code I tried, but I only managed to cause the console to enter an infinite loop and freeze:

resetGameBoard:
li $t0,0

lb $t1, blank($t0) 

sb $t1, gameBoard($t0)

add $t0, $t0,1

blt $t0, 10, resetGameBoard 

j main

Any help would be greatly appreciated!

Drew Pesall
  • 179
  • 3
  • 12

1 Answers1

2

There is a problem with loop counter initialization.

Counter increment should be done with addi not add

.data
blank:        .asciiz "-/-/-/-/-/"  
gameBoard:    .asciiz "-/-/-/-/-/"
.text
resetGameBoard:
    li   $t0,0
loop:
    lb   $t1, blank($t0) 
    sb   $t1, gameBoard($t0)
    addi $t0, $t0,1
    blt  $t0, 10, loop
    nop
    j main

Note that the way you access your arrays by adding the address as a constant in lb/sb to an index register is non standard. It works, but only if data segment address can coded with 15 bits.

To go beyond these constraints, addresses of arrays should be stored in registers.

resetGameBoard:
    li   $t0, 0
    la   $t2, blank     #t2=@blank
    la   $t3, gameBoard $t3=@gameBoard
loop:
    lb   $t1, 0($t2) 
    sb   $t1, 0($t3)
    addi $t0, $t0,1
    addi $t2, $t2, 1 #@blank++
    addi $t3, $t3, 1 #@gameboard++
    blt  $t0, 10, loop
    nop
    j main

The code is slightly longer, but for real programs, any array address can be generated. la (load address) is a macro that can write any 32 address in a register. It is also possible to get the address through the global pointer register. This method adds flexibility and allows, for instance, to easily unroll the loop, etc.

Alain Merigot
  • 10,667
  • 3
  • 18
  • 31