Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ldc.i4.s and ldc.i4?

Tags:

c#

.net

cil

il

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?

like image 917
Pratik Singhal Avatar asked Feb 03 '14 04:02

Pratik Singhal


1 Answers

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

like image 184
leppie Avatar answered Oct 06 '22 01:10

leppie