Jump to content
  • Advertisement
Sign in to follow this  

X86 ASM question!

This topic is 3634 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to learn some X86 assembly, and found this code on the internet. The code is supposed to allow the user to enter a number, then determine whether it is prime or not. My question is in section L3 of isPrime. As far as I can see, this code only jumps to this section if the number in EAX is 1 or 2, but it also appears to be the only section that sets the ZERO flag! How does it handle a prime number that isnt 1 or 2? Also in the isPrime function: how does the program escape the loop in section L1? It appears to be infinite to me. Sorry if these are stupid questions, I am only familiar with ARM assembly. EDIT: The Irvine32.inc is just a library that allows you to read and write to the console in Windows, if you see a call not included, its from Irvine32
INCLUDE Irvine32.inc

heading BYTE "*** Prime Number Program *** (DEMO)",

prompt BYTE "Enter an integer between 2 and 2,147,483,647, ",
	"or enter 0 to quit: ",0
msgPrime BYTE " is prime.",0dh,0ah,0
msgNotPrime BYTE " is not prime.",0dh,0ah,0

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"

	call WriteDec		; display the number
	call Writestring		; display the message
	jmp  L1

	call Clrscr
main ENDP

IsPrime PROC
; Checks the integer in EAX to see if it is prime. If so,
; returns with ZF = 1; otherwise, clears the Zero flag.
	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
IsPrime ENDP

IsDivisible PROC
; Sets the Zero flag if EAX is evenly divisible by EBX.
	push  eax
	push  edx
	mov   edx,0
	div   ebx	; divide EAX by EBX
	cmp   edx,0	; remainder = 0?
	pop   edx
	pop   eax
IsDivisible ENDP

END main

Share this post

Link to post
Share on other sites
the loop instruction decrements ecx and will jump only if ecx is not 0, so eventually L1 will fall through to L3.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!