You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
962 B
41 lines
962 B
(
|
|
; This is an example implementation of itoa using a buffer of characters
|
|
(ld r0 -123456789)
|
|
|
|
(mkbf r1)
|
|
(call itoa r1 r0)
|
|
|
|
; print it
|
|
(:pn) (bfrpop @cout @r1 (em? (ld @cout '\n') (halt))) (j :pn)
|
|
(halt)
|
|
|
|
(proc itoa buf num
|
|
(ld r1 num)
|
|
(tst r1 (<0? (mul r1 -1)))
|
|
(:next)
|
|
(mod r0 r1 10)
|
|
(add r0 '0')
|
|
(bfrpush @buf r0)
|
|
(div r1 10 (z?
|
|
(tst num (<0? (bfrpush @buf '-')))
|
|
(ret)))
|
|
(j :next)
|
|
)
|
|
|
|
; other version that prints it
|
|
(proc printnum num
|
|
(mkbf r15)
|
|
(ld r1 num)
|
|
(tst r1 (<0? (mul r1 -1)))
|
|
(:next)
|
|
(mod r0 r1 10)
|
|
(add r0 '0')
|
|
(bfrpush @r15 r0)
|
|
(div r1 10 (z?
|
|
(tst num (<0? (bfrpush @r15 '-')))
|
|
(lds @cout @r15)
|
|
(del @r15)
|
|
(ret)))
|
|
(j :next)
|
|
)
|
|
)
|
|
|