Decimal to Floating Point

Any discussions related to the creation of new hardware or software for the ZX80 or ZX81
swensont
Posts: 76
Joined: Tue Jan 18, 2011 4:55 am
Location: SF Bay Area
Contact:

Decimal to Floating Point

Post by swensont »

I've been looking into using the FP calculator and I've got it working, except for how to convert a decimal number as a string, into an FP number.
The ROM has the "decimal to floating point" routine ($14d9) that seems to take a decimal as a string, convert it to FP and put it on the stack. I've tried this routine, but I am unable to get it to work. It looks like it uses the system variable CH-ADD as the location of the next character in the string. I've created a string, put the location of the string in HL and then set CH-ADD to HL:

Code: Select all

   LD	HL,string
   LD 	(16406),HL
   CALL DEC2FP
   CALL	PRINTFP
   RET

string:         DEFB $1F,$1B,$1D,$20,$7E
The string is "3.14". The end result should show "3.14" on the screen, but all I get is "0". I hope someone has some pointers on using this routine.

Tim
User avatar
stefano
Posts: 542
Joined: Tue Dec 11, 2012 9:24 am
Contact:

Re: Decimal to Floating Point

Post by stefano »

It is just a blind guess, but I suppose you first need to load the string ptr to the FP calculator stack.
Use the routine at address $12c3 enter with DE pointing to your string, BC = string length
Moggy
Posts: 3231
Joined: Wed Jun 18, 2008 2:00 pm

Re: Decimal to Floating Point

Post by Moggy »

Not sure if this helps but using the suggestion above about pointing DE to the string start and BC to the length then this seems to work when pointing to a memory location for the string.

Code: Select all

LD.DE (START OF STRING)
LD BC.4 (LENGTH OF STRING) 
CALL $ 0B6B 
RET
For printing from the stack this seems to be the routine used...

Code: Select all

) The PRINT-STK routine collects the details of a string from the calculator stack. A number is dealt with by jumping to PRINT-FP, whereas a string is dealt with in the 'print string' section. First the syntax flag is read.

0B55 PRINT-STK	CALL	0AC5,UNSTACK-2
		BIT	6,(FLAGS)
		CALL	Z,13F8,STK-FETCH
		JR	Z,0B6B,PR-STR-4
		JP	15DB,PRINT-FP

x) The string printing routine.

The length of the string is held in the BC register pair and the starting address of the string is held in the DE register pair.

0B64 PR-STR-1	LD	A,+0B
0B66 PR-STR-2	RST	0010,PRINT-A
0B67 PR-STR-3	LD	DE,(X-PTR)
0B6B PR-STR-4	LD	A,B
		OR	C
		DEC	BC
		RET	Z
		LD	A,(DE)
		INC	DE
		LD	(X-PTR),DE
		BIT	6,A
		JR	Z,0B66,PR-STR-2
		CP	+C0
		JR	Z,0B64,PR-STR-1
		PUSH	BC
		CALL	094B,TOKENS
		POP	BC
		JR	0B67,PR-STR-3 

Please appreciate coding is not my strong point and apologies if I have got this wrong.
User avatar
XavSnap
Posts: 1940
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Decimal to Floating Point

Post by XavSnap »

:ugeek:

Another CrayonFullerPen for ZX81's MC.

:arrow:
Last edited by XavSnap on Fri Jul 01, 2022 7:06 am, edited 4 times in total.
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
swensont
Posts: 76
Joined: Tue Jan 18, 2011 4:55 am
Location: SF Bay Area
Contact:

Re: Decimal to Floating Point

Post by swensont »

Thanks for the replies.

Stefano - $12C2 is STK-Store and stores registers A, B, C, D, & E to the calculator stack. Logan's book says "Although this subroutine could be used to transfer floating point numbers it is, however, only used to transfer the parameters of strings". It only stores 5 byte (and exactly 5 bytes). Is there something else that converts the string to a floating point number?

