ASP.NET
  Home arrow ASP.NET arrow Page 2 - Finishing an Introductory Look at CIL
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Finishing an Introductory Look at CIL
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-11-26

    Table of Contents:
  • Finishing an Introductory Look at CIL
  • Local Variables
  • Loops
  • Loops Continued
  • The Outer Loop
  • The Inner Loop
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Finishing an Introductory Look at CIL - Local Variables


    (Page 2 of 7 )

    Before moving on to local variables, remember that we should state the maximum number of spaces on the stack that we'll require. We'll require two spaces for our program:


    .maxstack 2

    While the program will function if the number is set to more than is necessary, setting it to less than is necessary causes the program to crash. Alternatively, if we don't specify a value at all, the assembler will choose a default value.

    Recall that we also need to manually call for a return at the end of the method, unlike in C#:


    ret


    Otherwise, the program will crash.

    Moving on to local variables, from looking at the source of the C# version of the prime number program, we can see that four local variables are defined: i (the number being tested for primality), prime (a boolean value indicating whether or not the number is prime), limit (the square root of the number, up to which we check for factors – think of factors in pairs, one of the pair being less than the square root and one being greater than the square root, or the square root itself being a factor) and f (a possible factor, between two and the square root of the number).

    In CIL, local variables aren't defined as they are in C#. Instead of defining them as we need them at various parts of the method, we need to define them all at once near the top of the method. Let's do that now:


    .locals init (int32 i, int32 limit, int32 f)


    This method of defining variables is indeed different, but it shouldn't seem too scary. We simply give each variable a name and a type. Also, notice how we have not included our prime variable. In C#, we need it in order to mark a number as not prime when breaking out of the loop (otherwise, how could we distinguish between an early break and a complete run through the loop?). However, such a device is unnecessary with CIL, and soon we'll see why.

    So far, the CIL program should look like this:


    .assembly extern mscorlib {}

    .assembly FindPrimes {}


    .class public FindPrimes

    {

    .method public static void Main(string[] args)

    {

    .entrypoint

    .maxstack 2

    .locals init (int32 i, int32 limit, int32 f)


    ret

    }

    }


    More ASP.NET Articles
    More By Peyton McCullough


       · Hello,This is a continuation of the last article. It finishes the main example...
     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT