Tôi có đoạn mã sau trong C#Tôi có hiểu mã MSIL này chính xác không?
// test.Program
private static void Main()
{
int x = 5;
int y = 100;
Console.WriteLine(y + ", " + x);
}
Và tôi đọc mã IL, tôi chưa bao giờ được lập trình lắp ráp trước vì vậy tôi hỏi nếu những gì tôi từng dòng làm là đúng.
.method private hidebysig static
void Main() cil managed
{
// Method begins at RVA 0x2058
// Code size 33 (0x21)
.maxstack 3 // maximum stack in this method is 3
.entrypoint // method is initial entry point
.locals init (// reserves memory for x and y variables
[0] int32 x, // x variable is reserved on position 0 of the stack
[1] int32 y // y variable is reserved on position 1 of the stack
)
IL_0000: ldc.i4.5 // integer of 4 bytes in size and the value of 5 is loaded onto the evaluation stack position 0
IL_0001: stloc.0 // put evaluation stack position 0 into the stack position 0, the evaluation stack is emptied
IL_0002: ldc.i4.s 100 // integer of 4 bytes in size and the value of 100 is loaded onto the evaluation stack position 0
IL_0004: stloc.1 // put evaluation stack position 0 onto the stack position 1, the evaluation stack is emptied
IL_0005: ldloc.1 // load stack position 1 into the evaluation stack position 0
IL_0006: box [mscorlib]System.Int32 // box last valuetype placed on evaluation stack, replace valuetype with reference on evaluation stack position 0, do not empty stack
IL_000b: ldstr ", " // put reference to string on evaluation stack position 1
IL_0010: ldloc.0 // load stack position 0 into the evaluation stack position 2
IL_0011: box [mscorlib]System.Int32 // box last valuetype placed on evaluation stack, replace valuetype with reference on evaluation stack position 0, do not empty stack
IL_0016: call string [mscorlib]System.String::Concat(object, object, object) // call Concat, pass values on evaluation stack, empty evaluation stack, put result of concat on evaluationstack
IL_001b: call void [mscorlib]System.Console::WriteLine(string) // pass first value in evaluation stack
IL_0020: ret // return
} // end of method Program::Main
Tôi có hiểu chương trình này chính xác không?
Khi n số vị trí được tiêu thụ bởi một cái gì đó (gọi ví dụ), chỉ có vị trí hàng đầu được tiêu thụ phải không? Và sau đó đặt một số giá trị trở lại trên ngăn xếp đánh giá? Tôi đoán điều này là đúng, bởi vì nó là một 'chồng' sau khi tất cả, nhưng tôi chỉ yêu cầu để chắc chắn. – ProgrammerAtWork
Có. Một hoạt động giả định có thể đọc A, B, C, D và tạo ra Y, Z sẽ bật 4 mục từ ngăn xếp *, tính kết quả *, và sau đó sẽ đẩy 2 mục mới vào nó. (*) không cần thiết theo thứ tự đó, nhưng việc đẩy luôn là lần cuối cùng. – quetzalcoatl