Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions exe/asm_to_bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env ruby
DEBUG = ENV["DEBUG"] == "true" || ENV["DEBUG"].to_i == 1

OPERATORS = DEBUG ? {
"PRT" => "0001",
"SET" => "0010",
"ADD" => "0011",
"SUB" => "0100",
"MUL" => "0101",
"DIV" => "0110",
"JMP" => "0111",
"JNP" => "1000",
"EQL" => "1001",
"CBP" => "1010",
"CLP" => "1011",
} : {
"PRT" => 1,
"SET" => 2,
"ADD" => 3,
"SUB" => 4,
"MUL" => 5,
"DIV" => 6,
"JMP" => 7,
"JNP" => 8,
"EQL" => 9,
"CBP" => 10,
"CLP" => 11,
}

filename = ARGV[0]

if filename.nil?
puts "Usage: asm_to_bin filename"
exit
end

file = File.read(filename)
bytes = []

file.split("\n").each do |line|
next if line.start_with?(";") || line.empty?
tokens = line.split(" ")
op = tokens[0]
var = tokens[1]
var_type = tokens[2]
num = tokens[3]

if DEBUG
print(OPERATORS[op])
print(var[1].to_i(16).to_s(2).rjust(4, "0"))
print(var_type.to_i.to_s(2).rjust(2, "0"))
print(num.to_i.to_s(2).rjust(8, "0"))
print("\n")
else
row = OPERATORS[op]
row = row << 4 | var[1].to_i(16)
row = row << 2 | var_type.to_i
row = row << 8 | num.to_i
bytes.push(row)
end
end

if DEBUG == false
print(bytes.pack("L*"))
end
35 changes: 35 additions & 0 deletions exe/bin_to_asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env ruby
DEBUG = ENV["DEBUG"] == "true" || ENV["DEBUG"].to_i == 1

OPERATORS = {
1 => "PRT",
2 => "SET",
3 => "ADD",
4 => "SUB",
5 => "MUL",
6 => "DIV",
7 => "JMP",
8 => "JNP",
9 => "EQL",
10 => "CBP",
11 => "CLP",
}

filename = ARGV[0]

if filename.nil?
puts "Usage: bin_to_asm filename"
exit
end

file = File.read(filename)
file.unpack("L*").each do |row|
row = row & 0b111111111111111111 # ARGH - get rid of 14-bit padding
# 4-4-2-8
op = row >> 14
var = (row >> 10) & 0b1111
var_type = (row >> 8) & 0b11
num = row & 0b11111111

puts "#{OPERATORS[op]} $#{var} #{var_type} #{num}"
end