概要
ilasmでstack machineやってみた。
練習問題やってみた。
練習問題
AtCoderを解け。
500円玉、100円玉、50円玉を使ってXX円を支払う方法が何通りあるかを求める。
サンプルコード
.assembly AtCoder {}
.method public static void Main() {
.entrypoint
.locals init ([0] int32 A, [1] int32 B, [2] int32 C, [3] int32 X, [4] int32 a, [5] int32 i, [6] int32 j, [7] int32 k, [8] bool, [9] bool, [10] bool, [11] bool)
call string [System.Console]System.Console::ReadLine()
call int32 [System.Runtime]System.Int32::Parse(string)
stloc.0
call string [System.Console]System.Console::ReadLine()
call int32 [System.Runtime]System.Int32::Parse(string)
stloc.1
call string [System.Console]System.Console::ReadLine()
call int32 [System.Runtime]System.Int32::Parse(string)
stloc.2
call string [System.Console]System.Console::ReadLine()
call int32 [System.Runtime]System.Int32::Parse(string)
stloc.3
ldc.i4.0
stloc.s 4
ldc.i4.0
stloc.s 5
br.s IL_0098
IL_0035:
ldc.i4.0
stloc.s 6
br.s IL_0083
IL_003b:
ldc.i4.0
stloc.s 7
br.s IL_006e
IL_0041:
ldc.i4 500
ldloc.s 5
mul
ldc.i4.s 100
ldloc.s 6
mul
add
ldc.i4.s 50
ldloc.s 7
mul
add
ldloc.3
ceq
stloc.s 8
ldloc.s 8
brfalse.s IL_0067
ldloc.s 4
ldc.i4.1
add
stloc.s 4
IL_0067:
ldloc.s 7
ldc.i4.1
add
stloc.s 7
IL_006e:
ldloc.s 7
ldloc.2
cgt
ldc.i4.0
ceq
stloc.s 9
ldloc.s 9
brtrue.s IL_0041
ldloc.s 6
ldc.i4.1
add
stloc.s 6
IL_0083:
ldloc.s 6
ldloc.1
cgt
ldc.i4.0
ceq
stloc.s 10
ldloc.s 10
brtrue.s IL_003b
ldloc.s 5
ldc.i4.1
add
stloc.s 5
IL_0098:
ldloc.s 5
ldloc.0
cgt
ldc.i4.0
ceq
stloc.s 11
ldloc.s 11
brtrue.s IL_0035
ldloc.s 4
call void [System.Console]System.Console::WriteLine(int32)
ret
}
実行結果
>at4
2
2
2
100
2
以上。