Moggy - $0B6B looks to be part of a print string routine (PR-STR-4).

XavSnap - I'm not sure exactly what you are trying to do and not sure why D-FILE is needed. Can you expand on what you are trying to do?

Tim
User avatar
mrtinb
Posts: 1906
Joined: Fri Nov 06, 2015 5:44 pm
Location: Denmark
Contact:

Re: Decimal to Floating Point

Post by mrtinb »

I haven’t used the calculator stack. However I would assume this:
  1. Put string on calculator stack
  2. Run VAL on calculator stack
  3. Fetch FP number from calculator stack
Martin
https://zx.rtin.be
ZX81, Lambda 8300, Commodore 64, Mac G4 Cube
User avatar
mrtinb
Posts: 1906
Joined: Fri Nov 06, 2015 5:44 pm
Location: Denmark
Contact:

Re: Decimal to Floating Point

Post by mrtinb »

Can this article help?

http://www.rtin.be/zx81/fp.pdf
Martin
https://zx.rtin.be
ZX81, Lambda 8300, Commodore 64, Mac G4 Cube
olofsen
Posts: 189
Joined: Wed Jan 08, 2014 12:29 pm

Re: Decimal to Floating Point

Post by olofsen »

Code: Select all

RST 18H
or

Code: Select all

LD A,(HL)
before calling DEC2FP may help :)
David G
Posts: 387
Joined: Thu Jul 17, 2014 7:58 am
Location: 21 North, 156 West

Re: Decimal to Floating Point

Post by David G »

Yes, this works

Code: Select all

LD    HL,string
LD    (16406),HL    ;CH_ADD
LD    A,(HL)
CALL  $14D9         ;DEC-TO-FP
CALL  $15DB         ;PRINT-FP
RET
string: DEFB $1F,$1B,$1D,$20,$1D,$21,$25,$1E,$23   ;"3.1415927"
A few years ago I was trying to figure out DEC-TO-FP, but never could make it work. Now with olofsen's LD A suggestion and swensont's updating of CH_ADD, success!


By the way, 3.1415927 is what is printed by PRINT PI, but internally is it slightly different. Maybe more digits that aren't printed. "3.1415972" is 82 49 0F DA D3 while PI is 82 49 0F DA A2


Original problem:
how to convert a decimal number as a string, into an FP number...

Code: Select all

   ...
   CALL DEC2FP
   CALL	PRINTFP
From Logan:
14D9 DEC-TO-FP
15DB PRINT-FP

DEC-TO-FP THE 'DECIMAL TO FLOATING-POINT' SUBROUTINE
This subroutine reads the decimal number digit by digit and gives its result as a 'last value' on the calculator stack.

PRINT-FP THE 'PRINT A FLOATING-POINT NUMBER' SUBROUTINE
The subroutine prints X, the 'last value' on the calculator stack.


Like most commented code, i find Logan's comments sometimes mysterious. Here's what I was able to work out about how to use PRINT-FP
1. Put the number on the stack (in floating-point format). This is the CALC stack, not the machine stack
2. Optionally, load DF_CC with the display file address where printing is wanted. You could just let it use the current value. After RUN it is the first character position of the screen
3. Call PRINT-FP


Moggy's comment was quite interesting

Code: Select all

CALL $ 0B6B
0B6B PR-STR-4

This prints a string from the CALC processor. I wonder why the CALC has the ability to do this as it would be easier to just use PRINT "3.14". Or in assembly, copy the string to the Display File. But maybe it makes the CALC a general purpose processor implemented in software (and thus independent of the Z80)
User avatar
stefano
Posts: 542
Joined: Tue Dec 11, 2012 9:24 am
Contact:

Re: Decimal to Floating Point

Post by stefano »

The CALC processor is in charge also of the string operations of the BASIC interpreter, in example the string concatenation.
I think the interpreter simply puts the arguments on the FP stack.
Post Reply