2

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

  • 1
    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 Oct 25 '18 at 8:48
  • 3
    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 Oct 25 '18 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 Oct 25 '18 at 10:06
  • 1
    @Digerkam The first question here would be why converting it to FP and doing slow FP when the task itself is about an integer multiplication? – Raffzahn Oct 25 '18 at 10:24
  • 3
    A nitpick: On the C64, the kernal does not do any floating point; only BASIC does. – Wilson Oct 25 '18 at 12:02
12

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

  • 2
    You know, you can get much better-looking footnotes by using the sup tag, such as <sup>(1)</sup>. – user6464 Oct 25 '18 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 Oct 25 '18 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 Oct 25 '18 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 Oct 25 '18 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 Oct 25 '18 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.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.