1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
'use strict';
const { assemble } = require('./parser.js');
test('assemble the imp', () => {
expect(assemble(`
mov 0, 1
end
`)).toEqual({
start: 0,
program: [
{
opcode: 'MOV',
a: { mode: 'direct', value: 0 },
b: { mode: 'direct', value: 1 },
},
],
});
});
test('assemble dwarf', () => {
expect(assemble(`
bomb dat #0
dwarf add #4, bomb
mov bomb, @bomb
jmp dwarf
end dwarf
`)).toEqual({
start: 1,
program: [
{ opcode: 'DAT',
a: { mode: 'immediate', value: 0 },
b: { mode: 'immediate', value: 0 },
},
{ opcode: 'ADD',
a: { mode: 'immediate', value: 4 },
b: { mode: 'direct', value: -1 },
},
{ opcode: 'MOV',
a: { mode: 'direct', value: -2 },
b: { mode: 'indirect', value: -2 },
},
{ opcode: 'JMP',
a: { mode: 'direct', value: -2 },
b: { mode: 'direct', value: 0 },
},
],
});
});
test('assemble armadillo', () => {
expect(assemble(`
; armadillo
; author: stefan strack
bomb spl 0
loop add #3039, ptr
ptr mov bomb, 81
jmp loop
mov 1, <-1
end bomb
`)).toEqual({
start: 0,
program: [
{ opcode: 'SPL',
a: { mode: 'direct', value: 0 },
b: { mode: 'direct', value: 0 },
},
{ opcode: 'ADD',
a: { mode: 'immediate', value: 3039 },
b: { mode: 'direct', value: 1 },
},
{ opcode: 'MOV',
a: { mode: 'direct', value: -2 },
b: { mode: 'direct', value: 81 },
},
{ opcode: 'JMP',
a: { mode: 'direct', value: -2 },
b: { mode: 'direct', value: 0 },
},
{ opcode: 'MOV',
a: { mode: 'direct', value: 1 },
b: { mode: 'predecrement', value: -1 },
},
],
});
});
test('illegal instructions are not assembled', () => {
expect(() => assemble("dat 0, #0\nend")).toThrow();
expect(() => assemble("dat @0, #0\nend")).toThrow();
expect(() => assemble("mov 0, #0\nend")).toThrow();
expect(() => assemble("add 0, #0\nend")).toThrow();
expect(() => assemble("sub 0, #0\nend")).toThrow();
expect(() => assemble("cmp 0, #0\nend")).toThrow();
expect(() => assemble("slt 0, #0\nend")).toThrow();
expect(() => assemble("jmp #0, 0\nend")).toThrow();
expect(() => assemble("jmz #0, 0\nend")).toThrow();
expect(() => assemble("jmn #0, 0\nend")).toThrow();
expect(() => assemble("jmn #0, 0\nend")).toThrow();
expect(() => assemble("djn #0, 0\nend")).toThrow();
expect(() => assemble("spl #0, 0\nend")).toThrow();
});
|