Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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, i.e, address of the start (first node) of the list
location: number of node in the linked list after which node is to be added (0 to add before the 1st node, 1 to add after 1st 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 (0) 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 n^th 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 n^th and (n+1)^th nodes
outputs: addresses of the n^th and (n+1)^th nodes.
Registers Variables1:Compare storage
Drogram timed out
2:Compare storage
Program timed out
3:Compare storage
Program timed out
4:Compare storage
Program timed out
$s0 head
$s1 newNode
$s2 n
$s3 val
Linked List and New Node in Memory:
Addresses Contents
newNode newNode->value
head node1->value
head +4 node1->next
node1->next node2->value
node1->next +4 node2->next
node2->next node3->value
node2->next +4 node3->next
......
Example Test: If the values of $s0 through $s3 and Memory contents are initialized in the simulator as:
(Use the '+' button under the Registers display to initialize register values for $s0, $s1, $s2 and the '+' button under the Memory display to initialize the initial array elements.)
Registers Data
$s04000
$s18000
$s22
$s30
Addresses Contents
8000230
40004
40043848
3848-15
38526104
6104-10
61085008
50080
50124500
450040
45040
The resultant registers are:
Registers Data
$s04000
$s18000
$s22
$s3230
The resultant array is:
Addresses Contents
8000230
80046104
40004
40043848
3848-15
38528000
6104-10
61085008
50080
50124500
450040
45040
# 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 $a0, $s0,0 # arg1= head = $s0
addi $a1, $s2,0 # arg2= n = $s2
addi $a2, $s1,0 # arg3= node = $s1
jal addNode # Call addNode function
Ret:
move $s3, $v0 # $s3= output of addNode function
j Exit # Exit the program
# Function to add a node at a specified location
addNode:
bne $a1, $zero, C_find # If n !=0, call findNode logic
sw $a0,4($a2) # newNode->next = head
add $s0, $a2, $zero # head = newNode
j R_insert # return
C_find:
move $t0, $a0 # curr = head
add $t1, $zero, $zero # i =0
Loop:
bge $t1, $a1, R_find # if i >= n, return
lw $t0,4($t0) # curr = curr->next
beq $t0, $zero, R_find # if curr == NULL, break
addi $t1, $t1,1 # i++
j Loop
R_find:
move $v0, $t0 # v0= address of n-th node
lw $v1,4($t0) # v1= address of (n+1)-th node
sw $a2,4($v0) # n-th node's next = newNode
sw $v1,4($a2) # newNode's next =(n+1)-th node
j R_insert # return
R_insert:
lw $v0,0($a2) # Return newNode->value
j Loop # Loop indefinitely
Exit:
j Exit # Infinite loop to halt program execution
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions