4

How to use floating point kernal routines for 16 bit arithmetics such as: $1234*$ABCD or $1234/$ABCD ?

10
  • 2
    Please edit your question to make it clearer (what's your actual question?) and ensure it's actually answerable. See How to Ask for guidelines.
    – wizzwizz4
    Commented Oct 25, 2018 at 8:48
  • 4
    I was just in the middle of writing an answer and it's closed as "too broad". There's no way asking how to convert a 16 bit number to floating point on the C64 is too broad.
    – JeremyP
    Commented Oct 25, 2018 at 9:42
  • 1
    @Digerkam It's a bit difficult to write code in a comment but have a look at the C64 Wiki. The routine at $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.
    – JeremyP
    Commented Oct 25, 2018 at 10:06
  • 3
    A nitpick: On the C64, the kernal does not do any floating point; only BASIC does. Commented Oct 25, 2018 at 12:02
  • 2
    The question was recently edited to change 'kernal' to 'kernel'. Since this is about a Commodore machine, the incorrect spelling was in fact the actual name of the code. Reading 'kernal' causes me physical pain, but it is historically accurate.
    – dave
    Commented Dec 3, 2022 at 0:13

2 Answers 2

14

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 :))

10
  • 2
    You know, you can get much better-looking footnotes by using the sup tag, such as <sup>(1)</sup>.
    – user6464
    Commented Oct 25, 2018 at 12:53
  • 2
    For 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
    – Jules
    Commented Oct 25, 2018 at 16:47
4

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.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .