Included sources:
%native write 0 ;; TODO(#127): a better way of allocating memory for standard printing functions %const print_memory "******************************" %const FRAC_PRECISION 10 ;; addr_a ;; addr_b swap8: swap 2 dup 1 dup 1 read8 swap 1 read8 dup 2 swap 1 write8 dup 2 swap 1 write8 drop drop ret reverse: swap 2 swap 1 dup 0 push 2 divi ; middle index reverse_loop: dup 0 push 0 eqi ; we reached the middle? jmp_if reverse_loop_end dup 2 dup 2 push 1 minusi dup 1 plusi call swap8 push 1 minusi swap 1 push 2 minusi swap 1 swap 2 push 1 plusi swap 2 jmp reverse_loop reverse_loop_end: drop drop drop ret fabs: swap 1 dup 0 push 0.0 gef jmp_if fabs_skip_negation push -0.0 xor fabs_skip_negation: swap 1 ret frac: swap 1 dup 0 f2i i2f minusf swap 1 ret floor: swap 1 dup 0 f2i i2f dup 0 swap 2 minusf push 0.0 lef jmp_if floor_skip_dec push 1.0 minusf floor_skip_dec: swap 1 ret ;; 1.0^{-n} b: swap 1 push 1.0 b_loop_begin: dup 1 push 0 lei jmp_if b_loop_end push 0.1 multf swap 1 push 1 minusi swap 1 jmp b_loop_begin b_loop_end: swap 1 drop swap 1 ret print_frac: swap 1 push FRAC_PRECISION call b push 2.0 divf print_frac_loop_begin: ;; U dup 1 push 10.0 multf call floor swap 2 push 10.0 multf call frac swap 2 swap 1 push 10.0 multf swap 1 ;; if (R < M) break; dup 2 dup 2 ltf jmp_if print_frac_loop_end ;; if (R > 1 - M) break; dup 2 dup 2 push 1.0 swap 1 minusf gtf jmp_if print_frac_loop_end f2i push '0' plusi push print_memory swap 1 write8 push print_memory push 1 native write jmp print_frac_loop_begin print_frac_loop_end: dup 2 push 0.5 lef jmp_if print_frac_skip_inc push 1.0 plusf print_frac_skip_inc: f2i push '0' plusi push print_memory swap 1 write8 push print_memory push 1 native write drop drop ret print_positive: swap 1 push print_memory ; address of print_memory in memory print_positive_loop: dup 1 push 10 modu ; extracted digit push '0' plusi ; char for digit dup 1 swap 1 write8 ; digit was written to memory push 1 plusi ; address of next char in memory swap 1 push 10 divu ; number to print without last digit swap 1 dup 1 push 0 equ not ; no digits left? jmp_if print_positive_loop push print_memory minusi ; length of string to print push print_memory ; address of print_memory swap 1 dup 1 dup 1 call reverse ; back to print_positive native write drop ret ;; TODO(#142): dump_f64 does not support NaN and Inf dump_f64: swap 1 dup 0 push -0.0 andb not jmp_if dump_f64_skip_negative push -0.0 xor push print_memory push '-' write8 push print_memory push 1 native write dump_f64_skip_negative: debug: push FRAC_PRECISION call b push 2.0 divf push 1.0 dup 2 call frac minusf gef jmp_if dump_f64_no_frac ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; dup 0 f2i call print_positive push print_memory push '.' write8 push print_memory push 1 native write call frac call print_frac jmp dump_f64_no_frac_end dump_f64_no_frac: f2i push 1 plusi call print_positive push print_memory push '.' write8 push print_memory push 1 native write push print_memory push '0' write8 push print_memory push 1 native write dump_f64_no_frac_end: push print_memory push 10 write8 push print_memory push 1 native write ret dump_i64: swap 1 dup 0 push 0 gei jmp_if dump_i64_skip_negation push print_memory push '-' write8 push print_memory push 1 native write push -1 multi dump_i64_skip_negation: call print_positive push print_memory push 10 write8 push print_memory push 1 native write ret dump_u64: swap 1 call print_positive ; back to dump_u64 push print_memory push 10 write8 push print_memory push 1 native write ret %include "./examples/natives.hasm" main: push 1 push 2 push 3 plusi plusi ; result call dump_u64 ; back to main halt %entry main
Step a line
Author of BASM language