The variable List contains the address of the first node in the list. It is zero when the list is empty.
The first two bytes of each entry contains the address of the next node in the list. That address is zero in the last node of the list.
This exercise is to find and remove a node from a list. The variable Node contains its address.
Code: Select all
00008 *
00009 * Remove a node from a linked list.
00010 *
00011 * Input:
00012 * List = the address of the first node in the list.
00013 * Node = the address of the node to remove
00014 *
00015 * Scoreboard:
00016 *
00017 * 21 cycles if the list is empty
00018 *
00019 * otherwise
00020 *
00021 * 24 cycles to start
00022 *
00023 * 23 cycles for each node before the specified one
00024 *
00025 * 20 cycles to exit if the node is not found
00026 * 38 cycles to exit if the node is found
00027 *
0100 00028 Remove
0100 CE 0002 [3] 00029 ldx #List ; This is the "previous" of the first node
0103 DF 04 [5] 00030 stx Prev
00031
0105 DE 02 [4] 00032 ldx List ; Point to the first node
0107 27 0A (0113) [4] 00033 beq Done ; End of the list?
00034
0109 00035 Loop
0109 9C 00 [4] 00036 cpx Node ; Is this the node?
010B 27 07 (0114) [4] 00037 beq Found ; Yes
00038
010D DF 04 [5] 00039 stx Prev ; This will be the previous node
00040
010F EE 00 [6] 00041 ldx ,X ; Point to the next node
00042
0111 26 F6 (0109) [4] 00043 bne Loop ; Repeat
00044
0113 00045 Done
0113 39 [5] 00046 rts
00047
0114 00048 Found
0114 EE 00 [6] 00049 ldx ,X ; Get the address of the next node
0116 DF 06 [5] 00050 stx Next
00051
0118 DE 04 [4] 00052 ldx Prev ; Point back to the previous node
00053
011A 96 06 [3] 00054 ldaa Next ; Link it to the next node
011C A7 00 [6] 00055 staa ,X
011E 96 07 [3] 00056 ldaa Next+1
0120 A7 01 [6] 00057 staa 1,X
00058
0122 39 [5] 00059 rts