Reputation: 49
My program prints the msg3 statement (PutStr msg3
), but does not proceed to the
DIV CX
instruction in my program.
Is there something I'm doing incorrectly with that register?
Or should the instruction be
DIV [CX]
instead or do I not have the compare and jump conditions set correctly?
prime_loop:
sub AX,AX ;clears the reg to allow the next index of the array
sub CX,CX ;clears counter to decrement starting from number of the value for array
mov AX, [test_marks+ESI*4] ;copy value of array at index ESI into reg
mov CX, [test_marks+ESI*4] ;copy value of array at index ESI into reg for purposes of counting down
check_prime:
dec CX
nwln
PutStr msg3
div WORD CX ;divide value of EAX by ECX
cmp DX,0 ;IF the remainder is zero
je chck_divisor ;check to see divisor 'ECX'
sub AX,AX ;else clear quotient register EAX
sub DX,DX ;clear remainder register
mov AX,[test_marks+ESI*4] ;move the number of the current iteration back into EAX
jmp check_prime ;start again from loop
chck_divisor:
cmp CX,1
jne prime_loop ;if the divisor is not 1 then it is not a prime number so continue with iterations
PutInt AX ;else print the prime_num
PutStr
inc ESI
jmp prime_loop
done:
.EXIT
Upvotes: 2
Views: 1503
Reputation: 39306
These are some points about your code:
If this is indeed 8086 assembly then instructions like mov AX, [test_marks+ESI*4]
that use scaled indexed addressing simply don't exist!
The scale by 4 suggests that your array is filled with doublewords, yet you use just a word. This could be what you want, but it looks suspicious.
Let's hope no array element is 1 because if so, then the div cx
instruction will trigger an exception (#DE). Because you don't test the CX register for becoming 0.
In the check_prime loop only the 1st iteration lacks the zeroing of DX in order to give a correct quotient.
The solution will depend on the targetted architecture 8086 or x86. Now your program is a mix of both!
Upvotes: 1
Reputation: 28921
It's possible that since DX is not zeroed before div, that you're getting overflow. I don't know how your environment handles overflow.
Upvotes: 0