Hi forum,
I wrote a procedure in MASM 6.15 which takes a 32-bit unsigned int from the user and tells the user whether it is a prime number or not. Now the program loop works fine with small numbers (obviously) but with larger numbers the program takes anything up to 5-10 secs to calculate whether its prime or not!! Plz take some time to look at the code and advise me how I may possibly optimize the loop to run faster.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Thank you
KG.
I wrote a procedure in MASM 6.15 which takes a 32-bit unsigned int from the user and tells the user whether it is a prime number or not. Now the program loop works fine with small numbers (obviously) but with larger numbers the program takes anything up to 5-10 secs to calculate whether its prime or not!! Plz take some time to look at the code and advise me how I may possibly optimize the loop to run faster.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Code:
CheckIfPrime proc uses eax ecx edx
cmp input, 0
je Finish ; Finish is global label inside main
mov ebx, input ; input is global mem var, stores user 'input'
mov ecx, input
CheckPrime:
mov eax, input ;move input into eax
dec ebx ;decrement ebx
cmp ebx, 1 ;is ebx = 1?
je IsPrime ;if so, number 2 is prime
cmp ebx, 0 ;is ebx = 0?
je NotPrime ;if so, number 1 is not prime
mov edx, 0 ;clear edx
div ebx ;divide eax by ebx
cmp edx, 0 ;is the remainder of the division 0
je NotPrime ;if so, number is not prime
Loop CheckPrime
NotPrime:
;Display msg indicating num NOT prime
IsPrime:
;Display msg indicating num IS prime
ret
CheckIfPrime endp
Thank you
KG.