Page 1 of 2
Decimal to Floating Point
Posted: Thu Jun 30, 2022 5:43 am
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
Re: Decimal to Floating Point
Posted: Thu Jun 30, 2022 10:59 am
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
Re: Decimal to Floating Point
Posted: Thu Jun 30, 2022 5:06 pm
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.
Re: Decimal to Floating Point
Posted: Thu Jun 30, 2022 5:22 pm
by XavSnap
Another CrayonFullerPen for ZX81's MC.

Re: Decimal to Floating Point
Posted: Fri Jul 01, 2022 4:58 am
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
Re: Decimal to Floating Point
Posted: Fri Jul 01, 2022 7:16 am
by mrtinb
I haven’t used the calculator stack. However I would assume this:
- Put string on calculator stack
- Run VAL on calculator stack
- Fetch FP number from calculator stack
Re: Decimal to Floating Point
Posted: Fri Jul 01, 2022 10:14 am
by mrtinb
Re: Decimal to Floating Point
Posted: Fri Jul 01, 2022 5:25 pm
by olofsen
or
before calling DEC2FP may help

Re: Decimal to Floating Point
Posted: Fri Jul 01, 2022 10:07 pm
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...
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
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)
Re: Decimal to Floating Point
Posted: Fri Jul 01, 2022 11:29 pm
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.