I was studying about the Intermediate Language for C#(IL) and came across the following piece of code:-
//Add.il
//Add Two Numbers
.assembly extern mscorlib {}
.assembly Add
{
.ver 1:0:1:0
}
.module add.exe
.method static void main() cil managed
{
.maxstack 2
.entrypoint
ldstr "The sum of 50 and 30 is = "
call void [mscorlib]System.Console::Write (string)
ldc.i4.s 50
ldc.i4 30
add
call void [mscorlib]System.Console::Write (int32)
ret
}
I am a beginner in understanding IL but I know the meaning of these very basic instructions used in the Main
, in the above example.
My question is that Is there any difference between the instructions ldc.i4.s
which is used to load 50 and the instruction ldc.i4
which is used to load 30 onto the evaluation stack.
How does the compiler decide which instruction to use(out of these two) and when?
For signed byte values, no.
ldc.i4.s is a more efficient encoding for pushing the integers from -128 to 127 onto the evaluation stack.
See MSDN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With