Need some quick 68K assembly help...

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
I can't seem to find much about 68K asm, and I'm not sure if what I have is correct... But lets say we have 2 positive integers of word length in the first two data registers... like so:

MOVE.W #10,D0
MOVE.W #3,D1

So now I wanna find the remainder of dividing D0 by D1. I can't find any instruction for modulus, so I figured I need to get creative... My idea is as follows:

edit: fixed error pointed out by ChristianV

CLR.L D2
MOVE.W D0,D2
DIVU D1,D2
SWAP D2


^^ so basically:
- Clear out all 32 bits of D2
- Copy the contents of D0 into D2's last 16 bits (word length)
- Divide D2 by D1... D2 should now have the remainder in the upper 16 bits? (i think)
- Swap the upper and lower words of D2...
- And now I can refer to D2.W as the remainder of D0 divided by D1?

^^ This seems okay in theory... but I really don't know. Any advice?

edit: This is confirmed working. Thanks. I'll leave this hear in the off chance that someone's searching for the same solution.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
It's been years since I've done any assembly, and it was 6502, 8080 and x86. But for intel I think some division operations store the remainder into another register, and remainder == modulus. Check whether 68k division does this too.

Brute force would be something like:
do ( if a > b, then a -= b ) while (a > b)
 

ChristianV

Member
Feb 5, 2007
65
0
0
Yes, on Intel it works like this, but in 68k assembly, the remainder is stored in bits 16-31, the result in bit 0-15, in the same register. The SWAP command is the right one to use. But isn't the result stored in D1? because, you can execute DIVU #4,D1 (I assume #4 means that it is an immediate), so the result is stored in D1, obviously. That's why I think it has to be D1, too, in your example. However, I'm not sure because I've never written anything in 68k assembly.