SeaGtGruff Posted January 28, 2008 Share Posted January 28, 2008 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.wikidot.com/software-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 Quote Link to comment Share on other sites More sharing options...
SeaGtGruff Posted January 28, 2008 Author Share Posted January 28, 2008 (edited) 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 January 28, 2008 by SeaGtGruff Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.