Here’s my attempt to write a function in TASM that returns true if a number is prime and false otherwise.
masm
model small
.data
pos db "Yes, it's Prime",13,10,"$"
negat db "No, it's not Prime",13,10,"$"
.stack 256
.code
isPrime proc
push bp;
mov bp,sp;
push bx;
push cx;
mov bx, [bp+4]
mov cx, 2;
cmp bx, 0; check for zero
je isPrimeFalse;
cmp bx, 3; ;check for <=3
jle IsPrimeTrue;
isPrimeCycle:
cmp bx, cx;
je IsPrimeTrue;
mov ax, bx;
xor dx, dx;
DIV cx;
cmp ah, 0h;
je IsPrimeFalse;
inc cx;
jmp isPrimeCycle;
IsPrimeFalse:
mov ax, 0; false
jmp IsPrimeExit;
IsPrimeTrue:
mov ax, 1;
IsPrimeExit:
pop cx;
pop bx;
pop bp;
ret 2;
isPrime endp
main:
push 5;
call isPrime
cmp ax, 0;
je no;
jmp yes;
yes: ...
no: ...
end main
It works for number that are not prime, but for prime number(number>3, for example 5 above) it still shows NO.
Where is my mistake? The reminder should be stored in AH, but when i debug the code above in ollydbg(i changed the registers to x32 to make it work) the reminder is stored in edx, not in ah. Why?! is it so in the code above?
My main problem is I can’t debug my code, because I use tasm and the only good powerful debugger I know is ollydbg, but it is x32bit assembler, not 16. So i have to change registers and maybe not everything is the same…
For
DIV CX(and generallyDIV r/m16) the remainder goes toDX, the quotient toAX.Only for 8-bit divisor the remainder goes to AH. But you obviously wanted 16-bit divisor, since you’re zeroing
DXbefore you divide.