From 5742e1f66c4e70151865de7092547223898bbf6b Mon Sep 17 00:00:00 2001 From: Julian Blake Kongslie Date: Sun, 18 Apr 2021 16:00:54 -0700 Subject: Support a proper PDP-8 assembler. --- asm.rb | 70 ------------------------------------------------------------------ 1 file changed, 70 deletions(-) delete mode 100755 asm.rb (limited to 'asm.rb') diff --git a/asm.rb b/asm.rb deleted file mode 100755 index 6da40fb..0000000 --- a/asm.rb +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/ruby -w - -OPCODES = { - "i" => 0x080, - "acc=" => 0x100, - "ladd" => 0x200, - "store" => 0x300, - "ifeq" => 0x400, - "jmp" => 0x500, - "ascii" => 0x600, - "cla" => 0x001, - "++acc" => 0x002, - "--acc" => 0x004, - "tx" => 0x040, - "rx" => 0x080, - "halt" => 0x000, - } - -Line = Struct.new(:opcode, :refs, :code) - -$labels = {} -$code = [] -ARGF.each_line() do | line | - line.chomp!() - line.sub!(/^.*\/\/\s*/, "") - next unless line =~ /\S/ - op = 0x000 - refs = [] - line.scan(/\S+/).each() do | word | - break if word =~ /^#/ - if word =~ /^0(\d+)$/ - op |= $1.to_i(8) - elsif word =~ /^-0(\d+)$/ - op |= 0x100 - $1.to_i(8) - elsif word =~ /^(\d+)$/ - op |= $1.to_i(10) - elsif word =~ /^-(\d+)$/ - op |= 0x100 - $1.to_i(10) - elsif word =~ /^0x([0-9a-f]+)$/i - op |= $1.to_i(16) - elsif word =~ /^-0x([0-9a-f]+)$/i - op |= 0x100 - $1.to_i(16) - elsif OPCODES.key?(word) - op |= OPCODES[word] - elsif word =~ /^(.+):$/ - $labels[$1] = $code.size() - else - refs << word - end - end - $code << Line.new(op, refs, line) -end - -$code.each_with_index() do | line, i | - op = line.opcode - line.refs.each() do | ref | - if ref =~ /^@(.+)$/ and $labels.key?($1) - op |= $labels[$1] - elsif $labels.key?(ref) - target = $labels[ref] - (i + 1) - throw "Jump too far forward" if target > 0x7f - target += 0x80 if target < 0 - throw "Jump too far backward" if target < 0 - op |= target - else - throw "I don't understand #{ref.inspect()}" - end - end - $stdout.write("#{op.to_s(16).rjust(3, "0")} // #{line.code}\n") -end -- cgit v1.2.3