I have an assignment to clean up a linked list by "deleting" any nodes that contain a value less than a certain number (lets say 10). Its a bit more difficult than expected, since the nodes are not in order in memory. See below
我有一个通过“删除”任何包含小于某个数字的值的节点来清理链接列表的任务(比方说10)。它比预期的要困难一些,因为节点在内存中不是有序的。见下文
list:
.word 15
.word n1
n2:
.word 12
.word n3
n6:
.word 27
.word n7
n3:
.word 22
.word n4
n1:
.word 1
.word n2
n5:
.word 34
.word n6
n7:
.word 6
.word -1 # logical end of the list
n4:
.word 9
.word n5
If the head points to address "268500992", which contains 15, and the next location four bits away contains the pointer to the next node (called n1), how can I get the value from n1?
如果磁头指向包含15的“268500992”,并且下一个位置包含指向下一个节点(称为n1)的四个位,则如何从n1获取该值?
I have tried the following but it is loading an address (the pointer of the 1st node or address of n1) into $t0 instead of the value at that address.
我尝试了以下内容,但它将地址(第一个节点的指针或n1的地址)加载到$ t0而不是该地址的值。
la $a0,list
addi $a0, $a0, 4
lw $t0,0($a0)
1 个解决方案
#1
Don't forget that la $a0,list
will load a pointer to list. A subsequent lw $t0,0($a0)
will load the value of the head node, and lw $t0,4($a0)
(or the operation you did), will load the pointer to the next node. You are still is the head node, but now $t0
points to the node 1. The best thing to do would be to use lw $a0,4($a0)
to go to the next node. To retrieve its value you still need to do lw $t0,0($a0)
.
不要忘记la $ a0,list会加载一个指向列表的指针。随后的lw $ t0,0($ a0)将加载头节点的值,并且lw $ t0,4($ a0)(或您执行的操作)将加载指向下一个节点的指针。你仍然是头节点,但现在$ t0指向节点1.最好的办法是使用lw $ a0,4($ a0)转到下一个节点。要检索其值,您仍需要执行lw $ t0,0($ a0)。
#1
Don't forget that la $a0,list
will load a pointer to list. A subsequent lw $t0,0($a0)
will load the value of the head node, and lw $t0,4($a0)
(or the operation you did), will load the pointer to the next node. You are still is the head node, but now $t0
points to the node 1. The best thing to do would be to use lw $a0,4($a0)
to go to the next node. To retrieve its value you still need to do lw $t0,0($a0)
.
不要忘记la $ a0,list会加载一个指向列表的指针。随后的lw $ t0,0($ a0)将加载头节点的值,并且lw $ t0,4($ a0)(或您执行的操作)将加载指向下一个节点的指针。你仍然是头节点,但现在$ t0指向节点1.最好的办法是使用lw $ a0,4($ a0)转到下一个节点。要检索其值,您仍需要执行lw $ t0,0($ a0)。