Why is Int32 switching to Float64, which leads to data changes?

I teach myself CIL and so far everything is fine (just started yesterday), but I ran into a problem that I cannot understand. I suggest the user enter int (int32) and then save it and convert to float and display it. However, everything that I enter looks like floating. Here is my code:

.assembly variables {} .method public static void main() cil managed { .entrypoint .maxstack 8 .locals init (float64) ldstr "Enter a digit: " call void [mscorlib]System.Console::WriteLine(string) call int32 [mscorlib]System.Console::Read() conv.r8 stloc.0 ldstr "as a float: " call void [mscorlib]System.Console::WriteLine(string) ldloc.0 dup call void [mscorlib]System.Console::Write(float64) stloc.0 ldstr "Stored in location 0" call void [mscorlib]System.Console::WriteLine(string) ldloc.0 conv.i4 call void [mscorlib]System.Console::WriteLine(int32) call int32 [mscorlib]System.Console::Read() // to pause before closing window pop ret } 

I simply lied to CIL, but decided that I would give up my entire example for clarity. It compiles fine, but when I type 5, it returns 53 as float AND converted int32.

Can someone shed some light on what I'm doing wrong!


EDIT: Thanks to Mark Gravell, I was able to figure this out. For those interested, here is the correct code:

 .assembly variables {} .method public static void main() cil managed { .entrypoint .maxstack 8 .locals init (float64) ldstr "Enter a digit: " call void [mscorlib]System.Console::WriteLine(string) call string [mscorlib]System.Console::ReadLine() call int32 [mscorlib]System.Int32::Parse(string) conv.r8 stloc.0 ldstr "as a float: " call void [mscorlib]System.Console::WriteLine(string) ldloc.0 dup call void [mscorlib]System.Console::Write(float64) stloc.0 ldstr "Stored in location 0" call void [mscorlib]System.Console::WriteLine(string) ldloc.0 conv.i4 call void [mscorlib]System.Console::WriteLine(int32) call int32 [mscorlib]System.Console::Read() // to pause before closing window pop ret } 
+4
source share
1 answer

Console.Read returns a Unicode code point or -1 for EOF. 53 is the character code point (not an integer) '5' .

You could use Console.ReadLine and int.Parse .

+7
source

Source: https://habr.com/ru/post/1396719/


All Articles