Code wont stop running in MIPS assembly simulation

34 Views Asked by At
main:
    jal addNode              # Call addNode to insert the new node
    # Assuming addNode returns the new node's value in $v0
    move $s3, $v0            # Move the returned value to $s3
    j end                    # Jump to end to halt the program

addNode:
    addi $sp, $sp, -16       # Allocate stack space for saved registers
    sw $ra, 12($sp)          # Save $ra
    sw $s0, 8($sp)           # Save head
    sw $s1, 4($sp)           # Save newNode
    sw $s2, 0($sp)           # Save n

    beq $s2, $zero, addAtStart  # If n is 0, add at the start

    # Prepare and call findNode
    move $a0, $s0            # Pass head as argument
    move $a1, $s2            # Pass n as argument
    jal findNode             # Call findNode

    # After findNode, $v0 has addr of n-th node, $v1 has addr of (n+1)-th node
    # Insert newNode after n-th node
    lw $t0, 4($v0)           # Load address of (n+1)-th node
    sw $t0, 4($s1)           # Set newNode's next to (n+1)-th node
    sw $s1, 4($v0)           # Set n-th node's next to newNode

    # Load newNode's value to return it
    lw $v0, 0($s1)           

    # Restore registers and return
    lw $s2, 0($sp)
    lw $s1, 4($sp)
    lw $s0, 8($sp)
    lw $ra, 12($sp)
    addi $sp, $sp, 16
    jr $ra

addAtStart:
    sw $s0, 4($s1)           # Set newNode's next to current head
    move $s0, $s1            # Update head to newNode
    lw $v0, 0($s1)           # Load newNode's value to return
    jr $ra                   # Return to addNode, which returns to main

findNode:
    ori $t2, $zero, 0         # Initialize loop counter to 0
    move $t0, $a0             # $t0 is the current node, start with head

    # Check if we're adding at the start (should be handled outside this function)
    beq $a1, $zero, findNode_exit  # If adding at position 0, special case

    # Iterate through the list to find the correct node
loop_findNode:
    # Check if we've reached the position before where we want to add the new node
    addi $t2, $t2, 1           # Increment counter
    beq $t2, $a1, findNode_exit # If we've reached the right position, exit loop

    lw $t0, 4($t0)             # Move to the next node
    beq $t0, $zero, findNode_exit # If next node is NULL, exit loop (end of list)

    j loop_findNode            # Jump back to start of loop

findNode_exit:
    # Prepare to return: $v0 should already have the address of the correct node
    jr $ra                     # Return to caller
                # Return to caller

end:
    j end                    # Infinite loop to halt the program

**i cant use syscall or nop in this simulator so i tried an infinite loop. I am getting the array values i need but the code itself wont stop running. It seems to be in a infinite loop and wont stop. When i run it through the test cases it just times out understandably **

0

There are 0 best solutions below