The LinkedListobject contains a ListHeadmember of type ListItem
(from Listing 12.3), and two methods (not counting the constructor): AddItem
and Dump. Let’s begin with AddItem. This method starts with an interesting
sequence where the NewItemparameter is pushed into the stack, followed by
the first parameter, which is the thisreference for the LinkedListobject.
The next line uses the ldfld instruction to read from a field in the
LinkedListdata structure (the specific instance being read is the one whose
reference is currently at the top of the stack—the thisobject). The field being
accessed is ListHead; its contents are placed at the top of the stack (as usual,
the LinkedListobject reference is popped out once the instruction is done
with it).
You proceed to IL_0007, where stfldis invoked to write into a field in the
ListIteminstance whose reference is currently the second item in the stack
(the NewItempushed at IL_0000). The field being accessed is the Nextfield,
and the value being written is the one currently at the top of the stack, the
value that was just read from ListHead. You proceed to IL_000c, where the
ListHeadmember is again loaded into the stack, and is tested for a valid
value. This is done using the brfalseinstruction, which branches to the spec-
ified address if the value currently at the top of the stack is null or false.
Assuming the branch is not taken, execution flows into IL_0014, where
stfldis invoked again, this time to initialize the Prevmember of the List
Headitem to the value of the NewItemparameter. Clearly the idea here is to
push the item that’s currently at the head of the list and to make NewItemthe
new head of the list. This is why the current list head’s Prevfield is set to point
to the item currently being added. These are all classic linked list sequences.
The final operation performed by this method is to initialize the ListHead
field with the value of the NewItemparameter. This is done at IL_0020,
which is the position to which the brfalsefrom earlier jumps to when
ListHeadis null. Again, a classic linked list item-adding sequence. The new
items are simply placed at the head of the list, and the Prevand Nextfields of
the current head of the list and the item being added are updated to reflect the
new order of the items.
The next method you will look at is Dump, which is listed right below the
AddItemmethod in Listing 12.4. The method starts out by loading the current
value of ListHeadinto the V_0local variable, which is, of course, defined as
a ListItem. There is then an unconditional branch to IL_0016(you’ve seen
these more than once before; they almost always indicate the head of a
posttested loop construct). The code at IL_0016uses the brtrueinstruction
to check that V_0is non-null, and jumps to the beginning of the loop as long
as that’s the case.
The loop’s body is quite simple. It calls the Dumpvirtual method for each
ListItem(this method is discussed later), and then loads the Nextfield from
Reversing .NET 441