Reputation: 11
Translate the following code into assembly language and display the final value of Y. (2 pts.)
X = 1; Y = 2; WHILE(X <= 10 ){ IF((X%2) != 0) Y = Y * X; ELSE Y++; X++;} Output Y
PLEASE LET ME KNOW IF I AM DOING EVERYTHING RIGHT BECAUSE I AM NOT SURE IF MY OUTPUT IS RIGHT. RESULT IS 3712
*-----------------------------------------------------------
* Title :
* Written by :
* Date :
* Description:
*-----------------------------------------------------------
ORG $1000
START: ; first instruction of program
* Put program code here
MOVE.W #1,D1 ;PUT 0 IN D1 (X)
MOVE.W #2,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
BNE NOTEQUAL ;If NOT equal, then go to NOT 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
NOTEQUAL
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
* Put variables and constants here
MESSAGE DC.W 'Y is: ',0
END START ; last line of source
Upvotes: 0
Views: 742
Reputation: 18807
This will result in unexpected results, if the upper word of D3
contains data larger 1.
LOOP CLR.W D3 ; D3 = [**** 0000]
MOVE.W D1,D3 ; D3 = [**** X]
DIVU #2,D3 ; if D3.h > 1, division overflows and D3 does not change
SWAP D3 ; Lower word is (X%2) or random data
If you really want to use DIVU
make sure to clear the long register (with clr.l d3
or actually faster moveq #0,d3
). Just clearing the lower word is pointless, if you overwrite that afterwards.
But as @PeterCordes said, DIVU
is very inefficient to check the lower bit (I'll not point out other inefficiencies in your code). Better use btst #0,d1
(sets the Z
flag if the bit is empty).
Alternatives are:
and.w
the number 1 with the value you want to test. Also sets the Z
-flag if the lowest bit is emptyC
flag, if the lowest bit was set)Of course, you can also partially unroll the loop, and exploit the fact that (X%2) != 0
is true every other iteration. And you could fully unroll the loop and directly move the result into your target (that's what most optimizing compilers will be able to do: https://godbolt.org/z/VGecJ3).
Upvotes: 3