I'm standing in front of a weird situation:
I have an assembly code that should return whether a number is prime or not.
In the middle of the code, I have a register that is in zero state when arriving to it; But - the code return always 0
in EAX
, unless I go to this register and explicitly put 0
in it -> Just then, and only then, the code return correctly 1/0 in EAX
when asking it for a prime number.
Here is a snippet of the code that does not work:
__asm__ (
"IS_PRIME:;"
"push EBP;"
"MOV EBP, ESP;"
"MOV ECX, 2;"
"_LOOP_PRIME:;"
"CALL IS_FACTOR;"
"CMP EAX, 1;"
"JZ FALSE_PRIME;"
"INC ECX;"
"CMP ECX, EBX;"
"JNZ _LOOP_PRIME;"
"MOV EAX, 1;"
"JMP DONE;"
"FALSE_PRIME:;"
"MOV EAX, 0;"
"DONE:;"
"MOV ESP, EBP;"
"POP EBP;"
"JMP FINAL;"
"IS_FACTOR:;"
"MOV ESI, ECX;"
"MOV EAX, EBX;"
"DIV ESI;"
"test EDX, EDX;"
"JNZ FALSE;"
"MOV EAX, 1;"
"JMP DONE_IS_FACTOR;"
"FALSE:;"
"MOV EAX, 0;"
"DONE_IS_FACTOR:;"
"ret;"
"FINAL:;"
);
And here is a snippet of replace when I the code do working:
"MOV ESI, ECX;"
"MOV EAX, EBX;"
"MOV EDX, 0;" # THIS IS THE INSTRUCTION I HAVE TO ADD FOR IT TO WORK.
"DIV ESI;"
My question is:
If anyway I get 0
in EDX
when getting there in the first code, then why do I have to explicitly put 0
in there (as in the second snip) for the code to return correctly for whether it got a prime number in EBX
or not?