How to use floating point kernal routines for 16 bit arithmetics such as: $1234*$ABCD or $1234/$ABCD ?
2 Answers
There's a great and much detailed description at the C64 Wiki (*1) describing the basic structures and workings, as well as all functions involved to use the ROM routines for floating point (*2)
Cookbook:
- Use
GIVAYF
to convert a 16 Bit number in A:Y into an FP in FAC - If multiple numbers are involved, move FAC to memory using
MOVMF
- Repeat for all numbers
- Do whatever function is needed, like
FMULT
for multiplication (FAC times saved value) - Use
FACINX
to convert the result from FAC back into a 16 bit in A:Y - Or
QINT
if you expect the result to need a 32 bit integer - like with a 16x16 multiplication.
As usual, countless variations thereof are possible, depending on the task at hand and requirements set.
*1 - Thanks to JeremyP for that link
*2 - Which BTW are not part of the Kernel, but belong to BASIC as Wilson pointed out :))
-
2You know, you can get much better-looking footnotes by using the
sup
tag, such as<sup>(1)</sup>
.– user6464Commented Oct 25, 2018 at 12:53 -
2For references, I often use
<sup>[\[1\]][1]</sup>
. That gets old really fast, though; it'd be good if there was built-in support for footnotes and references.– wizzwizz4 ♦Commented Oct 25, 2018 at 12:59 -
1@Raffzahn There's not a way to make linked footnotes, unfortunately; Stack Exchange ignores ID links like [this one](#anchor) (
[this one](#anchor)
).– wizzwizz4 ♦Commented Oct 25, 2018 at 14:19 -
3@Digerkam Your response suggests that you are after somebody to write code for you. Generally this is something people are paid to do; might I suggest hiring a programmer?– wizzwizz4 ♦Commented Oct 25, 2018 at 14:29
-
3@Digerkam - the answer here is quite clear even to a non expert. I've never programmed on the C64 (other than simple BASIC stuff), and my experience of 6502 assembly in general is fairly limited (a couple of small projects on the BBC micro), but I'm sure I could turn this into working code within an hour. In general, understanding how something works is more useful than having code to copy and paste because c&p code almost always needs customizing for the application, and if you don't understand it you can't do that. It's hard with most languages but essentially impossible in assembly language– JulesCommented Oct 25, 2018 at 16:47
Consider this an addendum to Raffzahn's answer. If you are going to accept one, accept his.
The code for your first multiplication might look like this (note I haven't tested this)
lda #$12
ldy #$34
jsr GIBAYF
lda some_memory_loc_low_byte
ldy some_memory_loc_highbyte
jsr MOVEFM
lda $AB
ldy $CD
jsr GIBAYF
lda some_memory_loc_low_byte
ldy some_memory_loc_highbyte
jsr FMULT
jsr FACINX ; Answer in A:Y
That's going to be a lot slower than doing the 16 bit multiplication directly though.
-
As an addendum to this if you look in the Apple ][ technical reference on pages 83/84 there are 16 bit integer multiple and divide routines. archive.org/details/applerefjan78/page/n83/mode/2up which you can use.– PeterICommented Dec 6, 2022 at 0:25
$B391
will convert from 16 bit to floating point and the routine at$B1AA
will convert the other way. And there are other routines for the arithmetic ops.