Reputation: 8610
I am learning assembly language on x86 and made a simple programe that will tell number is prime or not.
I think I am doing right but still not getting the desired result,below is code
section .bss
b db
section .data
x db "Number is Prime",10,0
y db "Number is not Prime",10,0
z db "value is=%d",10,0
section .text
global main
extern printf
main:
mov eax,17
mov ebx,2
loop:
mov [b],eax
div ebx
mov eax,[b]
cmp edx,0
jz Print_not_Prime
inc ebx
cmp ebx,17
jnz loop
push x
call printf
add esp,4
ret
Print_not_Prime:
push y
call printf
add esp,4
ret
In above code I am checking with number 17 and output of programe telling its not prime number.
Could anybody let me know where I am doing wrong
Upvotes: 0
Views: 2532
Reputation: 22552
You need to zero edx
before each divide. The dividend for div
comes as a double-register operand in edx:eax
.
If you have a remainder in there from the last trial division then it will screw up your results.
Also it's better to store your dividend in a register (ecx
, esi
, edi
are still unused) or at least on the stack then in memory which as Michael pointed out is insufficient to store a dword.
Upvotes: 2
Reputation: 58467
I see at least two problems with this:
b db
....
mov [b],eax
You're only reserving space for a byte at b
but storing a dword (4 bytes). You should use dd
instead of db
.
div ebx
You should use cdq
(or xor edx,edx
) prior to div
in order to clear edx
, since this division will divide edx:eax
by ebx
.
Upvotes: 2