Page 1 of 1

Help converting numbers onto Char in ML

Posted: Sun Mar 19, 2017 1:58 am
by jalavera
Hi!

I'm programming a little in ML but I don't know what's the way to convert a byte number in a memory location to its converted string writable in screen.

For example if I PEEK nnnn memory we can use: LDA nnnn. But how can I print the acculator value to screen? I know internal $FDD2 routine, but this write the ASCII value not the string one.

Thanks!

Re: Help converting numbers onto Char in ML

Posted: Sun Mar 19, 2017 3:06 am
by srowe
Depending on what base you want it's either quite easy or a little complicated.

For printing in hex the bytes simply map on to pair of characters to display. So $1234 comes out as "1" ($31), "2" ($32), "3" ($33) and "4" ($34). Values above 9 have to be handled as there's a 'gap' between "9" ($39) and "A" $(41). Using blocks of 4 shifts to normalize each nibble you can do something like

Code: Select all

PRINTHEX16
	LDA STAL+1		; get high byte
	JSR PRINTHEX8		; output as hex digits
	LDA STAL		; get low byte
PRINTHEX8
	PHA			; save value
	LSR			; shift top nibble ..
	LSR
	LSR
	LSR			; .. into bottom nibble
	JSR LAD82		; output most significant nibble as hex digit
	PLA			; restore value
	AND #$0F		; mask off top nibble
LAD82	ORA #'0'
	CMP #'9'+1
	BCC LAD8A		; numeric, output it
	ADC #$06		; make hex digit
LAD8A	JMP CHROUT		; output character to channel
Printing a number as decimal is more complex because you need to do successive divisions by ten. You can find examples of doing multibyte division in most 6502 assembly books.

If all you want is to print a 16 bit unsigned number you can call the BASIC PRTFIX ($DDCD) subroutine, load .A with the MSB and .X with the LSB.

Re: Help converting numbers onto Char in ML

Posted: Sun Mar 19, 2017 10:12 am
by jalavera
Thanks. I will try!
Where can I found this Basic PRTFIX in detail? I never listened and read before. I suppose there are others which can be used.

Re: Help converting numbers onto Char in ML

Posted: Sun Mar 19, 2017 10:56 am
by srowe
jalavera wrote:Thanks. I will try!
Where can I found this Basic PRTFIX in detail? I never listened and read before. I suppose there are others which can be used.
There are many BASIC and KERNAL routines which can be used in your own programs. I have a dissassembly of the VIC-20 ROMs here

http://eden.mose.org.uk/gitweb/?p=rom-r ... sm;hb=HEAD

which are commented. PRTFIX is convenient but not efficient. It converts the integer into a floating point number then uses the floating point print routines.

Re: Help converting numbers onto Char in ML

Posted: Sun Mar 19, 2017 3:04 pm
by Mike
srowe wrote:Printing a number as decimal is more complex because you need to do successive divisions by ten.
It is also possible to use the decimal mode of the 6502 to build up a number in BCD code from its binary representation and then use your example routine to print HEX numbers: http://6502.org/source/integers/hex2dec-more.htm.

Those routines need to be used with caution on the VIC-20 though: the standard IRQ handler in the KERNAL doesn't issue a CLD as first instruction and this can lead to problems with interrupt routines that don't take the decimal mode into account. Unless the IRQ is adapted, this means these conversion routines must be run with interrupts disabled.

Also, the number conversion can implement the division as table-driven repeated multibyte subtractions. That works amazingly well even in the presence of mixed bases, as it's the case with a clock display from the jiffy timer: "Display of TI$ in border (unex. or +3K)".


... P.S. see this thread, "best way to display decimal number?", for a nice déjà vu. :mrgreen:

Re: Help converting numbers onto Char in ML

Posted: Mon Mar 20, 2017 5:51 am
by wimoos
Printing signed 16-bit integers (where the high bit of the msb denotes the sign):

Code: Select all

	LDA msb
	LDY lsb
	JSR $D391
	INY
	JMP $DDD7
Regards,

Wim.

Re: Help converting numbers onto Char in ML

Posted: Mon Mar 20, 2017 8:48 am
by Kweepa
Codebase64 has a great collection of efficient 6502 snippets.
http://www.codebase64.org
For example (to answer your question):
http://www.codebase64.org/doku.php?id=b ... ii_routine

Re: Help converting numbers onto Char in ML

Posted: Tue Mar 21, 2017 6:29 am
by jalavera
After testing routines you suggest I think Kweepa's one is what I was looking for: 1 byte (0-255) in decimal using .A (ones), .X (tens), .Y. (hundreds) registers. When combined it prints YXA on screen. It's like PRINT PEEK (1234).
Many thanks to all of you.