Skip to content

Instantly share code, notes, and snippets.

@orange-in-space
Created February 15, 2018 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orange-in-space/5bc1fb65fa9bfa12ebd48c40e586d77d to your computer and use it in GitHub Desktop.
Save orange-in-space/5bc1fb65fa9bfa12ebd48c40e586d77d to your computer and use it in GitHub Desktop.
sqrt CIL
.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