2019-12-07 19:26:41 +00:00
|
|
|
defmodule AssemblerTest do
|
|
|
|
use ExUnit.Case
|
|
|
|
doctest Assembler
|
2019-12-08 00:00:46 +00:00
|
|
|
import Assembler
|
2019-12-07 19:26:41 +00:00
|
|
|
|
|
|
|
test "assembles empty program" do
|
2019-12-08 00:00:46 +00:00
|
|
|
assert assemble("") == []
|
|
|
|
end
|
|
|
|
|
|
|
|
test "assembles instructions correctly" do
|
2019-12-08 03:47:48 +00:00
|
|
|
assert assemble("add 1, 2, 3") == [1101, 1, 2, 3]
|
|
|
|
assert assemble("mul 1, 2, 3") == [1102, 1, 2, 3]
|
|
|
|
assert assemble("in 7") == [3, 7]
|
|
|
|
assert assemble("out 7") == [104, 7]
|
|
|
|
assert assemble("out $7") == [4, 7]
|
|
|
|
assert assemble("jnz 1, 2") == [1105, 1, 2]
|
|
|
|
assert assemble("jnz $1, 2") == [1005, 1, 2]
|
|
|
|
assert assemble("jez 1, 2") == [1106, 1, 2]
|
|
|
|
assert assemble("jez $1, 2") == [1006, 1, 2]
|
|
|
|
assert assemble("clt 1, 2, 3") == [1107, 1, 2, 3]
|
|
|
|
assert assemble("clt $1, $2, $3") == [7, 1, 2, 3]
|
|
|
|
assert assemble("ceq 1, 2, 3") == [1108, 1, 2, 3]
|
|
|
|
assert assemble("ceq $1, $2, $3") == [8, 1, 2, 3]
|
|
|
|
assert assemble("hlt") == [99]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "assembles macros" do
|
|
|
|
assert assemble("mov 1, 2") == [1101, 0, 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]
|
2019-12-07 19:26:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "assembles simple program" do
|
|
|
|
program = """
|
2019-12-08 03:47:48 +00:00
|
|
|
add 1, 2, 10
|
|
|
|
mul 2, 3, 11
|
|
|
|
add $10, $11, 12
|
|
|
|
hlt
|
2019-12-07 19:26:41 +00:00
|
|
|
"""
|
|
|
|
|
2019-12-08 00:00:46 +00:00
|
|
|
assert assemble(program) == [
|
2019-12-07 19:26:41 +00:00
|
|
|
1101,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
10,
|
|
|
|
1102,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
11,
|
|
|
|
1,
|
|
|
|
10,
|
|
|
|
11,
|
|
|
|
12,
|
|
|
|
99
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "assembles simple program with a label" do
|
|
|
|
program = """
|
|
|
|
add 1, 2, 10
|
|
|
|
jnz $10, nonZero
|
|
|
|
nonZero:
|
|
|
|
hlt
|
|
|
|
"""
|
|
|
|
|
2019-12-08 00:00:46 +00:00
|
|
|
assert assemble(program) == [
|
2019-12-07 19:26:41 +00:00
|
|
|
1101,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
10,
|
|
|
|
1005,
|
|
|
|
10,
|
|
|
|
7,
|
|
|
|
99
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "assembles a simple program with a data label" do
|
|
|
|
program = """
|
|
|
|
add 1, 2, var
|
|
|
|
out $var
|
|
|
|
var: 0
|
|
|
|
"""
|
|
|
|
|
2019-12-08 00:00:46 +00:00
|
|
|
assert assemble(program) == [
|
2019-12-07 19:26:41 +00:00
|
|
|
1101,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
6,
|
|
|
|
4,
|
|
|
|
6,
|
|
|
|
0
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "assembles a program with a _self label" do
|
|
|
|
program = """
|
|
|
|
add 1, 2, _self
|
|
|
|
"""
|
|
|
|
|
2019-12-08 00:00:46 +00:00
|
|
|
assert assemble(program) == [1101, 1, 2, 3]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "assembles parameters with expressions" do
|
2019-12-08 03:47:48 +00:00
|
|
|
assert assemble("add 1, _self + 1, 3") == [1101, 1, 3, 3]
|
|
|
|
assert assemble("add 1, $(_self + 1), 3") == [101, 1, 3, 3]
|
2019-12-08 00:00:46 +00:00
|
|
|
|
|
|
|
assert assemble("""
|
|
|
|
add 1, label + 4 - 2, 3
|
|
|
|
label: 42
|
|
|
|
""") == [1101, 1, 6, 3, 42]
|
|
|
|
|
|
|
|
assert assemble("""
|
|
|
|
add 1, $(label + 4 - 2), 3
|
|
|
|
label: 42
|
|
|
|
""") == [101, 1, 6, 3, 42]
|
2019-12-07 19:26:41 +00:00
|
|
|
end
|
|
|
|
end
|