public
Created

  • Download Gist
gistfile1.rb
Ruby
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
#!/usr/bin/env ruby
 
class Op
attr_accessor :inst, :a, :b
attr_reader :num, :line
 
def initialize(num, line)
@num, @line = num, line
@inst, @a, @b = 0, 0, 0
@extensions = []
@references = []
end
 
def extend(value)
# Check value against max int
@extensions << value
end
 
def reference(label)
@references[@extensions.size] = label
@extensions << 0 # Placeholder
end
 
def resolve(labels)
@references.each_with_index do |label, i|
next unless label
location = labels[label]
error("Cannot find label #{label} in #{labels.keys.join(", ")}") unless location
@extensions[i] = location
end
end
 
def word
@inst + (@a << 4) + (@b << 10)
end
 
def words
[word] + @extensions.dup
end
 
def bytes
words.map{|w| [w].pack('n') }.join("")
end
 
def size
@extensions.size + 1
end
 
def hex(w)
"%04x" % w
end
 
def to_s
"#{@num}: #{words.map{|w| hex(w)}.join(" ")}" # ; #{line}"
end
 
def error(message)
raise Exception.new("Line #{num}: #{message}\n#{line}")
end
end
 
class Assembler
def self.declare(map, start, string)
count = start
string.split(" ").each do |token|
map[token] = count
count += 1
end
end
 
HEX_RE = /^0x[0-9a-fA-F]+$/
INT_RE = /^\d+$/
REG_RE = /^[A-Z]+$/
LABEL_RE = /^[a-z]+$/
INDIRECT_RE = /^\[.+\]/
INDIRECT_OFFSET_RE = /^[^+]+\+[^+]+$/
 
EXT_PREFIX = 0
 
INDIRECT = 0x08
INDIRECT_OFFSET = 0x10
INDIRECT_NEXT = 0x1e
NEXT = 0x1f
LITERAL = 0x20
 
INSTRUCTIONS = {}
EXTENDED_INSTRUCTIONS = {}
VALUES = {}
 
declare(INSTRUCTIONS, 1, "SET ADD SUB MUL DIV MOD SHL SHR AND BOR XOR IFE IFN IFG IFB")
declare(EXTENDED_INSTRUCTIONS, 1, "JSR")
declare(VALUES, 0, "A B C X Y Z I J")
declare(VALUES, 0x18, "POP PEEK PUSH SP PC O")
 
def initialize
@body = []
@label_these = []
end
 
def clean(line)
line.gsub(/;.*/, "").gsub(/,/, " ").gsub(/\s+/, " ").strip
end
 
def dehex(token)
return token.hex.to_s if HEX_RE === token
token
end
 
def parse_value(token, op)
token = dehex(token)
 
case token
when INT_RE
value = token.to_i
return LITERAL + value if value <= 31
op.extend value
return NEXT
when REG_RE
return VALUES[token]
 
when LABEL_RE
op.reference(token)
return NEXT
when INDIRECT_RE
inner = dehex(token[1..-2])
case inner
when INT_RE
value = inner.to_i
op.extend value
return INDIRECT_NEXT
 
when REG_RE
reg = VALUES[inner]
op.error("Can't use indirect addressing on non-basic reg #{reg}") unless reg <= VALUES["J"]
return INDIRECT + reg
 
when LABEL_RE
op.reference(inner)
return INDIRECT_NEXT
 
when INDIRECT_OFFSET_RE
offset, reg = inner.split("+").map{|x| x.strip }
offset = dehex(offset)
op.error("Malformed indirect offset value #{inner}") unless INT_RE === offset && REG_RE === reg
value = offset.to_i
op.extend value
return INDIRECT_OFFSET
end
end
 
op.error("Unrecognized value #{token}")
end
 
def parse_op(op, tokens)
inst_name = tokens.shift
 
inst_code = INSTRUCTIONS[inst_name]
if inst_code
op.inst = inst_code
op.a = parse_value(tokens.shift, op)
op.b = parse_value(tokens.shift, op)
return
end
 
inst_code = EXTENDED_INSTRUCTIONS[inst_name]
if inst_code
op.inst = EXT_PREFIX
op.a = inst_code
op.b = parse_value(tokens.shift, op)
end
 
raise Exception.new("No such instruction: #{inst_name}") unless inst_code
end
 
def assemble(text)
labels = {}
num = 0
location = 0
text.each_line do |line|
num += 1
op = Op.new(num, line)
 
cleaned = clean(line)
next if cleaned.empty?
 
tokens = cleaned.split(/\s/)
op.error("Wrong number of tokens") unless (2..4) === tokens.size
 
labels[tokens.shift[1..-1]] = location if tokens[0].start_with?(":")
parse_op(op, tokens)
 
@body << op
location += op.size
end
 
@body.each {|op| op.resolve(labels) }
 
display
end
 
def display
@body.each { |op| puts op }
end
 
def dump(filename)
File.open(filename, "w") do |file|
@body.each {|inst| file.write(inst.bytes) }
end
end
end
 
if __FILE__ == $PROGRAM_NAME
asm = Assembler.new
filename = "#{ARGV.first || "out.s"}.o"
asm.assemble ARGF
asm.dump filename
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.