I'm working on a program that will find the prime factors to an unsigned integer that is entered by the user. I just have to make sure the program can handle a number up to 1000. I'm writing assembly in x86. I have already got a prompt for the user and got the number from the user, but I don't know how to go from here. I know I have to find the prime factors that make up that number using a jump loop and using divide, but I'm kinda lost here. Any help would be appreciated.

Thanks for the help.

Sincerely yours;

jdm

TITLE MASM Template						(main.asm)

INCLUDE Irvine32.inc

.data                                                      ;Declare values
myMessage  BYTE "Enter an unsigned integer:   ", 0         ;Stores a string
myMessage2 BYTE " ",0dh,0ah, 0                             ;Stores a string


.code                                                      ;Declare main
main PROC                                                  ;Declare proc
	call Clrscr                                            ;Clears the screen
	mov eax, 0                                             ;Clear eax
	mov ebx, 0                                             ;Clear ebx
	mov ecx, 0                                             ;Clear ecx
	
	mov edx, OFFSET myMessage                              ;Mov message into edx
	call writestring                                       ;Display to screen
	call readdec                                           ;Reads the input number and store into eax in decimal form.  


	call writedec                                          ;Display the value located in eax in decimal form on screen.  
	mov edx, OFFSET myMessage2                             ;Mov message into edx
	call writestring                                       ;Display to screen
	
	exit                                                   ;Exit main
main ENDP                                                  ;Exit proc

END main                                                   ;Tells the program that it is the end

This code:

mov     edx,0
        cmp     eax,1
        jle     mEx

        mov     ecx,eax
        shr     ecx,1
        mov     ebx,2
        mov     edx,1
mMain:
        cmp     ebx,ecx
        jg      mEx

        push    eax
        mov     edx,0
        div     ebx
        pop     eax
        cmp     edx,0
        je      mEx
        inc     ebx
        jmp     mMain
        mov     edx,1
mEx:

checks if number in eax is prime. If it is prime edx will contain 1, otherwise 0. Insert this code before line 25. Number up to 1000 are fine.

Edited 5 Years Ago by skaa: n/a

This code:

mov     edx,0
        cmp     eax,1
        jle     mEx

        mov     ecx,eax
        shr     ecx,1
        mov     ebx,2
        mov     edx,1
mMain:
        cmp     ebx,ecx
        jg      mEx

        push    eax
        mov     edx,0
        div     ebx
        pop     eax
        cmp     edx,0
        je      mEx
        inc     ebx
        jmp     mMain
        mov     edx,1
mEx:

checks if number in eax is prime. If it is prime edx will contain 1, otherwise 0. Insert this code before line 25. Number up to 1000 are fine.

Thank you for the help Skaa, I will give it a try, but I was wondering if it does what I need it too. I haven't really study push or pop or shr yet. What I need to do for example is have the user put the int 20 into the program, the program will display 2 * 2 * 5 as the prime factors that make up 20.

I really appreciate the help.

Sincerely yours;

jdm

I am sorry, I gave you incorrect explanation. This piece of code just checks if the number in EAX is prime or not prime. It can be solved without PUSH and POP but it is not good idea. PUSH command adds to stack, POP - extracts from stack.

Forget about my previous message. This is the code for your problem:

mov     eax,20;this number must be >1

mMain:
        mov     ecx,2
mPFactors:
        push    eax
        mov     edx,0
        div     ecx
        pop     eax
        cmp     edx,0
        je      mNext
        inc     ecx
        jmp     mPFactors
mNext:
        mov     edx,0
        div     ecx
;Print ecx
        cmp     eax,1
        je      mEx

        jmp     mMain
mEx:

This is same without PUSH and POP:

mov     eax,20;this number must be >1

mMain:
        mov     ecx,2
mPFactors:
        mov     ebx,eax
        mov     edx,0
        div     ecx
        mov     eax,ebx
        cmp     edx,0
        je      mNext
        inc     ecx
        jmp     mPFactors
mNext:
        mov     edx,0
        div     ecx
;Print ecx
        cmp     eax,1
        je      mEx

        jmp     mMain
mEx:

Thank you for the help Skaa. I really appreciate it. I'm taking it that the program doesn't accept 0 and 1, because they are not prime?

Thanks for the help again.

Sincerely yours;

jdm

Thanks for the help. I really do appreciate it.

Sincerely yours;

jdm

Edited 5 Years Ago by jdm: n/a

This question has already been answered. Start a new discussion instead.