0

I'm trying to find all the prime factors of an inputted number. I was given the following Java code to convert to assembly.

    String getFactors(int number) {
    String answer = "";
    for(int factor=2; factor <= number; factor++) {
        if(number % factor == 0) {
            answer += factor + "*";
            number /= factor;
            factor--;
            }
        }
        return answer.substring(0, answer.length()-1);
    }

My attempt so far:

D3=factor

move.w  #1,D3   
loop1:
add1        #1,D3
cmp.w       input,factor
BHI     done
divu        factor,input
tst.b       input
BNE     loop1
add.w       factor,answer
move.w  answer,(A1)+
move.b  #’*’,(A1)+
add.w       factor,input
subq        #1,factor
BRA     loop1

done:
2
Contributors
1
Reply
17
Views
2 Years
Discussion Span
Last Post by Schol-R-LEA
0

Since you don't mention what architecture and assembler you are writing it for, it is hard to judge whether it is correct or not. I am assuming it is for the M68000 processor, correct?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.