Untitled

Anonymous
plain_text
02/12/2026 10:11 PM
1.9 KB
18
Indexable
# Enter your MIPS code below. 
# Write short comments explaining each line of your code 
# Explain your use of labels and functions
#by the way, I did my outline on a google doc. I am combining my ideas.
main: #that is the main method
move $a0, $s0 #value = addNode(head, n, newNode)
move $a1, $s1 #value = addNode(head, n, newNode)
move $a2, $s2 #value = addnode(head, n, newNode)
jal addNode # calls addNode
move $s3, $v0 #that places the result from the function to s6
j exit

addNode:
addi $sp, $sp, -8 #allocates 8
sw $ra, 4($sp) #I was told I needed to save 8 bytes
sw $a1, 0($sp)  #saves a1

beq $a2, $zero, true #if(n == 0 || head == 0)
beq $a0, $zero, true #if(n == 0 || head == 0)
j false #if non equal 0 then it goes to false, ends if
true:
sw $a0, 4($a1) #newNode->next = head
move $s0, $a1 #head = newNode
lw $v0, 0($a1) #return newnode value

lw $ra, 4($sp)
lw $a1, 0($sp)
addi $sp, $sp, 8 #allocates 8
jr $ra #return newnode value

false:

jal findNode #calls findnode

sw $a1, 4($v0) #head->next = newnode
sw $v1, 4($a1) #newnode->next = n
lw $v0, 0($a1)

lw $ra, 4($sp)
lw $a1, 0($sp)
addi $sp, $sp, 8 #allocates 8

jr $ra #return



findNode: #findnode()
addi $t1, $zero, 1 # i = 1
addi $v0, $a0, 0

# lw $v0, 0($a0) #v0 is current
loop:
bge $t1, $a2, exit1 #i < n

lw $v0, 4($v0) #current is currentnext
lw $v1, 4($v0) 

beq $v0, $zero, end #if curr == 0 
beq $v1, $zero, end #if curr->next->next = zero
addi $t1, $t1, 1
j loop 
end:
exit1:
jr $ra #return
exit:


Editor is loading...
Leave a Comment