Compare commits

..

No commits in common. "bd71e23e04b103327ec2ef096cbba34aeae2f650" and "0989bd54f06fe24421804965c147550b9f5c5899" have entirely different histories.

1 changed files with 8 additions and 26 deletions

View File

@ -30,7 +30,7 @@ def t8() 24 end ; def t9() 25 end
# reserved for kernel, detroyed by some irq handlers # reserved for kernel, detroyed by some irq handlers
def k0() 26 end ; def k1() 27 end def k0() 26 end ; def k1() 27 end
def gp() 28 end # global pointer, rarely used (we can use this to reffer to a struct or smtn) def gp() 28 end # global pointer, rarely used
def sp() 29 end # stack pointer def sp() 29 end # stack pointer
def fp() 30 end # frame pointer def fp() 30 end # frame pointer
def ra() 31 end # return address def ra() 31 end # return address
@ -39,39 +39,25 @@ class Assembler
def initialize(base, size) def initialize(base, size)
@base_addr = base @base_addr = base
@file_size = size @file_size = size
# moar vars defined in assemble()
end end
def inspect def inspect
"(Assembler base_addr:0x#{@base_addr.to_s 16} file_size:0x#{@file_size.to_s 16})" "(Assembler base_addr:0x#{@base_addr.to_s 16} file_size:0x#{@file_size.to_s 16})"
end end
# TODO: Sub-labels
def label(l) def label(l)
l = l.to_s
@sub_labels[l] = {} if not @sub_labels.member?(l)
@last_label_subs = @sub_labels[l]
return if @first_pass == false return if @first_pass == false
raise "error redefinition of label #{l}" if @labels.member?(l) raise "error redefinition of label #{l.to_s}" if @labels.member?(l.to_s)
@labels[l] = @bytes.length + @base_addr @labels[l.to_s] = @bytes.length + @base_addr
end
def sub_label(l)
return if @first_pass == false
l = l.to_s
raise "error redefinition of sub_label #{l}" if @last_label_subs.member?(l)
@last_label_subs[l] = @bytes.length + @base_addr
end end
def method_missing(m, *args, &block) def method_missing(m, *args, &block)
return 0 if @first_pass return 0 if @first_pass
if @last_label_subs[m.to_s] then if m.to_s[0, 2] == "l_" then
return @last_label_subs[m.to_s] return @labels[m.to_s[2..]] if @labels.member?(m.to_s[2..])
elsif @labels.member?(m.to_s) then # if m.to_s[0, 2] == "l_" then raise "label doesn't exist #{m.to_s}"
return @labels[m.to_s] #return @labels[m.to_s[2..]] if @labels.member?(m.to_s[2..])
#raise "label doesn't exist #{m.to_s}"
else else
super super
end end
@ -81,13 +67,10 @@ class Assembler
@first_pass = true @first_pass = true
@labels = {} @labels = {}
@bytes = [] @bytes = []
@sub_labels = {}
@last_label_subs = {}
binding.eval source binding.eval source
@first_pass = false @first_pass = false
@bytes = [] @bytes = []
@last_label_subs = {}
binding.eval source binding.eval source
puts "assembled 0x#{@bytes.length.to_s 16} (#{@bytes.length}) bytes" puts "assembled 0x#{@bytes.length.to_s 16} (#{@bytes.length}) bytes"
@ -249,7 +232,6 @@ file_size = 0x800 * 3
f = File.new "exe-header.asm.rb", "r" f = File.new "exe-header.asm.rb", "r"
exe_header = Assembler.new(base_addr, 0x800).assemble f.read exe_header = Assembler.new(base_addr, 0x800).assemble f.read
# puts f.path
f.close f.close
f = File.new "main.asm.rb", "r" f = File.new "main.asm.rb", "r"