Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please troubleshoot the following mips code: main: # Call addNode jal addNode # The result ( value of the new node ) is now in

Please troubleshoot the following mips code:
main:
# Call addNode
jal addNode
# The result (value of the new node) is now in $s3
j end # stop
# Inputs: $s0= head, $s2= n, $s1= newNode
# Output: $s3= value of the inserted node
addNode:
addi $sp, $sp,-20 # Allocate stack space (including for error code)
sw $ra,16($sp) # Save return address
sw $s0,12($sp) # Save head
sw $s2,8($sp) # Save n
sw $s1,4($sp) # Save newNode
sw $zero, 0($sp) # Space for error code
beq $s2, $zero, add_at_beginning # If n ==0, add at the beginning
jal findNode # Call findNode function
lw $v1,0($sp) # Load error code
bne $v1, $zero, addNode_exit # If error code is not zero, exit
# Get the returned addresses from findNode
lw $t0,4($sp) # Reload newNode
lw $t3,0($v0) # Address of n^th node (addr1)
lw $t4,4($v0) # Address of (n+1)^th node (addr2)
# Insert newNode after addr1
sw $t4,4($t0) # newNode->next = addr2
sw $t0,4($t3) # addr1->next = newNode
lw $s3,0($t0) # Store value of newNode in $s3
j addNode_exit # Jump to exit
add_at_beginning:
lw $t0,4($sp) # Reload newNode
lw $t1,12($sp) # Reload head
sw $t1,4($t0) # newNode->next = head
move $s0, $t0 # Update head to newNode
lw $s3,0($t0) # Store value of newNode in $s3
j addNode_exit # Jump to exit
addNode_exit:
lw $ra,16($sp) # Restore return address
addi $sp, $sp,20 # Deallocate stack space
jr $ra # Return
# Function: findNode
# Inputs: $s0= head, $s2= n
# Outputs: $v0= addresses of n^th and (n+1)^th nodes (stored in memory)
findNode:
addi $sp, $sp,-20 # Allocate more stack space for error code
sw $ra,16($sp) # Save return address
sw $s0,12($sp) # Save head
sw $s2,8($sp) # Save n
sw $zero, 4($sp) # Initialize error code to 0(no error)
move $t0, $s0 # curr = head
move $t1, $zero # i =0
findNode_loop:
beq $t1, $s2, findNode_end # If i == n, exit loop
beq $t0, $zero, findNode_error # If curr is NULL, list is too short
lw $t0,4($t0) # curr = curr->next
addi $t1, $t1,1 # i++
j findNode_loop
findNode_error:
ori $v1, $zero, 1 # Set error code to 1(list too short), equivalent to li $v1,1
j findNode_cleanup # Jump to cleanup
findNode_end:
sw $t0,0($sp) # Store address of n^th node
lw $t2,4($t0) # Get address of (n+1)^th node
sw $t2,4($sp) # Store address of (n+1)^th node
move $v0, $sp # Return address of stored addresses
findNode_cleanup:
lw $ra,16($sp) # Restore return address
addi $sp, $sp,20 # Deallocate stack space
jr $ra # Return
end:
Why are these values off?:
image text in transcribed

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_2

Step: 3

blur-text-image_3

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

Database Basics Computer EngineeringInformation Warehouse Basics From Science

Authors: Odiljon Jakbarov ,Anvarkhan Majidov

1st Edition

620675183X, 978-6206751830

More Books

Students also viewed these Databases questions