adds hand coded fibo, works
This commit is contained in:
@ -65,7 +65,7 @@ module Arm
|
||||
io.write_uint32 val
|
||||
end
|
||||
def shift val , by
|
||||
raise "Not integer #{val}:#{val.class}" unless val.is_a? Fixnum
|
||||
raise "Not integer #{val}:#{val.class} in #{inspect}" unless val.is_a? Fixnum
|
||||
val << by
|
||||
end
|
||||
end
|
||||
|
@ -73,6 +73,40 @@ module Core
|
||||
b.callne( utoa_function )
|
||||
return utoa_function
|
||||
end
|
||||
|
||||
# testing method, hand coded fibo, expects arg in 1 , so pass 2 in, first bogy
|
||||
# result comes in 0
|
||||
# a hand coded version of the fibonachi numbers
|
||||
# not my hand off course, found in the net from a basic introduction
|
||||
def fibo context
|
||||
fibo_function = Vm::Function.new(:fibo , [Vm::Integer , Vm::Integer] , Vm::Integer )
|
||||
result = fibo_function.args[0]
|
||||
int =fibo_function.args[1]
|
||||
i = Vm::Integer.new(2)
|
||||
f1 = Vm::Integer.new(3)
|
||||
f2 = Vm::Integer.new(4)
|
||||
loop_block = Vm::Block.new("loop")
|
||||
fibo_function.body.instance_eval do
|
||||
cmp( int , 1)
|
||||
mov( result, int , condition_code: :le)
|
||||
mov( :pc , :lr , condition_code: :le)
|
||||
push [ i , f1 , f2 , :lr]
|
||||
mov( f1 , 1)
|
||||
mov(f2 , 0)
|
||||
sub( i , int , 2)
|
||||
add_code loop_block
|
||||
end
|
||||
loop_block.instance_eval do
|
||||
add( f1 , f1 , f2)
|
||||
sub( f2 , f1 , f2)
|
||||
sub( i , i , 1 , update_status: 1)
|
||||
bpl( loop_block )
|
||||
mov( result , f1 )
|
||||
pop [ i , f1 , f2 , :pc]
|
||||
end
|
||||
fibo_function
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
extend ClassMethods
|
||||
|
@ -6,9 +6,9 @@ require 'elf/string_table_section'
|
||||
module Elf
|
||||
|
||||
class ObjectWriter
|
||||
def initialize(target)
|
||||
def initialize(program , target)
|
||||
@object = Elf::ObjectFile.new(target)
|
||||
|
||||
@program = program
|
||||
sym_strtab = Elf::StringTableSection.new(".strtab")
|
||||
@object.add_section sym_strtab
|
||||
@symbol_table = Elf::SymbolTableSection.new(".symtab", sym_strtab)
|
||||
@ -16,6 +16,17 @@ module Elf
|
||||
|
||||
@text = Elf::TextSection.new(".text")
|
||||
@object.add_section @text
|
||||
|
||||
program.link_at( 0 , program.context )
|
||||
|
||||
binary = program.assemble(StringIO.new )
|
||||
|
||||
blocks = program.functions.collect{ |f| [f.entry , f.exit , f.body] }
|
||||
blocks += [program.entry , program.exit , program.main]
|
||||
blocks.flatten.each do |b|
|
||||
add_symbol b.name.to_s , b.position
|
||||
end
|
||||
set_text binary.string
|
||||
end
|
||||
|
||||
def set_text(text)
|
||||
|
Reference in New Issue
Block a user