Motorola M68000 User's Guide Page 135

  • Download
  • Add to my manuals
  • Print
  • Page
    / 256
  • Table of contents
  • BOOKMARKS
  • Rated. / 5. Based on customer reviews
Page view 134
Llnhed ListsA Programming Example 121
INSR: JSR NEWLINE
CLR.B (AO)
CMP.B #CR,D0
BNE INSR1
CLR.B -1(AO)
INSR1: MOVEM.L (SP)+,A0/D0-D1
RTS
OUTPUT A NEWLINE
ADD A NULL TO STRING
LAST CHAR A CR?
NO
YES, PUT NULL THERE TOO
RESTORE REGISTERS
Note that the method used to handle an overflow count is different from
that used to handle a string terminated with a carriage return. If the
carriage return is entered, it must not appear in the string. The second
CMP.B instruction handles this check. If the last character entered was a
carriage return, it is overwritten with a null byte.
At this point we have enough basic subroutines to start manipulating
the linked lists themselves. We will need two types of list insertions; one
that inserts a node at the head of a list, and one that inserts a node in
alphabetical order. To insert at the head of the list is very simple. We
merely obtain the pointer to the first node from the header. You can think
of the header as a special node that has no data and is always located in a
known place. For example, the header for the free list is always located
at the memory location FLIST. We set the forward pointer in the node
we are inserting to point to the node that was pointed to by the header.
This may be a null pointer, but we dont care. The only other piece of
business is to set the header to point to the node we are inserting. Here
is how it’s done:
* INSERT - INSERT A NODE AT THE HEAD OF A LIST.
* AO -> NODE
* A1 -> HEADER OF LIST
****************************************************************
INSERT: MOVE.L (A1),(A0) SET UP LINK IN NEW NODE
MOVE.L A0,(A1) SET HEADER TO -> NEW NODE
RTS
Figure 10 shows this operation.
Insertion of a node in alphabetical order is considerably more difficult.
We must chain down the linked list until we find the proper insertion
point. This is determined by a string comparison using STRCMP. We
then have to link this node into the list by breaking the previous link and
setting the proper pointers. Refer to Figure 11.
INSERTA - INSERT A NODE ALPHABETICALLY.
AO -> NODE
A1 -> HEADER OF LIST
INSERTA:MOVEM.L A0-A2,-(SP) SAVE REGISTERS
ADDQ.L #4/AO AO -> NEW NODE DATA
Page view 134
1 2 ... 130 131 132 133 134 135 136 137 138 139 140 ... 255 256

Comments to this Manuals

No comments