vararg.il
来自「< Microsoft .NET IL汇编语言程序设计>>书上」· IL 代码 · 共 70 行
IL
70 行
.assembly extern mscorlib { }
.assembly Vararg {}
.module Vararg.exe
// Compute sum of undefined number of arguments
.method public static vararg unsigned int64 Sum(/* all arguments optional */)
{
.locals init(valuetype [mscorlib]System.ArgIterator Args,
unsigned int64 Sum,
int32 NumArgs)
ldc.i8 0
stloc Sum
ldloca Args
arglist // Create argument list structure
// Initialize ArgIterator with this structure:
call instance void [mscorlib]System.ArgIterator::.ctor(
value class [mscorlib]System.RuntimeArgumentHandle)
// Get the optional argument count:
ldloca Args
call instance int32 [mscorlib]System.ArgIterator::GetRemainingCount()
stloc NumArgs
// Main cycle:
LOOP:
ldloc NumArgs
brfalse RETURN // if(NumArgs == 0) goto RETURN;
// Get next argument:
ldloca Args
call instance typedref [mscorlib]System.ArgIterator::GetNextArg()
// Interpret it as unsigned int64:
refanyval [mscorlib]System.UInt64
ldind.u8
// Add it to Sum:
ldloc Sum
add
stloc Sum // Sum += *((int64*)&next_arg)
// Decrease NumArgs and go for next argument:
ldloc NumArgs
ldc.i4.m1
add
stloc NumArgs
br LOOP
RETURN:
ldloc Sum
ret
}
.method public static void Exec( )
{
.entrypoint
// Compute 123+234+345+567 and print result upon console
ldc.i8 123
ldc.i8 234
ldc.i8 345
ldc.i8 567
call vararg unsigned int64 Sum(..., unsigned int64,
unsigned int64, unsigned int64, unsigned int64)
call void [mscorlib]System.Console::WriteLine(unsigned int64)
ret
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?