Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Any help would be appreciated, tia! Write a MIPS program to add a given node in a linked list at the specified location, using Nested

Any help would be appreciated, 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.
Following is a sample C code segment to perform the required task. (this code is incomplete and does not include creating the linked list or a new node, so you cannot compile it using any C compiler.) You may modify the code for the functions, but the task performed should not be changed.
// Parameters of a node in the linked list (need not declare or initialize in MIPS)
typedef struct node {
int value; // Value in the node accessed by node->value
node* next; // Address of next node accessed by node->next
} node; // Datatype for each node
node *head; // address of head (first node) of linked list (global pointer)
int main(){
// Variable Declaration
node *newNode; // address of node to be added
int n; // number of the node in the list after which node is to be added
int value; // Value of the node to be added
// Task of main function
value = addNode(head, n, newNode);
}
int addNode (node* head, int n, node* newNode){
node *addr1,*addr2; // addr1= address of n^th node, addr2= address of (n+1)^th node
if (n ==0){// If node should be added at the beginning of the list
newNode->next = head; // Next for new node = head of original list
head = newNode; // global head updated to the new node
return(newNode->value); // value of the node = data at the address of the node, and then return to caller
}
[addr1, addr2]= findNode (head, n); // Call findNode function
addr1->next = newNode; // Next for n^th node = node to be added
newNode->next = addr2; // Next for added node =(n+1)^th node of original list
return(newNode->value); // value of the node = data at the address of the node
}
node* findNode (node* head, int n){
node* curr = head; // Start with head of linked list
for (int i =1; i < n; i ++){
curr = curr->next; // Update the pointer to next node address
if (curr->next ==0)// Break if end of List
break;
}
return([curr, curr->next]); // Two return values (need not return as array in MIPS)
}
Registers Variables
$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:
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
This is what I have, but I get an error saying Line 5: Unrecognized syntax near: .data
.data
head: .word 0x10010000
newNode: .word 25,0
location: .word 2
.text
.globl main
main:
lw $a0, head
la $a1, newNode
lw $a2, location
jal addNode
li $v0,10
syscall
addNode:
beqz $a2, addAtStart
jal findNode
lw $t0,4($a1)
sw $t0,4($a0)
sw $a1,4($a0)
jr $ra
addAtStart:
sw $a0,4($a1)
sw $a1, head
jr $ra
findNode:
jr $ra

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Pro PowerShell For Database Developers

Authors: Bryan P Cafferky

1st Edition

1484205413, 9781484205419

More Books

Students also viewed these Databases questions

Question

How do you identify yourself culturally?

Answered: 1 week ago

Question

Understand the basic theories and concepts of OD

Answered: 1 week ago