Jump to content



0

Function: Divide 16 bits by 8 bits


1 reply to this topic

#1 SeaGtGruff OFFLINE  

SeaGtGruff

    River Patroller

  • 4,543 posts
  • Location:Georgia, USA

Posted Mon Jan 28, 2008 2:03 AM

I've already posted this function in another thread, but I'm reposting it as a separate thread for increased visibility.

Here's a user-defined function that you can add to your bB program if you want to be able to divide a 16-bit number by an 8-bit number. It's taken directly from a routine I found here:

http://6502org.wikid...are-math-intdiv

   function div16by8
   asm
   LDX #8
   ASL temp2
div16by8_1
   ROL
   BCS div16by8_2
   CMP temp3
   BCC div16by8_3
div16by8_2
   SBC temp3
   SEC
div16by8_3
   ROL temp2
   DEX
   BNE div16by8_1
   RTS
end
To call the function, you must pass it three parameters. When the function exits, the two parts of the answer will be in the accumulator and temp2.

The following pseudocode example uses the function to divide a 16-bit integer by an 8-bit integer, giving a 16-bit integer as the result:

   quotient_hibyte = div16by8 (numerator_hibyte, numerator_lobyte, divisor)
   quotient_lobyte = temp2
This next pseudocode example uses the function to divide an 8.8 fixed-point (16-bit) number by an 8-bit integer, giving an 8.8 fixed-point number as the result:

   quotient_wholepart = div16by8 (numerator_wholepart, numerator_fractionalpart, divisor)
   quotient_fractionalpart = temp2
As you can see, they're exactly the same, except for the way we interpret the parameters and results.

This routine is for *unsigned* values, and hasn't been tested with "negative" numbers.

Michael

#2 SeaGtGruff OFFLINE  

SeaGtGruff

    River Patroller

  • 4,543 posts
  • Location:Georgia, USA

Posted Mon Jan 28, 2008 7:50 AM

Please disregard my previous explanation. This routine does *not* work as advertised-- although it turns out that it *does* do exactly what I was originally looking for. I need to revise it a little bit, then post a new explanation.

Michael

Edited by SeaGtGruff, Mon Jan 28, 2008 7:50 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users