Search code examples
.netclrcilinvalidprogramexception

Program crashes when calling a function from Main


This is probably some rookie mistake but I can't find it.

ilasm says that my code generates System.InvalidProgramException. I discovered that it is thrown the moment I invoke Fibonacci() - a flag put before calling it is written to the console but another flag put inside the method just before .locals init isn't (because of the exception).

.assembly extern mscorlib { }
.assembly foo { }

.method public static int32 Fibonacci(int32 n)
{
    .locals init ([0] int32 i, [1] int32 last, [2] int32 prev)

    ldc.i4.0
    ldarg n
    brfalse done

    ldc.i4.1
    dup
    ldarg n 
    sub
    brfalse done

    ldc.i4.2
    stloc i

et1:
    dup
    stloc prev
    add
    stloc last
    ldloc prev
    ldloc last

    ldarg n
    ldloc i
    sub
    brfalse done

    ldloc i
    ldc.i4.1
    add
    stloc i
    br et1

done:
    stloc i
    pop
    ldloc i
    ret
}

.method public static void Main()
{
    .entrypoint

    ldstr "result is: {0}"
    ldstr "enter n: "
    call void [mscorlib]System.Console::Write(string)   
    call string [mscorlib]System.Console::ReadLine()    
    call int32 [mscorlib]System.Int32::Parse(string)
    call int32 Fibonacci(int32)
    box [mscorlib]System.Int32
    call void [mscorlib]System.Console::WriteLine(string,object)
    ret
}

Solution

  • In case n == 0 you take the brfalse branch with an int on the stack.

    But the done code assumes a different stack layout:

    done:
        stloc i
        pop
        ldloc i
        ret
    

    Looks like it assumes 2 elements coming in.