Question: my code is failing the tests pictured, please help, tia! Write a MIPS program to add a given node in a linked list at the
my code is failing the tests pictured, please help, tia!
Write a MIPS program to add a given node in a linked list at the specified location, using Nested Procedure Calls. If your code runs perfectly, but you didn't use Procedure Execution correctly, you will be given zero points.
Given Inputs:
the head of a linked list, ie address of the start first node of the list
location: number of node in the linked list after which node is to be added to add before the st node, to add after st node and so on
the address of the node to be inserted And each node contains:
an integer value
address to the next node address is NULL if it is the last node in the list
Write the following three functions that will be called in order to update the linked list by adding the new node.
main
task: calls the addNode function and reads the value of the newly added node.
addNode
inputs: head of linked list head location to add node n and address of node to be added node
task: calls the findNode function and add the node after the nth node in the linked list. If the number is greater than the size of list, then add the node at the end of the list.
output: value of the inserted node.
findNode
inputs: head of linked list head and location to add node n
task: navigate the linked list to find the addresses of the nth and nth nodes
outputs: addresses of the nth and nth nodes.
Registers Variables:Compare storage
Drogram timed out
:Compare storage
Program timed out
:Compare storage
Program timed out
:Compare storage
Program timed out
$s head
$s newNode
$s n
$s val
Linked List and New Node in Memory:
Addresses Contents
newNode newNodevalue
head nodevalue
head nodenext
nodenext nodevalue
nodenext nodenext
nodenext nodevalue
nodenext nodenext
Example Test: If the values of $s through $s and Memory contents are initialized in the simulator as:
Use the button under the Registers display to initialize register values for $s $s $s and the button under the Memory display to initialize the initial array elements.
Registers Data
$s
$s
$s
$s
Addresses Contents
The resultant registers are:
Registers Data
$s
$s
$s
$s
The resultant array is:
Addresses Contents
# Enter your MIPS code below.
# Write short comments explaining each line of your code
# Explain your use of labels and functions
# Main function
main:
addi $a $s # arg head $s
addi $a $s # arg n $s
addi $a $s # arg node $s
jal addNode # Call addNode function
Ret:
move $s $v # $s output of addNode function
j Exit # Exit the program
# Function to add a node at a specified location
addNode:
bne $a $zero, Cfind # If n call findNode logic
sw $a$a # newNodenext head
add $s $a $zero # head newNode
j Rinsert # return
Cfind:
move $t $a # curr head
add $t $zero, $zero # i
Loop:
bge $t $a Rfind # if i n return
lw $t$t # curr currnext
beq $t $zero, Rfind # if curr NULL, break
addi $t $t # i
j Loop
Rfind:
move $v $t # v address of nth node
lw $v$t # v address of nth node
sw $a$v # nth node's next newNode
sw $v$a # newNode's next nth node
j Rinsert # return
Rinsert:
lw $v$a # Return newNodevalue
j Loop # Loop indefinitely
Exit:
j Exit # Infinite loop to halt program execution
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
