serious bit fiddling, div10 using shift magic

forgot that arm has no division (or respectively only later models have)
many magic formulae out there, none seem to work 1000% on the
interpreter. some big 0 ending numbers are 1 off.
This commit is contained in:
Torsten Ruger
2015-11-13 20:46:27 +02:00
parent c190f718ec
commit 4a8bb32039
5 changed files with 120 additions and 52 deletions

View File

@ -152,7 +152,7 @@ module Register
@space.get_class_by_name(:Word).add_instance_method Builtin::Word.send(:putstring , nil)
obj = @space.get_class_by_name(:Integer)
[:mod , :putint , :div10 , :mod10].each do |f|
[ :putint, :mod4].each do |f|
obj.add_instance_method Builtin::Integer.send(f , nil)
end
end

View File

@ -5,49 +5,8 @@ module Register
module ClassMethods
include AST::Sexp
def div10 context
compiler = Soml::Compiler.new.create_method(:Integer,:div10 ).init_method
do_div10(compiler)
# return div
return compiler.method
end
def mod10 context
compiler = Soml::Compiler.new.create_method(:Integer,:mod10 ).init_method
do_div10(compiler)
#return mod
return compiler.method
end
def do_div10 compiler
end
# The conversion to base10 is quite a bit more complicated than i thought.
# The bulk of it is in div10
# We set up variables, do the devision and write the result to the string
# then check if were done and recurse if neccessary
# As we write before we recurse (save a push) we write the number backwards
# arguments: string address , integer
# def utoa context
# compiler = Soml::Compiler.new.create_method(:Integer ,:utoa , [ :Integer ] ).init_method
# function.source.receiver = :Integer
# return utoa_function
# # str_addr = utoa_function.receiver
# # number = utoa_function.args.first
# # remainder = utoa_function.new_local
# # RegisterMachine.instance.div10( utoa_function , number , remainder )
# # # make char out of digit (by using ascii encoding) 48 == "0"
# # utoa_function.instance_eval do
# # add( remainder , remainder , 48)
# # strb( remainder, str_addr )
# # sub( str_addr, str_addr , 1 )
# # cmp( number , 0 )
# # callne( utoa_function )
# # end
# # return utoa_function
# end
def mod context
compiler = Soml::Compiler.new.create_method(:Integer,:mod , {:Integer => :by} ).init_method
def mod4 context
compiler = Soml::Compiler.new.create_method(:Integer,:mod4 ).init_method
return compiler.method
end
def putint context