Sunday 20 April 2008

Write Number

Here's a short iterative routine to write a number to standard out. Although longer than the recursive algorithm, it avoids the need to maintain a data / return stack. 9x+1 cycles are required, where x is the number of digits.

The sts opcode is available in exMARS Streams.
writedec  mov    number,         temp
jmp _wdloop+1, >_wdloop

_wdloop mul #10, #0
div #10, temp
jmn _wdloop, temp

_wdprint mov number, temp
div.b _wdloop, temp
add #48, temp
sts temp, 0
mod.b _wdloop, number
div #10, _wdloop
jmn _wdprint, _wdloop

No comments: