IGNORED

# Understanding internal BCD representation

## Recommended Posts

Hi team

According to Mapping the Atari, D4-D9 (212-217) correspond to FR0: "Floating point register zero; holds a six-byte internal form of the FP number."

Here you can find some examples of how decimal numbers are represented on D4-D9:

```Number      00D4 00D5 00D6 00D7 00D8 00D9
37.5         40   37   50   00   00   00
3.14159      40   03   14   15   90   00
117.809625   41   01   17   80   96   25
0.12         3F   12   00   00   00   00
12345678.99  43   12   34   56   78   90```

I don't understand the meaning of 00D4.

According to Bob Duhamel's Atari System Reference manual, this corresponds to "excess 64 exponent + sign".

Can you please let me know how to interpret \$00D4?

Kind regards,

Luis.

##### Share on other sites

The high bit (\$80) is the sign of the whole number.

The remaining 7 bits are the exponent in base-100. But instead of using bit 6 (\$40) as the sign they shift the range up so an exponent of zero is stored as \$40. Values above that are positive exponents (\$43 = 100^3 = 10^6) and values below are negative exponents (\$3F = 100^-1 = 10^-2).

The FP package doesn't use the full exponent range (-64 to +63) instead stopping at -/+50 to represent exponent values from 10^-99 to 10^99.

• 3
• 2
##### Share on other sites

Hi Teapot

Based on your explanation, I understand the following values for \$00D4:

\$40 -> 100^0

\$41 -> 100^1

\$42 -> 100^2

\$43 -> 100^3

...and so on up to 10^99. On the other hand we have:

\$3F -> 100^-1

\$3E -> 100^-2

\$3D -> 100^-3

...and so on up to 10^-99. Is this correct?

Kind regards,

Luis.

##### Share on other sites

Yes, that's correct.

And I had the range wrong - the limits are 10^-98 and 10^98 which means -97 and 97 are the last valid values. Also, while checking the behavior, I see that the Newell fast FP ROM doesn't do exponent range checking so you can go all the way to 100^63. You can even assign values beyond that (10^130) but it wraps around to negative exponent values.

You can use the Altirra debugger to easily see the stored values via BASIC.

Have a memory window looking at the address stored in \$86/\$87 (\$2093 on my default setup). This is the variable table and numbers are stored directly (the last 6 bytes of every 8 byte block).

Set a variable using no line number (A=123) and refresh the memory window. Keep setting the same variable and the value will get changed in the same memory.

• 3
• 1
##### Share on other sites

• 3 weeks later...

If I understand this correctly, this FastBasic program should show what's going on,

``` Hex\$ = "0123456789ABCDEF"
Dim Z%(1)

Do

Input "Number or Q to quit? "; X\$

If X\$ = "Q" Then Exit

Z%(0) = Val(X\$)

Print Z%(0); ":";

' Hex dump

For i = 0 to 5
j = Peek(Za + i)
. Print " "; j;
Print " ";
Print Hex\$[1 + (j / 16), 1];
Print Hex\$[1 + (j & 15), 1];
Next i
Print

' Quick and dirty Str\$(Z%(0))

Print " = ";
If (Peek(Za) & 128) Then Print "-";
For i = 1 to 5
j = Peek(Za + i)
Print Hex\$[1 + (j / 16), 1];
Print Hex\$[1 + (j & 15), 1];
If i = 1 Then Print ".";
Next i
Print " * 100 ^ "; (127 & Peek(Za)) - 64

Loop

Print
Print "OK"
Get ZZ```

Note: Strings start at 1, % means a variable is float, no type suffix means it's a signed word, -32768..32767

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.