Page 1 of 1

Best way to get the modulus of two 8bit values?

Posted: Sat Jul 09, 2005 6:16 am
by mbrenaman
Hello everyone,

I'm new to programming the 6502 (and the NES) and need to know the best way to obtain the modulus of a given (unknown) number and 80 and 81 (X%80 and X%81). Right now I'm using an 8bit / 8bit division routine I found in an old text book. Seemed to me the only way to obtain the mod for those off numbers (80 and 81).

So, I'm asking, does anyone have a clever solution to doing this other then using a normal 8bit / 8bit subroutine (it is still a good bit of instructions).

Thank you in advance. And thanks for keeping this site up. I wouldn't be working on an NES game without it.

Posted: Sat Jul 09, 2005 6:34 am
by tepples
Is there a reason why you can't just use a divide-free algorithm in general? What will you be using the result of x%80 and x%81 for?

Posted: Sat Jul 09, 2005 7:22 am
by Guest
I'm making a fall down kinda of game with vertical scrolling. When the scroll Y index%80 and 81 == 0, I want to update the name table with platform tiles instead of the normal background. This way, when scroll Y index == 0, 80, and 160, I'll have a platform two tiles thick running across the screen from left to right at those positions.

Posted: Sat Jul 09, 2005 7:41 am
by tepples
If your goal is to catch when Y equals 0, 1, 80, 81, 160, or 161, it might be best to brute-force it. In the following code, each cpy/beq pair that does not match takes only 4 cycles.

Code: Select all

  cpy #0
  beq draw_platform
  cpy #1
  beq draw_platform
  cpy #80
  beq draw_platform
  cpy #81
  beq draw_platform
  cpy #160
  beq draw_platform
  cpy #161
  beq draw_platform

Posted: Sat Jul 09, 2005 7:46 am
by blargg
Am I missing something here?

Code: Select all

while ( n >= 80 )
    n = n - 80;

Posted: Sat Jul 09, 2005 7:51 am
by mbrenaman
Thanks both of you. Sorry. You can see why I posted in the Newbie Help Center. I'll keep blargg's suggestion in mind but I'm going with brute force since there's only 6 values.

Again, thanks.

Woops,

Posted: Sat Jul 09, 2005 4:14 pm
by mbrenaman
Woops. Just to clarify. The values I meant were 0,7,79,87,159,167. See what happens when you stay up all night...

Posted: Mon Jul 11, 2005 5:08 am
by xian106
I think you can keep blargg's suggestion, just change the value to 0,7,79..which you want.
Maybe I don't know what is your problem.