Compare commits
2 Commits
0989bd54f0
...
bd71e23e04
Author | SHA1 | Date |
---|---|---|
bx | bd71e23e04 | |
bx | 0c2217168d |
34
assembler.rb
34
assembler.rb
|
@ -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
|
||||
|
@ -39,25 +39,39 @@ class Assembler
|
|||
def initialize(base, size)
|
||||
@base_addr = base
|
||||
@file_size = size
|
||||
|
||||
# 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 m.to_s[0, 2] == "l_" then
|
||||
return @labels[m.to_s[2..]] if @labels.member?(m.to_s[2..])
|
||||
raise "label doesn't exist #{m.to_s}"
|
||||
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
|
||||
super
|
||||
end
|
||||
|
@ -67,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"
|
||||
|
@ -232,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"
|
||||
|
|
Loading…
Reference in New Issue