-5

Problem- Using the Intel86 simulator, given three one-byte numbers X, Y and Z (1<X,Y,Z<100), write the code that computes the modular exponentiation X^y mod Z (Example, X=4, Y=3, Z=5 result = 4). X, Y and Z ARE FOUND in memory positions 107H, 108H and 109H of your program, so make sure you do not use those bytes for instructions. The result of the calculation should must be stored in DL.The algorithm code to be used to compute the modular exponentiation X^y mod Z is: d,1. Assume that Y is represented by bits Yk,Yk-1,Yk-2,...Y0 For j <-- k down to zero d <-- (dd) % Z IF Y1 == 1 then d <-- (dX) % Z result is in d.

Craig S. Anderson
  • 6,966
  • 4
  • 33
  • 46
Ian
  • 5
  • 2
  • 1
    If you don't understand your assignment you should talk to your tutor about it. [So] is not a place to get your homework done free of charge. –  Apr 28 '15 at 01:13
  • Consider visiting http://stackoverflow.com/tour to learn how SO works. – brodoll Apr 28 '15 at 01:15
  • Simply asking if someone can translate this problem to me better than my "tutor" can. – Ian Apr 28 '15 at 01:18
  • Could you please describe what "Intel86 simulator" is supposed to be. – rkhb Apr 28 '15 at 17:10

2 Answers2

1

Seems the algorithm is poorly worded, and also backwards, assuming the goal is to repeatedly square X mod Z to speed up the process.

    R = 1;
    goto loop1;
loop0: 
    if(Y & 1) /* if least signficant bit of Y is set */
        R = (R * X) % Z;
    X = (X * X) % Z
    Y = (Y >> 1) /* logical (unsigned) shift right) */
loop1:
    if(Y != 0)
        goto loop0;
    DL = R;
rcgldr
  • 27,407
  • 3
  • 36
  • 61
1

I guess there is a typo "IF Y1 ..." should be "IF Yj ...". Also the algorithm is incomplete, there should be a shift anywhere. Here is an example for the algorithm in C:

#include <stdio.h>
int main (void)
{
    unsigned char X = 4, Y = 3, Z = 5;

    printf("(%d ^ %d) %% %d = ",X,Y,Z);

    unsigned int d = 1;
    for (int j=7; j>=0; --j)
    {
        d = (d*d) % Z;
        if (Y & 0x80)           // leftest bit (Yj) set?
        {
            d = (d * X) % Z;
        }
        Y <<= 1;
    }

    printf("%d\n", d);

    return 0;
}

If I only knew what "Intel86 simulator" is ... ;-)

rkhb
  • 14,159
  • 7
  • 32
  • 60