How can I take mod of a number in assembly in Moto

2019-01-12 08:07发布

问题:

How can I take mod of a number for instance a%9 in assembly in Motorola M6800.Please tell me which mnemonics should I use.

回答1:

At last if memory serves, the 6800 doesn't have a division instruction (IIRC that was added in the 6809), so you'll have to implement division on your own (or, if you don't care about speed, just subtract the divisor repeatedly until the result is less than the divisor, and that's your remainder).

To just figure the remainder (without the division) is actually pretty easy in binary:

  1. shift the divisor left until it's larger that the dividend
  2. shift it right one place
  3. If that's smaller than the dividend, subtract it from the dividend
  4. repeat steps 2 and 3 until what's left of the dividend is smaller than the divisor
  5. That's your remainder

For example, let's figure the remainder after dividing 127 by 9. We start by shifting 9 left:

127 = 0111 1111
9   = 0000 1001

shift left until you get:

  0111 1111
  1001 0000

Repeatedly shift and subtract:

      0111 1111
-     0100 1000
=     0011 0111

      0011 0111
-     0010 0100
=     0001 0011

      0001 0011
-     0001 0010
=     0000 0001

Since 1 is smaller than 9, we have our remainder: 1. In case you want to check that, 9x14=126.



回答2:

using easy 68k

#include <iostream>
using namespace std;
int main ()
{

    {cout << "THE MULTIPLES OF THREE FROM 1-30 ARE: " <<endl;
    int a;
    int sum =0;
    for (a=1; a<=30; a++)
    {if ((a%3) == 0)
    {cout <<a << "\n" <<endl;
    sum =sum+a;
    }}
    cout <<"\tSUM = " <<sum<<endl;
    }
    system ("Pause");
    return 0;
}