Addressing Fields

Addressing Fields

Six instructions can be used to load a field value or an address on the stack or to store a value from the stack in a field. Because a field signature does not indicate whether the field is static or instance, the IL instruction set defines separate instructions for dealing with instance and static fields. Instructions dealing with instance fields take the instance pointer—an object reference if the field addressed belongs to a class, and a managed pointer if the field belongs to a value type—from the stack.

  • ldfld <token> (0x7B)  Pop the instance pointer from the stack and load the value of an instance field on the stack. <token> must be a valid FieldDef or MemberRef token, uncompressed and uncoded.

  • ldsfld <token> (0x7E)  Load the value of a static field on the stack.

  • ldflda <token> (0x7C)  Pop the instance pointer from the stack and load a managed pointer to the instance field on the stack.

  • ldsflda <token> (0x7F)  Load a managed pointer to the static field on the stack.

  • stfld <token> (0x7D)  Pop the value from the stack, pop the instance pointer from the stack, and store the value in the instance field.

  • stsfld <token> (0x80)  Store the value from the stack in the static field.

The ILAsm notation requires full field specification, which is resolved to <token> at compile time:

ldfld int32 Foo.Bar::ii

The applicable conversion rules when loading and storing values are the same as those discussed earlier. Note also that the fields cannot be of managed pointer type.



Inside Microsoft. NET IL Assembler
Inside Microsoft .NET IL Assembler
ISBN: 0735615470
EAN: 2147483647
Year: 2005
Pages: 147
Authors: SERGE LIDIN

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net