2019-12-06 03:48:17 +00:00
|
|
|
defmodule Assembler do
|
|
|
|
@asm """
|
2019-12-07 19:26:41 +00:00
|
|
|
in res
|
|
|
|
clt $res, 8 cmpRes
|
|
|
|
jnz $cmpRes, lessThan
|
|
|
|
ceq $res, 8, cmpRes
|
|
|
|
jnz $cmpRes, equal
|
|
|
|
out 1001
|
2019-12-06 03:48:17 +00:00
|
|
|
hlt
|
|
|
|
|
|
|
|
lessThan:
|
2019-12-07 19:26:41 +00:00
|
|
|
out 999
|
2019-12-06 03:48:17 +00:00
|
|
|
hlt
|
|
|
|
|
|
|
|
equal:
|
2019-12-07 19:26:41 +00:00
|
|
|
out 1000
|
2019-12-06 03:48:17 +00:00
|
|
|
hlt
|
2019-12-07 19:26:41 +00:00
|
|
|
|
|
|
|
res: 0
|
|
|
|
cmpRes: 0
|
2019-12-06 03:48:17 +00:00
|
|
|
"""
|
|
|
|
def test do
|
|
|
|
@asm
|
|
|
|
|> IO.inspect()
|
2019-12-07 19:26:41 +00:00
|
|
|
|> assemble()
|
2019-12-06 03:48:17 +00:00
|
|
|
end
|
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
def assemble(asm) do
|
2019-12-06 03:48:17 +00:00
|
|
|
{memory, labels} =
|
|
|
|
asm
|
|
|
|
|> String.split("\n")
|
|
|
|
|> Enum.reject(&(String.length(&1) == 0))
|
|
|
|
|> Enum.reduce({[], %{}}, fn line, {memory, labels} ->
|
|
|
|
cond do
|
|
|
|
Regex.match?(~r/^\s+$/, line) ->
|
|
|
|
{memory, labels}
|
|
|
|
|
|
|
|
String.starts_with?(line, " ") ->
|
|
|
|
{
|
|
|
|
assemble_instruction(String.slice(line, 2..-1), memory),
|
|
|
|
labels
|
|
|
|
}
|
|
|
|
|
|
|
|
String.ends_with?(line, ":") ->
|
|
|
|
{
|
|
|
|
memory,
|
|
|
|
Map.put(labels, String.slice(line, 0..-2), length(memory))
|
|
|
|
}
|
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
Regex.match?(~r/^\w+: \d+$/, line) ->
|
|
|
|
[name, value] = String.split(line, ": ")
|
|
|
|
value = String.to_integer(value)
|
|
|
|
|
|
|
|
{
|
|
|
|
[value | memory],
|
|
|
|
Map.put(labels, name, length(memory))
|
|
|
|
}
|
|
|
|
|
2019-12-06 03:48:17 +00:00
|
|
|
true ->
|
|
|
|
IO.inspect("Ignoring line: #{line}")
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
# IO.inspect(labels)
|
|
|
|
|
|
|
|
memory
|
|
|
|
|> Enum.reverse()
|
|
|
|
|> Enum.with_index()
|
|
|
|
|> Enum.map(fn
|
|
|
|
{{:label, name}, index} -> get_label(name, index, labels)
|
|
|
|
{it, _} -> it
|
|
|
|
end)
|
|
|
|
end
|
2019-12-06 03:48:17 +00:00
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
@doc """
|
|
|
|
Get the address of a label.
|
2019-12-06 03:48:17 +00:00
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
`_self` is a special label that resolves to the address of where it's being inserted into the program.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
iex> Assembler.get_label("test", 1, %{"test" => 14})
|
|
|
|
14
|
|
|
|
iex> Assembler.get_label("_self", 1, %{})
|
|
|
|
1
|
|
|
|
"""
|
|
|
|
def get_label(name, index, labels)
|
|
|
|
|
|
|
|
def get_label("_self", index, _) do
|
|
|
|
index
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_label(name, _, labels) do
|
|
|
|
Map.fetch!(labels, name)
|
2019-12-06 03:48:17 +00:00
|
|
|
end
|
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
@doc """
|
|
|
|
Raises the base to to the given power.
|
2019-12-06 03:48:17 +00:00
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
## Examples
|
|
|
|
iex> Assembler.pow(10, 3)
|
|
|
|
1000
|
|
|
|
iex> Assembler.pow(5, 4)
|
|
|
|
625
|
|
|
|
"""
|
2019-12-06 03:48:17 +00:00
|
|
|
def pow(base, 1), do: base
|
|
|
|
def pow(base, exp), do: base * pow(base, exp - 1)
|
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
@doc """
|
|
|
|
Parses assembly instruction parameter values and modes.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
iex> Assembler.parse_params("1, 2, 3", [:read, :read, :read])
|
|
|
|
{[1, 2, 3], 11100}
|
|
|
|
iex> Assembler.parse_params("1, 2, 3", [:read, :read, :write])
|
|
|
|
{[1, 2, 3], 1100}
|
|
|
|
iex> Assembler.parse_params("1, 2, $3", [:read, :read, :write])
|
|
|
|
{[1, 2, 3], 1100}
|
|
|
|
iex> Assembler.parse_params("$1, 2, 3", [:read, :read, :write])
|
|
|
|
{[1, 2, 3], 1000}
|
|
|
|
iex> Assembler.parse_params("$label, 2, 3", [:read, :read, :write])
|
|
|
|
{[{:label, "label"}, 2, 3], 1000}
|
|
|
|
iex> Assembler.parse_params("1, label, 3", [:read, :read, :write])
|
|
|
|
{[1, {:label, "label"}, 3], 1100}
|
|
|
|
"""
|
2019-12-06 03:48:17 +00:00
|
|
|
def parse_params(params, param_types) do
|
|
|
|
params
|
2019-12-07 19:26:41 +00:00
|
|
|
|> String.split(",")
|
|
|
|
|> Enum.map(&String.trim/1)
|
2019-12-06 03:48:17 +00:00
|
|
|
|> Enum.zip(param_types)
|
|
|
|
|> Enum.with_index()
|
|
|
|
|> Enum.map_reduce(0, fn {{param, type}, index}, modes ->
|
|
|
|
val =
|
2019-12-07 19:26:41 +00:00
|
|
|
case Regex.run(~r/^\$?(\d+)$/, param) do
|
2019-12-06 03:48:17 +00:00
|
|
|
[_, digits] ->
|
|
|
|
String.to_integer(digits)
|
|
|
|
|
|
|
|
_ ->
|
2019-12-07 19:26:41 +00:00
|
|
|
[_, name] = Regex.run(~r/^\$?(\w+)$/, param)
|
2019-12-06 03:48:17 +00:00
|
|
|
{:label, name}
|
|
|
|
end
|
|
|
|
|
|
|
|
modes =
|
2019-12-07 19:26:41 +00:00
|
|
|
case type do
|
|
|
|
:read ->
|
|
|
|
if String.starts_with?(param, "$") do
|
|
|
|
modes
|
|
|
|
else
|
|
|
|
modes + pow(10, index + 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
modes
|
2019-12-06 03:48:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
{val, modes}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
@doc """
|
|
|
|
Assembles the given instruction and adds it to the memory (in reverse order).
|
|
|
|
"""
|
2019-12-06 03:48:17 +00:00
|
|
|
def assemble_instruction(insn, memory \\ [])
|
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
@doc """
|
|
|
|
Assemble add instruction.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
iex> Assembler.assemble_instruction("add 1, 2, 3")
|
|
|
|
[3, 2, 1, 1101]
|
|
|
|
"""
|
2019-12-06 03:48:17 +00:00
|
|
|
def assemble_instruction("add " <> params, memory) do
|
|
|
|
{[a, b, dest], modes} = parse_params(params, [:read, :read, :write])
|
|
|
|
|
|
|
|
[dest, b, a, 1 + modes | memory]
|
|
|
|
end
|
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
@doc """
|
|
|
|
Assemble multiply instruction.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
iex> Assembler.assemble_instruction("mul 1, 2, 3")
|
|
|
|
[3, 2, 1, 1102]
|
|
|
|
"""
|
2019-12-06 03:48:17 +00:00
|
|
|
def assemble_instruction("mul " <> params, memory) do
|
|
|
|
{[a, b, dest], modes} = parse_params(params, [:read, :read, :write])
|
|
|
|
|
|
|
|
[dest, b, a, 2 + modes | memory]
|
|
|
|
end
|
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
@doc """
|
|
|
|
Assemble halt instruction.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
iex> Assembler.assemble_instruction("hlt")
|
|
|
|
[99]
|
|
|
|
"""
|
2019-12-06 03:48:17 +00:00
|
|
|
def assemble_instruction("hlt", memory) do
|
|
|
|
[99 | memory]
|
|
|
|
end
|
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
@doc """
|
|
|
|
Assemble input instruction.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
iex> Assembler.assemble_instruction("in 7")
|
|
|
|
[7, 3]
|
|
|
|
"""
|
2019-12-06 03:48:17 +00:00
|
|
|
def assemble_instruction("in " <> params, memory) do
|
|
|
|
{[dest], modes} = parse_params(params, [:write])
|
|
|
|
|
|
|
|
[dest, 3 + modes | memory]
|
|
|
|
end
|
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
@doc """
|
|
|
|
Assemble input instruction.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
iex> Assembler.assemble_instruction("out 7")
|
|
|
|
[7, 104]
|
|
|
|
iex> Assembler.assemble_instruction("out $7")
|
|
|
|
[7, 4]
|
|
|
|
"""
|
2019-12-06 03:48:17 +00:00
|
|
|
def assemble_instruction("out " <> params, memory) do
|
|
|
|
{[val], modes} = parse_params(params, [:read])
|
|
|
|
|
|
|
|
[val, 4 + modes | memory]
|
|
|
|
end
|
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
@doc """
|
|
|
|
Assemble jump-if-non-zero instruction.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
iex> Assembler.assemble_instruction("jnz 1, 2")
|
|
|
|
[2, 1, 105]
|
|
|
|
iex> Assembler.assemble_instruction("jnz $var, 2")
|
|
|
|
[2, {:label, "var"}, 5]
|
|
|
|
"""
|
2019-12-06 03:48:17 +00:00
|
|
|
def assemble_instruction("jnz " <> params, memory) do
|
|
|
|
{[target, dest], modes} = parse_params(params, [:read, :read])
|
|
|
|
|
|
|
|
[dest, target, 5 + modes | memory]
|
|
|
|
end
|
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
@doc """
|
|
|
|
Assemble jump-if-zero instruction.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
iex> Assembler.assemble_instruction("jez 1, 2")
|
|
|
|
[2, 1, 106]
|
|
|
|
iex> Assembler.assemble_instruction("jez $var, 2")
|
|
|
|
[2, {:label, "var"}, 6]
|
|
|
|
"""
|
2019-12-06 03:48:17 +00:00
|
|
|
def assemble_instruction("jez " <> params, memory) do
|
|
|
|
{[target, dest], modes} = parse_params(params, [:read, :read])
|
|
|
|
|
|
|
|
[dest, target, 6 + modes | memory]
|
|
|
|
end
|
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
@doc """
|
|
|
|
Assemble compare-less-than instruction.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
iex> Assembler.assemble_instruction("clt 1, 2, 3")
|
|
|
|
[3, 2, 1, 1107]
|
|
|
|
iex> Assembler.assemble_instruction("clt $1, $2, 3")
|
|
|
|
[3, 2, 1, 7]
|
|
|
|
"""
|
2019-12-06 03:48:17 +00:00
|
|
|
def assemble_instruction("clt " <> params, memory) do
|
|
|
|
{[a, b, dest], modes} = parse_params(params, [:read, :read, :write])
|
|
|
|
|
|
|
|
[dest, b, a, 7 + modes | memory]
|
|
|
|
end
|
|
|
|
|
2019-12-07 19:26:41 +00:00
|
|
|
@doc """
|
|
|
|
Assemble compare-equal instruction.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
iex> Assembler.assemble_instruction("ceq 1, 2, 3")
|
|
|
|
[3, 2, 1, 1108]
|
|
|
|
iex> Assembler.assemble_instruction("ceq $1, $2, 3")
|
|
|
|
[3, 2, 1, 8]
|
|
|
|
"""
|
2019-12-06 03:48:17 +00:00
|
|
|
def assemble_instruction("ceq " <> params, memory) do
|
|
|
|
{[a, b, dest], modes} = parse_params(params, [:read, :read, :write])
|
|
|
|
|
|
|
|
[dest, b, a, 8 + modes | memory]
|
|
|
|
end
|
|
|
|
end
|