Created
February 18, 2010 01:40
-
-
Save puffnfresh/307239 to your computer and use it in GitHub Desktop.
Hand coded factorial program in CIL assembly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .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