Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / MSIL

Mandelbrot Set in MSIL

Rate me:
Please Sign up or sign in to vote.
4.83/5 (5 votes)
23 Jun 2002BSD1 min read 136.9K   559   29   15
Demonstrates the use of Windows Forms, GDI+ from CIL

Introduction

To really be able to program .NET, a good programmer will find himself/herself using ILDASM, the MSIL disassembler, on a regular basis. This necessitates an ability to, at a minimum, be competent at reading MSIL. Unfortunately, the best way to learn to read MSIL is to spend some time writing it! Thus, I set about thinking of something I could write, which would not be too large, but would cover enough of the concepts to give me an ability to read MSIL.

I remember a friend at college who, when he was learning a new language, would always generate Mandelbrot sets as his first piece of code (a mandelbrot set written in Postscript was amusing). This seemed do-able, so I chose to write a Mandelbrot set generator in MSIL.

The code uses Windows Forms to show a new window, then draws the pixels onto this graphics surface.

Compiling

Compile the code simply by ilasm /exe mandelbrot.il. Remember to have ilasm in your path - the batch file corvars.bat in the bin folder of the .NET Framework SDK will add it for you.

This should generate mandelbrot.exe in the same folder.

Screenshot of Mandelbrot.il

The Code

The code inherits from System.Windows.Form and manages its own painting.

MSIL
.class public auto ansi MandelBrotIL extends [System.Windows.Forms]System.Windows.Forms.Form {

This class overrides the OnPaint() method of System.Windows.Forms.Form (which it inherits from). OnPaint simply sets up the graphics to draw onto, and then calls DrawMandelbrot() to do the actual drawing:

MSIL
.method family hidebysig virtual instance void OnPaint(class 
               [System.Windows.Forms]System.Windows.Forms.PaintEventArgs e) cil managed {

    ldarg.0
    ldarg e
    call  instance class [System.Drawing]System.Drawing.Graphics 
                    [System.Windows.Forms]System.Windows.Forms.PaintEventArgs::get_Graphics()
    ldc.r4 200 // Width in pixels
    ldc.i4 250 // Iterations before we assume convergence to zero
    call instance void ThunderMain.Demo.MandelBrotIL::DrawMandelbrot(class 
                    [System.Drawing]System.Drawing.Graphics, float32, int32)
    ret
}

DrawMandelbrot() simply implements a basic Mandelbrot algorithm, details of which can be found in many places on the Internet.

MSIL
.method private hidebysig instance 
           void DrawMandelbrot(class [System.Drawing]System.Drawing.Graphics g,
                               float32 fpMandelWidth, int32 ipIterations) cil managed {

    .locals (
    [0] float32 x, // Complex Real portion
    [1] float32 y, // Complex Imaginary portion
    [2] float32 fpGranularity, // Resolution of image on screen
    [3] int32 ipPixelX, // X position on screen
    [4] int32 ipPixelY, // Y position on screen
    // Escape Velocity locals
    [5] float32 fpX,
    [6] float32 fpY,
    [7] float32 fpXTemp,
    [8] float32 fpYTemp,
    [9] float32 fpX2, // X Squared
    [10] float32 fpY2, // Y Squared
    [11] int32 i // Loop variable used to calc escape velocity
    )
    // Calculate Granularity
    ldc.r4 4
    ldarg fpMandelWidth
    div
    stloc fpGranularity

    // Initialise point on screen
    ldc.i4.0
    dup
    stloc ipPixelX
    stloc ipPixelY

    // Start the real portion off
    ldc.r4 -2.5
    stloc x

NextReal:

    // Start the imaginary portion off
    ldc.r4 -1.5
    stloc y

NextImaginary:

    // Calculate Escape Velocity

    // Initialise locals
    ldloc x
    stloc fpX

    ldloc y
    stloc fpY

    ldloc x
    dup
    mul
    stloc fpX2

    ldloc y
    dup
    mul
    stloc fpY2


    // Initialise i
    ldc.i4.0
    stloc i
NextIteration:

    ldloc x
    ldloc fpX2
    ldloc fpY2
    sub
    add
    stloc fpXTemp

    ldloc y
    ldloc fpY
    ldloc fpX
    ldc.r4 2
    mul
    mul
    add
    stloc fpYTemp

    // Calculate X squared
    ldloc fpXTemp
    dup
    mul
    stloc fpX2

    // Calculate Y squared
    ldloc fpYTemp
    dup
    mul
    stloc fpY2

    // If X Squared plus Y Squared is greater than 4, then we are guaranteed 
    // divergence to Infinity
    ldloc fpX2
    ldloc fpY2
    add
    ldc.r4 4
    bge Divergence

    // The previous values in the sequence become the current values in the sequence
    ldloc fpXTemp
    stloc fpX
    ldloc fpYTemp
    stloc fpY

    // Incrememt i
    ldc.i4.1
    ldloc i
    add
    stloc i

    ldloc i
    ldarg ipIterations
    blt NextIteration // Assume convergence to zero if we reach our iteration limit

Divergence:

    // Draw Pixel. i is now the escape velocity

    // Load the Graphics context
    ldarg g

    // Calculate the color based on the escape velocity

    ldarg ipIterations
    ldloc i
    sub
    stloc i

    // Red
    ldloc i
    ldc.i4 12
    mul
    ldc.i4 256
    rem

    // Green
    ldloc i
    ldc.i4 16
    mul
    ldc.i4 256
    rem

    // Blue
    ldloc i
    ldc.i4 5
    mul
    ldc.i4 256
    rem

    call value class [System.Drawing]System.Drawing.Color 
                           [System.Drawing]System.Drawing.Color::FromArgb(int32,
                                                                          int32,
                                                                          int32)

    // Create a new Pen on the stack
    ldc.r4 1
    newobj instance void [System.Drawing]System.Drawing.Pen::.ctor(value class 
                                              [System.Drawing]System.Drawing.Color,
                                                float32)

    // Load the coords of the point to draw
    ldloc ipPixelX
    ldloc ipPixelY

    // Width and Height
    ldc.i4.2
    dup

    call instance void [System.Drawing]System.Drawing.Graphics::DrawRectangle(class 
                                               [System.Drawing]System.Drawing.Pen,
                                                int32,
                                                int32,
                                                int32,
                                                int32)

    // Next PixelY
    ldc.i4.1
    ldloc ipPixelY
    add
    stloc ipPixelY

    // Advance through the imaginary portion
    ldloc y
    ldloc fpGranularity
    add
    stloc y

    ldloc y
    ldc.r4 1.5
    blt NextImaginary

    // Advance through ipPixelX
    ldc.i4.1
    ldloc ipPixelX
    add
    stloc ipPixelX

    // Start at the top of the screen for the next column
    ldc.i4.0
    stloc ipPixelY

    // Advance through the real portion
    ldloc x
    ldloc fpGranularity
    add
    stloc x

    ldloc x
    ldc.r4 1.5
    blt NextReal

    ret
} // method

History

  • 24th June, 2002: Initial version

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Web Developer
United Kingdom United Kingdom
Richard Birkby is a software engineer from London, UK, specializing in .Net. Richard has coded for many different sized companies from small venture-capital funded start-ups, to multi-national corporations (ie Microsoft). When he's not programming, he enjoys driving his sports car or eating curry (although never at the same time!).

Richard helps run CurryPages.com and has several other covert ventures in development. Stay tuned!

Comments and Discussions

 
QuestionDid you... Pin
Drew Noakes20-Mar-08 3:49
Drew Noakes20-Mar-08 3:49 
GeneralMSIL to HEX Pin
les halles8-Mar-04 5:11
les halles8-Mar-04 5:11 
AnswerRe: MSIL to HEX Pin
Ri Qen-Sin21-Jan-07 13:36
Ri Qen-Sin21-Jan-07 13:36 
GeneralThis is very nice. Pin
Marc Clifton26-Dec-02 10:09
mvaMarc Clifton26-Dec-02 10:09 
GeneralHelp!, I need to know how to use msil Pin
Sungkyou Che1-Aug-02 18:59
sussSungkyou Che1-Aug-02 18:59 
GeneralRe: Help!, I need to know how to use msil Pin
Barrett80812-Aug-02 11:36
Barrett80812-Aug-02 11:36 
GeneralIt don't run Pin
sultan_of_6string21-Jun-02 0:33
sultan_of_6string21-Jun-02 0:33 
GeneralRe: It don't run Pin
Richard Birkby21-Jun-02 7:03
Richard Birkby21-Jun-02 7:03 
GeneralRe: You suck Pin
Richard Birkby30-May-02 3:06
Richard Birkby30-May-02 3:06 
GeneralPenis envy from a VB programmer? Pin
24-Jun-02 10:47
suss24-Jun-02 10:47 
Generalinteresting... Pin
19-Dec-01 6:33
suss19-Dec-01 6:33 
GeneralRe: interesting... Pin
Chris Losinger19-Dec-01 6:40
professionalChris Losinger19-Dec-01 6:40 
GeneralRe: interesting... Pin
18-Jun-02 20:15
suss18-Jun-02 20:15 
GeneralRe: interesting... Pin
Joel Matthias24-Jun-02 5:11
Joel Matthias24-Jun-02 5:11 
GeneralRe: interesting... Pin
Christian Graus24-Jun-02 13:00
protectorChristian Graus24-Jun-02 13:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.