diff --git a/lib/asm/arm/arm_assembler.rb b/lib/asm/arm/arm_assembler.rb index 0194d1a6..de71b1a9 100644 --- a/lib/asm/arm/arm_assembler.rb +++ b/lib/asm/arm/arm_assembler.rb @@ -6,7 +6,7 @@ require 'asm/arm/generator_label' require 'asm/nodes' require 'stream_reader' require 'stringio' -require "asm/data_object" +require "asm/string_node" module Asm module Arm diff --git a/lib/asm/arm/normal_builder.rb b/lib/asm/arm/normal_builder.rb index b75b0c49..dfe4d52b 100644 --- a/lib/asm/arm/normal_builder.rb +++ b/lib/asm/arm/normal_builder.rb @@ -46,7 +46,7 @@ module Asm def build_operand(arg , position = 0) #position only needed for calculating relative addresses to data objects #there is a design stink here which makes my head ache. But shanti shanti - if arg.is_a?(Asm::DataObject) + if arg.is_a?(Asm::StringNode) # do pc relative addressing with the difference to the instuction # 8 is for the funny pipeline adjustment (ie oc pointing to fetch and not execute) arg = Asm::NumLiteralNode.new( arg.position - position - 8 ) diff --git a/lib/asm/assembler.rb b/lib/asm/assembler.rb index 4ee49c23..6712aa5b 100644 --- a/lib/asm/assembler.rb +++ b/lib/asm/assembler.rb @@ -16,7 +16,7 @@ module Asm def add_string str value = @string_table[str] return value if value - data = Asm::DataObject.new(str) + data = Asm::StringNode.new(str) @string_table[str] = data end @@ -27,8 +27,6 @@ module Asm def add_value(val) val.at(@position) length = val.length - #rounding up to the next 4 - length = (((length - 1 ) / 4 ) + 1 ) * 4 @position += length @values << val end diff --git a/lib/asm/data_object.rb b/lib/asm/data_object.rb deleted file mode 100644 index e9ff555c..00000000 --- a/lib/asm/data_object.rb +++ /dev/null @@ -1,21 +0,0 @@ -module Asm - class DataObject - def initialize(data) - @data = data - end - - def position - throw "Not set" unless @address - @address - end - def at address - @address = address - end - def length - @data.length - end - def assemble(io, as) - io << @data - end - end -end \ No newline at end of file diff --git a/lib/asm/string_node.rb b/lib/asm/string_node.rb new file mode 100644 index 00000000..5ca7be49 --- /dev/null +++ b/lib/asm/string_node.rb @@ -0,0 +1,26 @@ +module Asm + class StringNode + def initialize(str) + #align + length = str.length + # rounding up to the next 4 (always adding one for zero pad) + pad = ((length / 4 ) + 1 ) * 4 - length + raise "#{pad} #{self}" unless pad >= 1 + @string = str + "\x00" * pad + end + + def position + throw "Not set" unless @address + @address + end + def at address + @address = address + end + def length + @string.length + end + def assemble(io, as) + io << @string + end + end +end \ No newline at end of file diff --git a/test/small_program_test.rb b/test/small_program_test.rb index 9ffcc79c..ff07ae3c 100644 --- a/test/small_program_test.rb +++ b/test/small_program_test.rb @@ -25,7 +25,7 @@ class TestSmallProg < MiniTest::Test end def test_hello - hello = "Hello Raisa"+ "\n\x00" + hello = "Hello Raisa\n" @generator.instance_eval { mov r7, 4 # 4 == write mov r0 , 1 # stdout @@ -35,7 +35,7 @@ class TestSmallProg < MiniTest::Test mov r7, 1 # 1 == exit swi 0 } - write(7 , 'label') + write(7 + hello.length/4 + 1 , 'hello') end #test dropped along with functionality, didn't work and not needed (yet?) TODO diff --git a/unused/ast_assembler.rb b/unused/ast_assembler.rb index 545e1c1e..a30e7b16 100644 --- a/unused/ast_assembler.rb +++ b/unused/ast_assembler.rb @@ -35,10 +35,10 @@ module Asm bytes = cmd.value.strip.split(/\s+/).map do |hex| hex.to_i(16) end.pack('C*') - @asm.add_value Asm::DataObject.new(bytes) + @asm.add_value Asm::StringNode.new(bytes) elsif (cmd.name == "asciz") str = eval(cmd.value) + "\x00" - @asm.add_value Asm::DataObject.new(str) + @asm.add_value Asm::StringNode.new(str) elsif (defined?(Asm::Arm) and cmd.name == 'addrtable') @asm.add_value Asm::Arm::AddrTableObject.new else