2014-05-06 11:47:07 +02:00
|
|
|
module Core
|
2014-05-06 11:42:43 +02:00
|
|
|
class Kernel
|
|
|
|
|
|
|
|
#there are no Kernel instances, only class methods.
|
2014-05-06 20:36:28 +02:00
|
|
|
# We use this module syntax to avoid the (ugly) self (also eases searching).
|
2014-05-06 11:42:43 +02:00
|
|
|
module ClassMethods
|
2014-06-01 13:24:54 +02:00
|
|
|
|
2014-06-07 22:22:32 +02:00
|
|
|
def putstring context
|
|
|
|
function = Vm::Function.new(:putstring , Vm::Integer , [] )
|
2014-06-12 07:34:46 +02:00
|
|
|
ret = Vm::RegisterMachine.instance.write_stdout(function)
|
2014-05-25 07:43:07 +02:00
|
|
|
function.set_return ret
|
2014-05-13 20:06:12 +02:00
|
|
|
function
|
2014-05-06 11:42:43 +02:00
|
|
|
end
|
2014-05-15 15:54:23 +02:00
|
|
|
|
2014-06-06 20:49:03 +02:00
|
|
|
def putint context
|
|
|
|
putint_function = Vm::Function.new(:putint , Vm::Integer , [] , Vm::Integer )
|
2014-05-19 11:18:01 +02:00
|
|
|
buffer = Vm::StringConstant.new(" ") # create a buffer
|
2014-05-31 11:52:29 +02:00
|
|
|
context.object_space.add_object buffer # and save it (function local variable: a no no)
|
2014-06-07 16:59:44 +02:00
|
|
|
int = putint_function.receiver
|
2014-05-21 18:06:21 +02:00
|
|
|
moved_int = putint_function.new_local
|
2014-05-31 15:19:44 +02:00
|
|
|
utoa = context.object_space.get_or_create_class(:Object).get_or_create_function(:utoa)
|
2014-06-10 23:38:46 +02:00
|
|
|
putint_function.instance_eval do
|
2014-06-12 08:07:03 +02:00
|
|
|
mov( moved_int , int ) # move arg up
|
|
|
|
add( int , buffer ,nil ) # string to write to (add string address to pc)
|
|
|
|
add( int , int , buffer.length - 3) # 3 for good measure , ahem.
|
2014-06-10 23:38:46 +02:00
|
|
|
call( utoa )
|
2014-06-12 08:07:03 +02:00
|
|
|
after = new_block("after_call")
|
2014-06-10 23:38:46 +02:00
|
|
|
insert_at after
|
|
|
|
# And now we "just" have to print it, using the write_stdout
|
|
|
|
add( int , buffer , nil ) # string to write to
|
|
|
|
mov( moved_int , buffer.length )
|
|
|
|
end
|
2014-06-12 08:07:03 +02:00
|
|
|
Vm::RegisterMachine.instance.write_stdout(putint_function)
|
2014-05-19 11:18:01 +02:00
|
|
|
putint_function
|
2014-05-15 15:54:23 +02:00
|
|
|
end
|
2014-05-16 18:55:46 +02:00
|
|
|
|
2014-05-16 22:24:19 +02:00
|
|
|
# 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
|
2014-05-19 11:18:01 +02:00
|
|
|
# arguments: string address , integer
|
2014-05-16 18:55:46 +02:00
|
|
|
def utoa context
|
2014-06-06 20:49:03 +02:00
|
|
|
utoa_function = Vm::Function.new(:utoa , Vm::Integer , [ Vm::Integer ] , Vm::Integer )
|
2014-06-07 16:59:44 +02:00
|
|
|
str_addr = utoa_function.receiver
|
|
|
|
number = utoa_function.args.first
|
2014-05-21 18:06:21 +02:00
|
|
|
remainder = utoa_function.new_local
|
2014-06-10 23:38:46 +02:00
|
|
|
Vm::RegisterMachine.instance.div10( utoa_function , number , remainder )
|
2014-05-16 22:24:19 +02:00
|
|
|
# make char out of digit (by using ascii encoding) 48 == "0"
|
2014-06-10 23:38:46 +02:00
|
|
|
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
|
2014-05-19 11:18:01 +02:00
|
|
|
return utoa_function
|
2014-05-16 18:55:46 +02:00
|
|
|
end
|
2014-05-19 16:32:41 +02:00
|
|
|
|
2014-06-12 15:23:57 +02:00
|
|
|
# testing method, hand coded fibo, expects arg in receiver_register
|
|
|
|
# result comes in return_register
|
2014-05-19 16:32:41 +02:00
|
|
|
# a hand coded version of the fibonachi numbers
|
2014-06-12 15:23:57 +02:00
|
|
|
# not my hand off course, found in the net http://www.peter-cockerell.net/aalp/html/ch-5.html
|
2014-05-19 16:32:41 +02:00
|
|
|
def fibo context
|
2014-06-06 20:49:03 +02:00
|
|
|
fibo_function = Vm::Function.new(:fibo , Vm::Integer , [] , Vm::Integer )
|
2014-06-07 16:59:44 +02:00
|
|
|
result = fibo_function.return_type
|
|
|
|
int = fibo_function.receiver
|
2014-06-12 15:23:57 +02:00
|
|
|
|
|
|
|
last = fibo_function.new_block("return")
|
|
|
|
|
2014-05-21 18:27:27 +02:00
|
|
|
f1 = fibo_function.new_local
|
|
|
|
f2 = fibo_function.new_local
|
2014-05-21 11:47:40 +02:00
|
|
|
|
2014-06-12 15:23:57 +02:00
|
|
|
fibo_function.instance_eval do
|
2014-06-10 23:38:46 +02:00
|
|
|
cmp int , 1
|
|
|
|
mov( result, int , condition_code: :le)
|
2014-06-12 15:23:57 +02:00
|
|
|
ble( last ) #branch to return, rather than return (as the original)
|
|
|
|
mov f1 , 1 #set up initial values
|
2014-06-10 23:38:46 +02:00
|
|
|
mov f2 , 0
|
|
|
|
end
|
2014-05-21 11:47:40 +02:00
|
|
|
|
2014-06-12 15:23:57 +02:00
|
|
|
loop = fibo_function.new_block("loop")
|
|
|
|
fibo_function.insert_at loop
|
2014-06-07 16:59:44 +02:00
|
|
|
|
2014-06-12 15:23:57 +02:00
|
|
|
fibo_function.instance_eval do #loop through
|
|
|
|
add f1 , f1 , f2 # f1 = f1 + f2
|
|
|
|
sub f2 , f1 , f2 # f2 = f1 -f2
|
|
|
|
sub int , int , 1 # todo: set.. should do below cmp, but doesn't , set_update_status: 1
|
|
|
|
cmp int , 1
|
|
|
|
bne( loop )
|
2014-06-10 23:38:46 +02:00
|
|
|
mov( result , f1 )
|
|
|
|
end
|
2014-06-12 15:23:57 +02:00
|
|
|
|
2014-05-19 16:32:41 +02:00
|
|
|
fibo_function
|
|
|
|
end
|
|
|
|
|
2014-05-02 07:02:25 +02:00
|
|
|
end
|
2014-05-06 11:42:43 +02:00
|
|
|
|
|
|
|
extend ClassMethods
|
2014-05-02 07:02:25 +02:00
|
|
|
end
|
|
|
|
end
|