tags:

views:

25

answers:

1

It seems that my understanding of MIPS fails me.

What I need to do is create a program that reverses a string input from terminal, using a stack, but without using $sp. Is there anyone here that knows MIPS?

Register usage

t0 - theString address start
t1 - stack address start
t2 - temp for retrieved character
t3 - counter for t0
t4 - counter for stack
t5 - newline
t6 - Length

.data

theString:  .space 42
newLine:    .asciiz "\n"
stack:      .space 42

.globl main

.text

main:

li      $v0, 8      #  Set to read string
la  $a0, theString
li      $a1, 42     #  Set size of string
syscall         #  Read string from terminal

la      $t0, theString  #  Prepare t0 with theString
la      $t1, stack  #  Prepare t1 with stack
la  $t5, newLine

addi    $t3, $t0, 0
addi    $t4, $t1, 42

j push

push:

addi    $t3, $t3, 1
addi    $t4, $t4, -1

lb  $t2, ($t3)
beq $t2, $t5, epush
sb  $t2, ($t4)

j push

epush:

sub $t6, $t3, $t0
addi    $t6, $t6, -1

addi    $t3, $t0, 0
addi    $t4, $t1, 0

j pop

pop:

addi    $t3, $t3, 1
addi    $t4, $t4, 1 

lb  $t2, ($t4)
beq $t2, $t5, epop
sb  $t2, ($t3)

j pop

epop:

addi    $t3, $t3, 1
sb  $t5, ($t3)


li  $v0, 4      #  Set to print string
la  $a0, theString  #  Set var to syscall output register
syscall         #  Print string

li  $v0, 10     #  Set to end program
syscall         #  End Program

For example, this just gives an infinite loop. (Sorry for lack of comments, I'm just tearing my hair out here)

Now, I think the problem is somewhere related to the newline character, but I don't know where?

A: 

Is there a reason you are using j and not jal? And it appears you are using SPIM, which has numerous issues.

In your pop loop, you are comparing (in beq) the address of newline (in $t5) and the address of the next character in the string. Even though they might contain the address of a location that contains "\n", the addresses might not be the same, because "\n" can be in more than one place. In fact, I can guarantee that they will never be the same, because one will refer to the address of the captured string, and the one in $t5 will be the address of the one at the beginning of the program.

xxpor
Ah, so how do I compare the actual newline value to the value in $t5?
Jack
I don't know if this would work, but try `lb` from `$t5` and `$t2` into 2 other temp registers and comparing them.
xxpor
Thanks. It gets out of the loop fine now, but just prints the original string?
Jack
Try using `"\0"` instead of `"\n"`. SPIM might terminate the string on newline, rather then appending it to the end of the string.
xxpor
I'll try that then.
Jack
Both "\0" and '\0' result in a syntax error on that line.
Jack
Hmm. I'm out of ideas then, sorry. When I took computer org and design last semester, I never did any string manipulation.
xxpor