Skip to content

Instantly share code, notes, and snippets.

@sklivvz
Last active May 18, 2016 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sklivvz/cbe958e57a28826baa7d34a3f031530d to your computer and use it in GitHub Desktop.
Save sklivvz/cbe958e57a28826baa7d34a3f031530d to your computer and use it in GitHub Desktop.
Solving the rover problem in IL
.assembly extern mscorlib {}
.assembly rover
{
.ver 1:0:1:0
}
// to compile, open visualstudio cmd, "ilasm rover.il"
.module rover.exe
.method static void main() cil managed
{
.maxstack 5
.entrypoint
.locals init (int32[] m, string inputs, int32 i, bool comp_res, int32 x, int32 y, int32 o, int32 cur_char, int32 length)
// create m and set to initial value
ldc.i4.5
newarr [mscorlib]System.Int32
dup
ldc.i4.1
ldc.i4.1
stelem.i4
dup
ldc.i4.3
ldc.i4.m1
stelem.i4
stloc.0
// input string
ldstr "5 5\r\n1 2 N\r\nLMLMLMLMM\r\n3 3 E\r\nMMRMMRMRRM"
stloc.1
// input length
ldloc.s inputs
callvirt instance int32 [mscorlib]System.String::get_Length()
stloc.s length
// skip until 0x0A
ldc.i4.0
stloc.s i
next_line:
ldloc.s i
ldloc.s length
ceq
brtrue end_prog
ldloc.1
ldloc.s i
callvirt instance char [mscorlib]System.String::get_Chars(int32)
ldc.i4.s 10
ceq
stloc.s comp_res
ldloc.s i
ldc.i4.1
add
stloc.s i
ldloc.s comp_res
brfalse.s next_line
ldloc.s i
ldloc.s length
ldc.i4.m1
add
ceq
brtrue end_prog
// read x
ldloc.1
ldloca.s i
call int32 read_uint(string, int32 &)
stloc.s x
// skip space
ldloc.s i
ldc.i4.1
add
stloc.s i
// read y
ldloc.1
ldloca.s i
call int32 read_uint(string, int32 &)
stloc.s y
// skip space
ldloc.s i
ldc.i4.1
add
stloc.s i
// read the orientation
ldloc.1
ldloc.s i
callvirt instance char [mscorlib]System.String::get_Chars(int32)
stloc.s cur_char
ldloc.s cur_char
ldc.i4.s 78 // 'N'
ceq
brfalse.s next_orientation_1
ldc.i4.0
stloc.s o
br orientation_end
next_orientation_1:
ldloc.s cur_char
ldc.i4.s 69 // 'E'
ceq
brfalse.s next_orientation_2
ldc.i4.1
stloc.s o
br orientation_end
next_orientation_2:
ldloc.s cur_char
ldc.i4.s 83 // 'S'
ceq
brfalse.s next_orientation_3
ldc.i4.2
stloc.s o
br orientation_end
next_orientation_3:
ldc.i4.3
stloc.s o
orientation_end:
// skip space x2
ldloc.s i
ldc.i4.3
add
stloc.s i
// main exec
main_exec:
ldloc.1
ldloc.s i
callvirt instance char [mscorlib]System.String::get_Chars(int32)
stloc.s cur_char
ldloc.s cur_char
ldc.i4.s 76 // 'L'
ceq
brfalse.s try_m
ldloc.s o
ldc.i4.m1
add
ldc.i4.3
and
stloc.s o
br.s next_i
try_m:
ldloc.s cur_char
ldc.i4.s 77 // 'M'
ceq
brfalse.s try_r
ldloc.0
ldloc.s o
ldelem.i4
ldloc.s x
add
stloc.s x
ldloc.0
ldloc.s o
ldc.i4.1
add
ldelem.i4
ldloc.s y
add
stloc.s y
br.s next_i
try_r:
ldloc.s cur_char
ldc.i4.s 82 // 'R'
ceq
brfalse.s main_end
ldloc.s o
ldc.i4.1
add
ldc.i4.3
and
stloc.s o
next_i:
ldloc.2
ldc.i4.1
add
stloc.2
ldloc.2
ldloc.s length
ceq
brfalse main_exec
ldloc.s x
ldloc.s y
ldloc.s o
call void report (int32, int32, int32)
br end_prog
main_end:
ldloc.s x
ldloc.s y
ldloc.s o
call void report (int32, int32, int32)
br next_line
end_prog:
ret
}
.method public static void report (int32, int32, int32) cil managed
{
ldarg.0
call void [mscorlib]System.Console::Write (int32)
ldc.i4.s 32
call void [mscorlib]System.Console::Write (char)
ldarg.1
call void [mscorlib]System.Console::Write (int32)
ldc.i4.s 32
call void [mscorlib]System.Console::Write (char)
ldstr "NESW"
ldarg.2
callvirt instance char [mscorlib]System.String::get_Chars(int32)
call void [mscorlib]System.Console::WriteLine (char)
ret
}
.method public static int32 read_uint (string, int32 &) cil managed
{
.maxstack 2
.locals init (string inputs, int32 i, int32 x)
ldarg.0
stloc.0
ldarg.1
ldind.i4
stloc.1
read_uint_loop:
ldloc.s x
ldc.i4.s 10
mul
stloc.s x
ldloc.s inputs
ldloc.s i
callvirt instance char [mscorlib]System.String::get_Chars(int32)
ldc.i4.s 48
sub
ldloc.s x
add
stloc.s x
ldloc.s i
ldc.i4.1
add
stloc.s i
ldloc.s inputs
ldloc.s i
callvirt instance char [mscorlib]System.String::get_Chars(int32)
ldc.i4.s 32
ceq
brtrue.s read_uint_exit
br.s read_uint_loop
read_uint_exit:
ldarg.1
ldloc.1
stind.i4
ldloc.2
ret
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment