Intcode assembler: Add more macros

This commit is contained in:
Shadowfacts 2019-12-08 13:48:27 -05:00
parent 2a1c657b9c
commit e1e617de5e
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 39 additions and 11 deletions

View File

@ -214,6 +214,11 @@ defmodule Assembler do
add #{a}, $#{dest}, #{dest} add #{a}, $#{dest}, #{dest}
""" """
assemble_macro ["not", src, dest],
"""
sub 1, #{src}, #{dest}
"""
assemble_macro ["cle", a, b, dest], assemble_macro ["cle", a, b, dest],
""" """
clt #{a}, #{b}, #{dest} clt #{a}, #{b}, #{dest}
@ -225,16 +230,14 @@ defmodule Assembler do
assemble_macro ["cgt", a, b, dest], assemble_macro ["cgt", a, b, dest],
""" """
clt #{a}, #{b}, #{dest} cle #{a}, #{b}, #{dest}
jnz $#{dest}, false not $#{dest}, #{dest}
ceq #{a}, #{b}, #{dest} """
jnz $#{dest}, false
mov 1, #{dest}
jmp end
false: assemble_macro ["cge", a, b, dest],
mov 0, #{dest} """
end: clt #{a}, #{b}, #{dest}
not $#{dest}, #{dest}
""" """
@doc """ @doc """
@ -262,9 +265,9 @@ defmodule Assembler do
iex> Assembler.parse_params(["$1", "2", "3"], [:read, :read, :write]) iex> Assembler.parse_params(["$1", "2", "3"], [:read, :read, :write])
{[1, 2, 3], 1000} {[1, 2, 3], 1000}
iex> Assembler.parse_params(["$label", "2", "3"], [:read, :read, :write]) iex> Assembler.parse_params(["$label", "2", "3"], [:read, :read, :write])
{[{:expr, "label"}, 2, 3], 1000} {[{:expr, ["label"]}, 2, 3], 1000}
iex> Assembler.parse_params(["1", "label", "3"], [:read, :read, :write]) iex> Assembler.parse_params(["1", "label", "3"], [:read, :read, :write])
{[1, {:expr, "label"}, 3], 1100} {[1, {:expr, ["label"]}, 3], 1100}
""" """
def parse_params(params, param_types) do def parse_params(params, param_types) do
params params

View File

@ -28,6 +28,31 @@ defmodule AssemblerTest do
assert assemble("mov 1, 2") == [1101, 0, 1, 2] assert assemble("mov 1, 2") == [1101, 0, 1, 2]
assert assemble("jmp 2") == [1105, 1, 2] assert assemble("jmp 2") == [1105, 1, 2]
assert assemble("sub 5, 3, res\nres: 0") == [1102, 3, -1, 8, 101, 5, 8, 8, 0] assert assemble("sub 5, 3, res\nres: 0") == [1102, 3, -1, 8, 101, 5, 8, 8, 0]
assert assemble("not 4, 4") == [1102, 4, -1, 4, 101, 1, 4, 4]
assert assemble("cle 1, 2, res\nres: 0") == [1107, 1, 2, 11, 1005, 11, 11, 1108, 1, 2, 11, 0]
assert assemble("cgt 1, 2, res\nres: 0") == [
1107,
1,
2,
19,
1005,
19,
11,
1108,
1,
2,
19,
1002,
19,
-1,
19,
101,
1,
19,
19,
0
]
end end
test "assembles simple program" do test "assembles simple program" do