summaryrefslogtreecommitdiff
path: root/asm.rb
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xasm.rb31
1 files changed, 15 insertions, 16 deletions
diff --git a/asm.rb b/asm.rb
index c3a0125..4883edb 100755
--- a/asm.rb
+++ b/asm.rb
@@ -2,20 +2,17 @@
2 2
3OPCODES = { 3OPCODES = {
4 "i" => 0x080, 4 "i" => 0x080,
5 "acc=" => 0x000, 5 "acc=" => 0x100,
6 "ladd" => 0x100, 6 "ladd" => 0x200,
7 "store" => 0x200, 7 "store" => 0x300,
8 "ifeq" => 0x300, 8 "ifeq" => 0x400,
9 "jmp" => 0x400, 9 "jmp" => 0x500,
10 "ascii" => 0x500, 10 "ascii" => 0x600,
11 "++acc" => 0xf01, 11 "cla" => 0x001,
12 "--acc" => 0xf02, 12 "++acc" => 0x002,
13 "++idx" => 0xf04, 13 "--acc" => 0x004,
14 "--idx" => 0xf08, 14 "tx" => 0x040,
15 "swap" => 0xf10, 15 "halt" => 0x080,
16 "idx" => 0xf20,
17 "tx" => 0xf40,
18 "halt" => 0xf80,
19 } 16 }
20 17
21Line = Struct.new(:opcode, :refs, :code) 18Line = Struct.new(:opcode, :refs, :code)
@@ -56,9 +53,11 @@ end
56$code.each_with_index() do | line, i | 53$code.each_with_index() do | line, i |
57 op = line.opcode 54 op = line.opcode
58 line.refs.each() do | ref | 55 line.refs.each() do | ref |
59 if $labels.key?(ref) 56 if ref =~ /^@(.+)$/ and $labels.key?($1)
57 op |= $labels[$1]
58 elsif $labels.key?(ref)
60 target = $labels[ref] - (i + 1) 59 target = $labels[ref] - (i + 1)
61 target += 0x100 if target < 0 60 target += 0x80 if target < 0
62 op |= target 61 op |= target
63 else 62 else
64 throw "I don't understand #{ref.inspect()}" 63 throw "I don't understand #{ref.inspect()}"