Start New Discussion within our Software Development Community

hey all, I'm just starting out with assembly in college and I wrote a program for an assignment to calculate prime numbers, and it works, however if you comment out the line in the code, it gives out this list of numbers:

3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
25 is prime
29 is prime
43 is prime

which is (obviously) wrong. I have no idea why!

.model flat,stdcall
option casemap:none
INCLUDE \masm32\include\
INCLUDE \masm32\include\
INCLUDE \masm32\include\
INCLUDE \masm32\include\
INCLUDE \masm32\macros\macros.asm
INCLUDELIB \masm32\lib\kernel32.lib
INCLUDELIB \masm32\lib\msvcrt.lib
INCLUDELIB \masm32\lib\masm32.lib
max WORD 50
counter WORD 1
scounter BYTE 1
mov counter, 3 ; start number counter at 3
mov scounter, 3
	mov bl, 1;start divisor at 1

		                ; I don't know why, but the program
	print chr$(13,13) ; gets the wrong answers unless this line
		               ; is here. I was using 13,10 for new line,
		               ; but it doesn't seem to matter as long as
		               ; print is called

	add bl, 2	

	cmp bl, scounter
	jge itsprime

	mov al, scounter
	div bl
	cmp ah, 0
	je done
	mov al, scounter
	cmp bl, al
	jl again
	mov ax, counter
	INVOKE crt_printf, chr$("%d is prime"), ax, bx
	print chr$(13,10)

	mov ax, counter
	cmp ax, max
	jge finished
	add counter, 2
	add scounter, 2
	jmp cnt
INVOKE ExitProcess, 0
end start

This should fix the problem with your code.
You see, an 8-bit divisor divides by the dividend in AX, so do this:

xor ah, ah ; right here, add this instruction
mov al, scounter
div bl
This article has been dead for over six months. Start a new discussion instead.