Printing a number in assembly

By SuchAGoonie

Expert (108)

SuchAGoonie's picture

19-01-2016, 11:24

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

Login or register to post comments

By Grauw

Ascended (8205)

Grauw's picture

19-01-2016, 13:05

https://bitbucket.org/grauw/vgmplay-msx/src/6d7592a1d/src/Sy...

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.

By Grauw

Ascended (8205)

Grauw's picture

19-01-2016, 13:16

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

https://bitbucket.org/grauw/synthesix/src/fc2670a525/src/ui/...

Same principle, you get the idea...

By SuchAGoonie

Expert (108)

SuchAGoonie's picture

19-01-2016, 14:49

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

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

By flyguille

Prophet (3029)

flyguille's picture

19-01-2016, 17:03

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
			add hl,hl
			rl b
			rla
			add hl,hl
			rl b
			rla
			add hl,hl
			rl b
			rla

.BHLDiv10Loop:		add hl,hl
			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
			add hl,hl
			rla
			add hl,hl
			rla
			add hl,hl
			rla

.HLDiv10Loop:		add hl,hl
			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.

By SuchAGoonie

Expert (108)

SuchAGoonie's picture

20-01-2016, 02:58

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