# Printing a number in assembly

Hum, I feel that this shouldn't be that hard, but I'm drawing a total blank here. I'm trying to print the byte-value of a register to the screen as a decimal number. I've looked through my other sources, but apparently I've only outputted characters, pixels and sprites in my assembly life. Help? XD

It counts the nr of 100’s, 10’s and 1’s, subtracting and adding alternatingly.

Can simplify it if you just need to print 16-bit or 8-bit numbers, but then again, any speed gained there will be nullified by the DOS’s slow console output.

Here for an 8-bit 1’s complement number:

https://hg.sr.ht/~grauw/synthesix/browse/fc2670a525/src/ui/V...

Same principle, you get the idea...

Thanks, Grauw. Those examples make me feel a bit better about myself, as it clearly wasn't as simple as I feared.

The `push` and `pop` are what you were talking about in the local variables topic, right?

for printing a number in decimal notation, you have to divide by 10 the number the remainder is the unit, divide it again by 10 and the remainder will be the decimal digit, divide by 10 again and the remainder will be 100's.

So, you must print in the screen backward, first the unit digit, 10's , 100's in that order. The same if building a string, reserve enough space, then poke it backward.

I will be generous with this 24bit & 16bit division routine.

```.BHLDiv10:		ld c, 10				; IN BHL = number, OUT BHL = result, A = rest.
ld d, \$15
xor a
rl b
rla
rl b
rla
rl b
rla

rl b
rla
cp c
jr c, .BHLDiv10Next

sub c
inc l

.BHLDiv10Next:		dec d
jr nz, .BHLDiv10Loop
ret
```

16 bits

```
.HLDiv10:		ld bc,\$0D0A				; IN HL = number. OUT HL = result, A = rest.
xor a
rla
rla
rla

rla
cp c
jr c, .HLDiv10Next

sub c
inc l

.HLDiv10Next:		djnz .HLDiv10Loop
ret

```

In the string / screen , reserve up to 8 characters places if 24bit numbers, or 5 places, if 16bits numbers.

Those are unsigned routines.

Thanks for the clear examples. I'll have no problem writing the code I need now.