我正在教自己CIL并且到目前为止一直做得很好(昨天真的开始)但我遇到了一个我无法弄清楚的问题.我正在提示用户输入一个int(int32),然后将其存储并转换为浮点并显示它.然而,无论我输入什么都是不同的浮动.这是我的代码:
.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
}
Run Code Online (Sandbox Code Playgroud)
我只是在和CIL搞砸,但我觉得为了清晰起见,我会把整个例子都扔进去.它编译得很好但是当我输入5时它返回53作为浮点数和转换后的int32.
有人可以说明我做错了什么!
编辑:感谢Marc Gravell,我能够弄明白.对于那些感兴趣的人,这里是正确的代码:
.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
}
Run Code Online (Sandbox Code Playgroud)
Console.Read返回Unicode代码点,或EOF返回-1.53是字符的代码点(不是整数)'5'.
你也许可以使用Console.ReadLine和int.Parse.