Un-google-able bug in MIPS Assembly

0

I am writing a program in R3000 MIPS Assembly using the IDE Mars. It's for a class I'm taking. The task is to write a function that iterates through a linked list and removes any nodes with values less than an integer given in an argument. I think I've got the solution down, but during testing I keep running into this runtime exception:

DEVELOPER: You must use setStatement() to write to text segment!0x00000014

I haven't the faintest idea what is causing this error. I've tried Googling it, but nothing remotely related comes up. I don't even know if it's a problem with the Assembly code or with the Mars IDE. The really weird thing is that it doesn't seem to show up all the time, even under identical circumstances. If I have the error, sometimes I'll change the code (usually by commenting out one of the syscalls), run it, and it will go away and not come back if I change the code back.

Here is my code for the function. The arguments are the address of the first node in the linked list, and the cutoff value for removing nodes, in $a0 and $a1 respectively.

   .text
cleanup:
    add $t0, $a0, $0    #keep the argument from being modified
    la  $t1, head   #the address for the first node
    sw  $a0, head   #the head points at the first word now

    li  $v0, 1      #DEBUG
    li  $t7, 0      #DEBUG

while:
    lw  $t2, ($t0)  #get the node value
    bge $t2, $a1, else  #IF the node value is >= x, skip the next bit

    add $a0, $t1, $0    #DEBUG
    syscall         #DEBUG

    lw  $t3, 4($t0) #get the address of the next node
    sw  $t3, 4($t1) #store the address of the next node to the next node address of the previous node
    b   ifend       #skip to the end of the if statement
else:
    lw  $t1, ($t0)  #ELSE set pointer to previous node to this node

ifend:
    la  $t4, 4($t0) #need to check if the end of the list is nigh
    beq $t4, -1, out    #exit loop at the end of the list
    lw  $t0, 4($t0) #set current node pointer to the next node


    addi    $t7, $t7, 1 #DEBUG
    addi    $a0, $t7, 0 #DEBUG
    syscall         #DEBUG

    b   while       #and loop!

out:

    .data
head:
    .word 0
assembly
mips
mars-simulator
asked on Stack Overflow Oct 2, 2013 by DementedDr • edited Oct 26, 2015 by Michael

1 Answer

2

Just looking at your code, on the first pass through, $t1 is set to the address of head:

la $t1 head

And then you store to one word after that address:

sw $t3, 4($t1)

This means that you will write to one word after the word located at head that you have allocated in your data-segment. But because your data segment only contains one word it is anybody's guess where this will go -- in this case probably to your text segment hence the error.

answered on Stack Overflow Oct 2, 2013 by Konrad Lindenbach • edited Feb 5, 2016 by Konrad Lindenbach

User contributions licensed under CC BY-SA 3.0