Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / November 2003

Tip: Looking for answers? Try searching our database.

MSIL

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Amir - 18 Nov 2003 15:55 GMT
Hello
Review bellow function and it's generated IL Code:

1-
 public int F(St arg)
 {
     return arg.stID;
 }

2-
 IL_0000:  ldarga.s   arg
 IL_0002:  ldfld      int32 St::stID
 IL_0007:  stloc.0
 IL_0008:  br.s       IL_000a
 IL_000a:  ldloc.0
 IL_000b:  ret

Where "St" is a structure like this:
public struct St
{
public int stID;
public string stName;
}

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".

2- Why after loading the address of "arg" (ldarga.s)
use "ldfld" instead of "ldobj" that load a Valuetype
object?

Thanks in advanced
Peter Koen - 18 Nov 2003 16:12 GMT
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------

Brian Tyler - 18 Nov 2003 16:22 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.