-
-
Save orange-in-space/5bc1fb65fa9bfa12ebd48c40e586d77d to your computer and use it in GitHub Desktop.
sqrt CIL
This file contains 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 sqrt_orange{} | |
.method public static void main(){ | |
.entrypoint | |
ldstr "Sqrt(2)=" | |
call void [mscorlib]System.Console::Write(string) | |
ldc.r8 2 | |
call float64 sqrt(float64) | |
call void [mscorlib]System.Console::WriteLine(float64) | |
ret | |
} | |
.method public static float64 sqrt(float64){ | |
.maxstack 5 | |
.locals init ( | |
[0] float64 Left, | |
[1] float64 Right | |
) | |
//Left=0; | |
ldc.r8 0.0 | |
stloc.0 | |
//Right= 1 arg +; | |
ldc.r8 1 | |
ldarg.0 // | |
add | |
stloc.1 | |
br loop_begin //goto loop_begin | |
loop_next: // pop prev. Mid( = StackAllClear) | |
pop | |
loop_begin: | |
//Mid= | |
ldloc.0 | |
ldloc.1 | |
add | |
ldc.r8 2 | |
div | |
// F Mid | |
dup | |
dup | |
dup | |
//debug begin(Show Mid) | |
//dup | |
//call void [mscorlib]System.Console::WriteLine(float64) | |
//debug end | |
// F Mid,Mid,Mid,Mid | |
mul | |
// F Square,Mid,Mid | |
ldarg.0 | |
// F arg,Square,Mid,Mid | |
bgt to_right //if Square<arg then goto to_right | |
// F Mid,Mid | |
to_left: | |
stloc.0 // Left=Mid | |
br loop_check //goto loop_check | |
to_right: | |
stloc.1 // Right=Mid | |
// F Mid | |
loop_check: | |
ldloc.1 | |
ldloc.0 | |
sub // F (Right-Left) Mid | |
ldc.r8 1E-06 // F 0.000001d, (Right-Left) | |
bgt loop_next // if (Right-Left) > 0.000001d then goto loop_next | |
loop_end: | |
ret | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment