Question: The following code is supposed to get the first 1 0 0 prime numbers using assembly lang. It runs indefinitly with no progress. Could you

The following code is supposed to get the first 100 prime numbers using assembly lang. It runs indefinitly with no progress. Could you help me fix it:
.586
.MODEL FLAT
INCLUDE io.h
.STACK 4096
.DATA
primeArray DWORD 100 DUP (?)
.CODE
_MainProc PROC
mov eax, 2 ; initialize prime[1] to 2
mov dword ptr primeArray[0], eax ; store prime[1]=2
mov eax, 3 ; initialize prime[2] to 3
mov dword ptr primeArray[4], eax ; store prime[2]=3
mov ecx, 2 ; initialize primeCount to 2
mov esi, 5 ; initialize candidate to 5
outerLoop:
cmp ecx, 100 ; check if primeCount <100
jge endOuterLoop ; if not, exit loop
mov ebx, 1 ; initialize index to 1
innerLoop:
mov eax, dword ptr primeArray[ebx*4] ; load prime[index] into eax
cmp eax, 0 ; check if prime[index] is 0(end of array)
je primeFound ; if so, prime not found, jump to primeFound
mov edx, 0 ; clear edx for division
mov eax, esi ; load candidate into eax
div dword ptr primeArray[ebx*4] ; divide candidate by prime[index]
cmp edx, 0 ; check if there is a remainder
je notPrime ; if no remainder, candidate is not prime, jump to notPrime
add ebx, 1 ; increment index
jmp innerLoop ; repeat inner loop
notPrime:
add ebx, 1 ; increment index
jmp innerLoop ; repeat inner loop
primeFound:
cmp ebx, ecx ; check if index > primeCount
jle incrementCandidate ; if not, candidate is not prime, increment candidate
mov dword ptr primeArray[ecx*4], esi ; store candidate in primeArray at primeCount
add ecx, 1 ; increment primeCount
incrementCandidate:
add esi, 2 ; increment candidate by 2
jmp outerLoop ; repeat outer loop
endOuterLoop:
mov eax, 0 ; return 0
ret
_MainProc ENDP
END

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!