a bit of cleanup and the good news is, we can output a (small) number
This commit is contained in:
parent
fee82643e5
commit
a08661955f
@ -74,31 +74,21 @@ module Arm
|
||||
|
||||
# the number (a Vm::integer) is (itself) divided by 10, ie overwritten by the result
|
||||
# and the remainder is overwritten (ie an out argument)
|
||||
# not really a function, more a macro, hence private
|
||||
# not really a function, more a macro,
|
||||
def div10 block, number , remainder
|
||||
|
||||
# takes argument in r1
|
||||
# returns quotient in r1, remainder in r2
|
||||
# SUB r2, r1, #10 # keep (x-10) for later
|
||||
# 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
|
||||
# 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 )
|
||||
# SUB r1, r1, r1, lsr #2
|
||||
block.add_code sub( number , left: number , right: number , shift_lsr: 2)
|
||||
# ADD r1, r1, r1, lsr #4
|
||||
block.add_code add( number , left: number , right: number , shift_lsr: 4)
|
||||
# ADD r1, r1, r1, lsr #8
|
||||
block.add_code add( number , left: number , right: number , shift_lsr: 8)
|
||||
# ADD r1, r1, r1, lsr #16
|
||||
block.add_code add( number , left: number , right: number , shift_lsr: 16)
|
||||
# MOV r1, r1, lsr #3
|
||||
block.add_code mov( number , right: number , shift_lsr: 3)
|
||||
# ADD r3, r1, r1, asl #2
|
||||
tmp = Vm::Integer.new( remainder.register + 1)
|
||||
block.add_code add( tmp , left: number , right: number , shift_lsl: 2)
|
||||
# SUBS r2, r2, r3, asl #1 # calc (x-10) - (x/10)*10
|
||||
block.add_code sub( remainder , left: remainder , right: tmp , shift_lsl: 1 , update_status: 1)
|
||||
# ADDPL r1, r1, #1 # fix-up quotient
|
||||
block.add_code add( number , left: number, right: 1 , condition_code: :pl )
|
||||
# ADDMI r2, r2, #10 # fix-up remainder
|
||||
block.add_code add( remainder , left: remainder , right: 10 , condition_code: :mi )
|
||||
end
|
||||
|
||||
|
@ -41,7 +41,7 @@ module Core
|
||||
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:6 ) # 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
|
||||
itos_fun = context.program.get_or_create_function(:utoa)
|
||||
block.add_code Vm::CMachine.instance.call( itos_fun , {})
|
||||
@ -49,32 +49,26 @@ module Core
|
||||
block.add_code Vm::CMachine.instance.add( str_addr , left: buffer ) # string to write to
|
||||
block.add_code Vm::CMachine.instance.mov( reg1 , right: buffer.length )
|
||||
ret = Vm::CMachine.instance.write_stdout(block)
|
||||
# function.return_type = ret
|
||||
function
|
||||
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
|
||||
def utoa context
|
||||
function = Vm::Function.new(:utoa , [Vm::Integer , Vm::Integer ] )
|
||||
block = function.body
|
||||
str_addr = context.str_addr
|
||||
number = Vm::Integer.new(str_addr.register + 1)
|
||||
remainder = Vm::Integer.new( number.register + 1)
|
||||
#STMFD sp!, {r9, r10, lr} #function entry save working regs (for recursion)
|
||||
#automatic block.add_code push( [:lr ] , {} ) #and the return address.
|
||||
# MOV r9, r1 # preserve arguments over following
|
||||
# MOV r10, r2 # function calls
|
||||
# BL udiv10 # r1 = r1 / 10
|
||||
Vm::CMachine.instance.div10( block , number , remainder )
|
||||
# ADD r10, r10, 48 #'0' # make char out of digit (by using ascii encoding)
|
||||
# make char out of digit (by using ascii encoding) 48 == "0"
|
||||
block.add_code Vm::CMachine.instance.add( remainder , left: remainder , right: 48 )
|
||||
#STRB r10, [r1], 1 # store digit at end of buffer
|
||||
# block.add_code Vm::CMachine.instance.strb( remainder , right: str_addr, offset: 1 , flagie: 1)
|
||||
block.add_code Vm::CMachine.instance.strb( remainder, right: str_addr , :offset => -1 , flaggie: 1) # CMP r1, #0 # quotient non-zero?
|
||||
block.add_code Vm::CMachine.instance.strb( remainder, right: str_addr )
|
||||
block.add_code Vm::CMachine.instance.sub( str_addr, left: str_addr , right: 1 )
|
||||
block.add_code Vm::CMachine.instance.cmp( number , right: 0 )
|
||||
#BLNE utoa # conditional recursive call to utoa
|
||||
block.add_code Vm::CMachine.instance.callne( function , {} )
|
||||
#LDMFD sp!, {r9, r10, pc} # function exit - restore and return
|
||||
#automatic block.add_code pop( [:pc] , {} )
|
||||
return function
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user