int X = 0;
int Y = 1;
while(X <= 10 ){
if(X%2 == 0)
Y = Y * X;
else
Y++;
X++;
}
cout << "Y is: " << Y;
This is what I have for my Easy68k code.
ORG $1000
START: ; first instruction of program
MOVE.W #0,D1 ;PUT 0 IN D1 (X)
MOVE.W #1,D2 ;PUT 1 IN D2 (Y)
LOOP CLR.W D3 ;Find the remainder
MOVE.W D1,D3
DIVU #2,D3
SWAP D3
CMP #0,D3 ;Compare remainder with 0
BEQ EQUAL ;If equal, then go to equal
ADD.W #1,D2 ;Y++
ADD.W #1,D1 ;X++
CMP #11,D1 ;Compare D1 with 11
BEQ DONE ;If D1 equals 11, break loop.
BRA LOOP
EQUAL MULU.W D1,D2 ;Multiply D1 and D2 and store it in D2
ADD.W #1,D1 ;X++
CMP #11,D1 ;Compare D1 with 11
BEQ DONE ;If D1 equals 11, break loop.
BRA LOOP
DONE LEA MESSAGE,A1
MOVE.W #14,D0
TRAP #15
MOVE.W D2,D1
MOVE.W #3,D0
TRAP #15
SIMHALT ; halt simulator
MESSAGE DC.W 'Y is: ',0
END START ; last line of source
I'm not exactly sure what is incorrect about my code but I have a feeling it is an issue at the beginning of the loop section. I have followed along with the code but I still cannot figure out where it is going wrong. When I run it, it outputs Y is: 10. D1 and D2 are also both A or 10. Any help is appreciated.
After doing the division and swap you still have both result and remainder of the division in d3
. This means it will never be zero and the comparison is always false. You need to zero the upper part with and
or use a form.of cmp
that only uses the lower part.
Just a note: when you are doing remainders of powers of two you can also skip the division and use and
directly with the value minus one. In this case the remainder of division by two is the same as and
with the value 1.