7800basic Modulus

Recommended Posts

In the 7800basic guide it says that modulus can be performed on variables using "//" but I have been unable to get it to work, it doesn't seem to have any effect on the variable at all. I've tried to write the code in different ways but no such luck. I would appreciate any help with this, thanks.

Here's a snippet of the code:

```scrollskip2
count1 = count1+1

if ((count1 // 4)=0) then count2 = count2+1

if count2 = 255 then goto introduction

goto TitleScreenLoop```

...and the entire bas file if needed

Share on other sites

I don't know why the modulus isn't working, but I know another way to achieve the same thing that I think would also take less CPU cycles. If you do a bitwise AND of count1 with 3, then you are only looking at the last two bits, so it will equal 0 every 4 iterations like I think you are intending here:

```scrollskip2
count1 = count1+1

if ((count1&3)=0) then count2 = count2+1

if count2 = 255 then goto introduction

goto TitleScreenLoop```

Share on other sites

Oh okay, this will work great! Thanks for the help

Share on other sites

Looking into it a bit more, the 7800basic documentation doesn't say much about modulus. Batari Basic implements it by putting the modulus value in the temp1 variable. I tested this, and it appears that either modulus isn't currently implemented in 7800basic, or it is implemented differently and not yet documented in the version of the docs that I saw.

```    Temp2 = 10 // 3
Temp1 = temp1
plotvalue scoredigits_8_wide 2 Temp1 2 56 11
plotvalue scoredigits_8_wide 2 Temp2 2 56 10```

Share on other sites

@RevEng - just making sure you saw this one.

Share on other sites

Thanks Karl. It's on my radar now.

Share on other sites

Just a heads up that it looks like // is broken in bB too, which is where 7800basic got the operator. I'll need to study the 16-bit divide routine to understand it better, and see where it's going off the rails.

Share on other sites

I've fixed this in my private builds for 7800basic and bB. The original div16 routine came from here and I don't think it was ever put through it's paces. I've replaced it with the one here and the results for // are correct now. (except remainders are wrong if you use a constant power-of-two divisor, due to power-of-two optimisations, which I'll document) I'll get some new releases out once I spend some time on incorporating both 32-bit and 64-bit binaries. (thanks Apple)

All of that said, using masks as Karl advised is the preferred method for that sort of thing. I've honestly never liked the idea of the remainder operator, since it's wasteful compared to masks.

Share on other sites

Looking for a way to find an arbitrary remainder (not guaranteed to be power of 2) and still seeing an issue where modulus doesn't work as expected. RevEng, did your changes make it into the release builds? I'm using Atari Dev Studio 0.7.1, so it should be 7800basic 0.18.

Share on other sites

The new routine should definitely be in there, though it's entirely possible you've run into some other issue. When I get a chance, I'll write up an exhaustive test.

Share on other sites

It should have been said that // is used to comment out code a la C/C++ and Javascript.

Share on other sites
1 hour ago, RevEng said:

The new routine should definitely be in there, though it's entirely possible you've run into some other issue. When I get a chance, I'll write up an exhaustive test.

I think it might be. The case I've found is when the dividend is less than the divisor, the result is always zero.

Share on other sites

I think it might be. The case I've found is when the dividend is less than the divisor, the result is always zero.

Ah, looking at how the routine runs, I think that's an overlooked case. I'll add a comparison check to the beginning of the routine to cover it.

Until the next release, I'd recommend you just add an if...then test for this case to your code.

Share on other sites
32 minutes ago, RevEng said:

Ah, looking at how the routine runs, I think that's an overlooked case. I'll add a comparison check to the beginning of the routine to cover it.

Until the next release, I'd recommend you just add an if...then test for this case to your code.

It's not time critical, but I'm glad we know what it is. Plenty more stuff get my head around still.

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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.