Professors generally do not give assignments that involve things that you have not learned yet. For an external observer, (such as anyone reading this question,) most chances are that you were not paying attention, or that you did not realize that what was being explained was a way to achieve a modulus operation.
Modulus in x86 assembly can be obtained by dividing two numbers. You put the divident in some register, you execute some instruction supplying the divisor, and after the instruction has executed some register receives the quotient and another register receives the remainder. But that's irrelevant, because you have probably not learned the division operation yet, and that's okay, because we are not going to use division.
In x86 assembly (and in any other assembly for that matter) you can very easily calculate the remainder of a division by a power of two without having to use a division operation. And 2 is a power of 2. (The 1st power of 2.)
Actually, things become even simpler in the case of 2, because I hope you agree that it is self evident that the remainder of a division by 2 has only two possible outcomes: either 1 or 0.
As you might recall, a number in binary looks something like this: 0011101010
The leftmost bit is the most significant bit, and the rightmost bit is the least significant bit. And it is a fundamental property of the binary system of representing numbers that the least significant bit of a number always represents the remainder that you would receive if you divided that number by two. (Just as in the decimal system the rightmost digit represents the remainder that you would receive if you divided that number by 10.)
So, all you need to do is to isolate the least significant bit from the number. This will be 0 or 1, and it will represent the remainder of the division of the number by 2.
"Precisely how to achieve this is left as an exercise to the student."
(Give it a try, and if you cannot do it, post another stackoverflow question.)
Regarding the JE/JNE part, it is actually wrong, because when CX ceases to be less than 99 the JL Loop1
instruction will fall through to the ifwrong:
label, which is not what you want. You should rewrite it as follows:
JE iftrue
ifwrong: ;unnecessary label, for illustration purposes only
SUB AX, CX
JMP after
iftrue:
ADD AX, CX
JMP after ;unnecessary instruction, for illustration purposes only.
after:
INC CX
CMP CX, 99
JL Loop1
Note that you do not exactly want ADD AX, CX
and SUB AX, CX
, you want ADD AX, MM
and SUB AX, MM
, where MM
is CX modulus 2, and which you are still in the process of figuring out how to calculate.
Also note that I am not vouching as to whether CMP CX, 99
followed by JL Loop1
is correct, you have not asked about that, you will probably encounter it later, but it should be easy to figure out.