bit of evaling
This commit is contained in:
parent
a08661955f
commit
53666e8b33
@ -65,9 +65,11 @@ module Arm
|
|||||||
|
|
||||||
# assumes string in r0 and r1 and moves them along for the syscall
|
# assumes string in r0 and r1 and moves them along for the syscall
|
||||||
def write_stdout block
|
def write_stdout block
|
||||||
block.add_code mov( :r2 , right: :r1 )
|
block.instance_eval do
|
||||||
block.add_code mov( :r1 , right: :r0 )
|
mov( :r2 , right: :r1 )
|
||||||
block.add_code mov( :r0 , right: 1 ) # 1 == stdout
|
mov( :r1 , right: :r0 )
|
||||||
|
mov( :r0 , right: 1 ) # 1 == stdout
|
||||||
|
end
|
||||||
syscall( block , 4 )
|
syscall( block , 4 )
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -79,17 +81,19 @@ module Arm
|
|||||||
# Note about division: devision is MUCH more expensive than one would have thought
|
# Note about division: devision is MUCH more expensive than one would have thought
|
||||||
# And coding it is a bit of a mind leap: it's all about finding a a result that gets the
|
# And coding it is a bit of a mind leap: it's all about finding a a result that gets the
|
||||||
# remainder smaller than an int. i'll post some links sometime. This is from the arm manual
|
# remainder smaller than an int. i'll post some links sometime. This is from the arm manual
|
||||||
block.add_code sub( remainder , left: number , right: 10 )
|
block.instance_eval do
|
||||||
block.add_code sub( number , left: number , right: number , shift_lsr: 2)
|
sub( remainder , left: number , right: 10 )
|
||||||
block.add_code add( number , left: number , right: number , shift_lsr: 4)
|
sub( number , left: number , right: number , shift_lsr: 2)
|
||||||
block.add_code add( number , left: number , right: number , shift_lsr: 8)
|
add( number , left: number , right: number , shift_lsr: 4)
|
||||||
block.add_code add( number , left: number , right: number , shift_lsr: 16)
|
add( number , left: number , right: number , shift_lsr: 8)
|
||||||
block.add_code mov( number , right: number , shift_lsr: 3)
|
add( number , left: number , right: number , shift_lsr: 16)
|
||||||
|
mov( number , right: number , shift_lsr: 3)
|
||||||
tmp = Vm::Integer.new( remainder.register + 1)
|
tmp = Vm::Integer.new( remainder.register + 1)
|
||||||
block.add_code add( tmp , left: number , right: number , shift_lsl: 2)
|
add( tmp , left: number , right: number , shift_lsl: 2)
|
||||||
block.add_code sub( remainder , left: remainder , right: tmp , shift_lsl: 1 , update_status: 1)
|
sub( remainder , left: remainder , right: tmp , shift_lsl: 1 , update_status: 1)
|
||||||
block.add_code add( number , left: number, right: 1 , condition_code: :pl )
|
add( number , left: number, right: 1 , condition_code: :pl )
|
||||||
block.add_code add( remainder , left: remainder , right: 10 , condition_code: :mi )
|
add( remainder , left: remainder , right: 10 , condition_code: :mi )
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def syscall block , num
|
def syscall block , num
|
||||||
|
@ -38,16 +38,18 @@ module Core
|
|||||||
buffer = Vm::StringConstant.new(" ")
|
buffer = Vm::StringConstant.new(" ")
|
||||||
context.program.add_object buffer
|
context.program.add_object buffer
|
||||||
str_addr = Vm::Integer.new(0) # address of the
|
str_addr = Vm::Integer.new(0) # address of the
|
||||||
reg1 = Vm::Integer.new(1)
|
|
||||||
block.add_code Vm::CMachine.instance.mov( reg1 , right: str_addr ) #move arg up
|
|
||||||
block.add_code Vm::CMachine.instance.add( str_addr , left: buffer ) # string to write to
|
|
||||||
block.add_code Vm::CMachine.instance.add( str_addr , left: str_addr , right: (buffer.length-3)) # string to write to
|
|
||||||
context.str_addr = str_addr
|
context.str_addr = str_addr
|
||||||
|
reg1 = Vm::Integer.new(1)
|
||||||
itos_fun = context.program.get_or_create_function(:utoa)
|
itos_fun = context.program.get_or_create_function(:utoa)
|
||||||
block.add_code Vm::CMachine.instance.call( itos_fun , {})
|
block.instance_eval do
|
||||||
|
mov( reg1 , right: str_addr ) #move arg up
|
||||||
|
add( str_addr , left: buffer ) # string to write to
|
||||||
|
add( str_addr , left: str_addr , right: (buffer.length-3))
|
||||||
|
call( itos_fun , {})
|
||||||
# And now we "just" have to print it, using the write_stdout
|
# And now we "just" have to print it, using the write_stdout
|
||||||
block.add_code Vm::CMachine.instance.add( str_addr , left: buffer ) # string to write to
|
add( str_addr , left: buffer ) # string to write to
|
||||||
block.add_code Vm::CMachine.instance.mov( reg1 , right: buffer.length )
|
mov( reg1 , right: buffer.length )
|
||||||
|
end
|
||||||
ret = Vm::CMachine.instance.write_stdout(block)
|
ret = Vm::CMachine.instance.write_stdout(block)
|
||||||
function
|
function
|
||||||
end
|
end
|
||||||
@ -64,11 +66,13 @@ module Core
|
|||||||
remainder = Vm::Integer.new( number.register + 1)
|
remainder = Vm::Integer.new( number.register + 1)
|
||||||
Vm::CMachine.instance.div10( block , number , remainder )
|
Vm::CMachine.instance.div10( block , number , remainder )
|
||||||
# make char out of digit (by using ascii encoding) 48 == "0"
|
# make char out of digit (by using ascii encoding) 48 == "0"
|
||||||
block.add_code Vm::CMachine.instance.add( remainder , left: remainder , right: 48 )
|
block.instance_eval do
|
||||||
block.add_code Vm::CMachine.instance.strb( remainder, right: str_addr )
|
add( remainder , left: remainder , right: 48 )
|
||||||
block.add_code Vm::CMachine.instance.sub( str_addr, left: str_addr , right: 1 )
|
strb( remainder, right: str_addr )
|
||||||
block.add_code Vm::CMachine.instance.cmp( number , right: 0 )
|
sub( str_addr, left: str_addr , right: 1 )
|
||||||
block.add_code Vm::CMachine.instance.callne( function , {} )
|
cmp( number , right: 0 )
|
||||||
|
callne( function , {} )
|
||||||
|
end
|
||||||
return function
|
return function
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user