Answered step by step
Verified Expert Solution
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 $s
j end # stop
# Inputs: $s head, $s n $s newNode
# Output: $s value of the inserted node
addNode:
addi $sp $sp # Allocate stack space including for error code
sw $ra$sp # Save return address
sw $s$sp # Save head
sw $s$sp # Save n
sw $s$sp # Save newNode
sw $zero, $sp # Space for error code
beq $s $zero, addatbeginning # If n add at the beginning
jal findNode # Call findNode function
lw $v$sp # Load error code
bne $v $zero, addNodeexit # If error code is not zero, exit
# Get the returned addresses from findNode
lw $t$sp # Reload newNode
lw $t$v # Address of nth node addr
lw $t$v # Address of nth node addr
# Insert newNode after addr
sw $t$t # newNodenext addr
sw $t$t # addrnext newNode
lw $s$t # Store value of newNode in $s
j addNodeexit # Jump to exit
addatbeginning:
lw $t$sp # Reload newNode
lw $t$sp # Reload head
sw $t$t # newNodenext head
move $s $t # Update head to newNode
lw $s$t # Store value of newNode in $s
j addNodeexit # Jump to exit
addNodeexit:
lw $ra$sp # Restore return address
addi $sp $sp # Deallocate stack space
jr $ra # Return
# Function: findNode
# Inputs: $s head, $s n
# Outputs: $v addresses of nth and nth nodes stored in memory
findNode:
addi $sp $sp # Allocate more stack space for error code
sw $ra$sp # Save return address
sw $s$sp # Save head
sw $s$sp # Save n
sw $zero, $sp # Initialize error code to no error
move $t $s # curr head
move $t $zero # i
findNodeloop:
beq $t $s findNodeend # If i n exit loop
beq $t $zero, findNodeerror # If curr is NULL, list is too short
lw $t$t # curr currnext
addi $t $t # i
j findNodeloop
findNodeerror:
ori $v $zero, # Set error code to list too short equivalent to li $v
j findNodecleanup # Jump to cleanup
findNodeend:
sw $t$sp # Store address of nth node
lw $t$t # Get address of nth node
sw $t$sp # Store address of nth node
move $v $sp # Return address of stored addresses
findNodecleanup:
lw $ra$sp # Restore return address
addi $sp $sp # Deallocate stack space
jr $ra # Return
end:
Why are these values off?:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started