Skip to content

Instantly share code, notes, and snippets.

@puffnfresh
Created February 18, 2010 01:40
Show Gist options
  • Select an option

  • Save puffnfresh/307239 to your computer and use it in GitHub Desktop.

Select an option

Save puffnfresh/307239 to your computer and use it in GitHub Desktop.
Hand coded factorial program in CIL assembly
.assembly 'fact'
{
}
.namespace Factorial
{
.class public Factorial
{
.method public static void Main(string[] argv)
{
.entrypoint
ldarg.0
ldlen
brtrue MAIN
ldstr "Usage: fact.exe n"
call void class [mscorlib]System.Console::WriteLine(string)
ret
MAIN:
ldarg.0
ldc.i4.0
ldelem.ref
call int32 class [mscorlib]System.Convert::ToInt32(string)
call int32 class Factorial.Factorial::Fact(int32)
call void class [mscorlib]System.Console::WriteLine(int32)
ret
}
.method private static int32 Fact(int32 n)
{
ldarg.0
ldc.i4.0
bgt NONZERO
ldc.i4.1
ret
NONZERO:
ldarg.0
ldarg.0
ldc.i4.1
sub
call int32 class Factorial.Factorial::Fact(int32)
mul
ret
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment