added sublabels

master
bx 2021-09-05 23:59:52 +01:00
parent 0c2217168d
commit bd71e23e04
1 changed files with 23 additions and 9 deletions

View File

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