Hi Amir,
"Amir" <anonymous@discussions.microsoft.com> wrote in news:06b801c3adec
$6bfd5f00$a001280a@phx.gbl:
[...]
> 1- Why loading a Valuetype argument (arg) in line
> number "IL_0000" generates "ldarga.s" command?
> It's a valuetype but load the address of "arg".
it loads the address of the argument. a struct is a value type but nether
the less it can't be handed to the cpu in one piece so it's necessary to
treat it internal as a reference and pass the address. It depends on the
target machine how the struct will be treated. on a x86 it will push the
address of a by value copy of the struct on the stack and then handle it
like any other value type
> 2- Why after loading the address of "arg" (ldarga.s)
> use "ldfld" instead of "ldobj" that load a Valuetype
> object?
loadfield takes the adress of the struct and loads the desired field from
the struct. you have to do this in il asm to be able to calc the right
offset on the real cpu. in x86 code you will notice that this is optimized
to a direct load on the member of the struct which is then really treated
like a value type.
greets
Peter

Signature
------ooo---OOO---ooo------
Peter Koen - www.kema.at
MCAD CAI/RS CASE/RS IAT
------ooo---OOO---ooo------
1. All methods for value type objects expect that the first argument is not
the "this" or "Me" pointer, but is instead the address of the value itself.
This also applies to obcodes for the value type. Thus the ldfld will want
the "this" pointer for a reference type but a managed pointer to the value
for a value type. Here is a simple example of this in action for a method -
one of my favorite "you can do that in .NET?"
static void ShowValue()
{
3.ToString();
}
becomes
.method private hidebysig static void ShowValue() cil managed
{
// Code size 11 (0xb)
.maxstack 1
.locals init ([0] int32 CS$00000002$00000000)
IL_0000: ldc.i4.3
IL_0001: stloc.0
IL_0002: ldloca.s CS$00000002$00000000
IL_0004: call instance string [mscorlib]System.Int32::ToString()
IL_0009: pop
IL_000a: ret
} // end of method Test::ShowValue
It first creates a local variable to hold the 3 so it has someplace with an
address, then loads the address of the local variable onto the stack.
Int32::ToString() knows that the "this" pointer is really a managed pointer
to the value.
2. ldobj would copy the entire value onto the evaluation stack - not a big
deal for an integer, but large structures would get to be a bit much.
However, the main answer is that is how the system is designed - if you want
to access a field of an object (value or reference), you use the ldfld.
I've found "Advanced .NET Programming" by Simon Robinson (Wrox) to be a
great reference for some of these "what the hell" issues.
Hope this helps.
Brian
> Hello
> Review bellow function and it's generated IL Code:
[quoted text clipped - 29 lines]
>
> Thanks in advanced