Team Program Project (PrimeNumberProgram)
Comment !
Description: This Program sets the Zero flag if the integer entered
by the user is prime. (A prime number is evenly
divisible by only itself and 1.)
!
INCLUDE Irvine32.inc
.data
heading BYTE "--- Prime Number Program ---",
0dh,0ah,0dh,0ah,0
prompt BYTE "Enter an integer greater than 0",
0dh,0ah,
"or enter -1 to quit: ",0
msgPrime BYTE " Is Prime.",0dh,0ah,0
msgNotPrime BYTE " Is Not Prime.",0dh,0ah,0
;----------------------------------------------------
;------------------ Main Procedure -------------------
.code
main PROC
call ClrScr
mov edx,OFFSET heading
call WriteString
L1: call Crlf
mov edx,OFFSET prompt
call WriteString
call ReadInt ; read value into EAX
call Crlf
cmp eax,0 ; does user want to quit?
jle Exit_main ; yes
call IsPrime ; is EAX prime?
.IF ZERO? ; yes:
mov edx,OFFSET msgPrime ; display "is prime"
.ELSE ; no
mov edx,OFFSET msgNotPrime ; display "is not prime"
.ENDIF
call WriteDec ; display the number
call Writestring ; display the message
jmp L1
Exit_main:
call Clrscr
exit
main ENDP
;--------------- End of Main Procedure ------------------
;----------------- Is Prime Procedure ---------------------
IsPrime PROC
; Checks the integer in EAX to see if it is prime. If so,
; returns with ZF = 1; otherwise, clears the Zero flag.
pusha
cmp eax,2 ; 1 and 2 are special cases
jbe L3 ; both are prime
mov ecx,eax ; get number for loop count
shr ecx,1 ; divide by 2
mov ebx,2 ; first divisor
L1: call IsDivisible ; EAX divisble by EBX?
jz L4 ; yes: quit
inc ebx ; next divisor
loop L1
L3: test eax,0 ; prime, so set the Zero flag
jmp L5
L4: or eax,0 ; not prime, so clear the Zero flag
jmp L5
L5: popa ;pops ESI in reverse order
ret
IsPrime ENDP
;--------------- End of Is Prime Procedure -----------------
;--------------- Is Divisible Procedure ----------------------
IsDivisible PROC
; Sets the Zero flag if EAX is evenly divisible by EBX.
push eax ;adds eax value to stack and increments
ESP by 4
push edx ;adds edx value to stack and increments
ESP by 4
mov edx,0
div ebx ; divide EAX by EBX
cmp edx,0 ; remainder = 0?
pop edx ;Subtracts the last edx value from ESP
pop eax ;Subtracts the last eax value from ESP
ret
IsDivisible ENDP
;------------------ End of Divisble Procedure -----------------------
END main
I HAVE TO CHANGE THIS CODE SO THAT IT WILL SHOW THAT THE NUMBER IS EVEN OR ODD INSTEAD OF PRIME AND DIVISIBLE SO IM THINKING I CAN CHANGE THIS CODE AROUND A LITTLE BIT INSTEAD OF WRITING A NEW ONE SO IM TRYING TO FIGURE OUT THE EVEN PROCEDURE AND THE ODD PROCEDURE