prime number generator

i am  new to assembly language and was wondering if the experts could point me in the right direction.
i have this code (give below)that needs to calculate first 100 prime number and display them 5 in a row
the code seems to stop executing around this line: mov [ebx], bx(in the IfStatement)
is this allowed(moving a byte register into indirect register).it seems to work earlier on in the code
mov ax, 2
mov [ebx], ax
add ebx, 2
mov ax, 3
mov [ebx], ax

And if this isn't the problem, then what is?

Help would greatly be appreciated...


ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD




GetStdHandle PROTO NEAR32 stdcall,

WriteFile PROTO NEAR32 stdcall,
    hFile:DWORD, lpBuffer:NEAR32, nNumberOfCharsToWrite:DWORD,
    lpNumberOfBytesWritten:NEAR32, lpOverlapped:NEAR32

CreateFileA PROTO NEAR32 stdcall,
    lpFileName:NEAR32, access:DWORD, shareMode:DWORD,
    lpSecurity:NEAR32, creation:DWORD, attributes:DWORD, copyHandle:DWORD

CloseHandle PROTO NEAR32 stdcall,


fileName   BYTE "p4out.dat",0

nbrArray    WORD    101 DUP (?)
written    DWORD  ?

hStdOut    DWORD  ?

hFile      DWORD  ?
candidate      wORD      ?
index      WORD      ?
primecount      WORD      ?

i      wORD      ?
j      wORD      ?
P      BYTE      6 DUP (' ')
Lines1      BYTE      'First 100 prime numbers are: '

           INVOKE GetStdHandle,       ; handle for console output
           mov    hStdOut, eax

         mov candidate, 4
         ;mov primecount, 2
         lea ebx, nbrArray
         mov ax, 2
         mov [ebx], ax
         add ebx, 2
         mov ax, 3
         mov [ebx], ax
         mov cx, 2

WhileBegin:cmp cx, 100
         jnl EndWhileBegin
         mov index, 1
         lea ebx, nbrArray

While1:         mov cx, 0
         cmp index, cx
         jnle IfStatement
         mov sp,[ebx]
         sub dx, dx
         mov ax, candidate
         jmp DThisMod

While2:         cmp index, cx
         jnle IfStatement
         add ebx, 2

         mov sp, [ebx]
         sub dx, dx
         mov ax, candidate

DThisMod:  div sp
         cmp dx, 0
         je IfStatement
         inc index
         jmp while2

IfStatement:cmp index, cx
          jng elseStatement
          inc cx
          add ebx, 2
          mov bx, candidate
          mov [ebx], bx

elseStatement:inc candidate
            jmp whileBegin


           ;INVOKE GetStdHandle,       ; handle for console output
           ;mov    hStdOut, eax

           ;INVOKE WriteFile,
             ;hStdOut,                 ; file handle for screen
             ;NEAR32 PTR Lines1,      ; address of message
             ;29,                      ; length of message
             ;NEAR32 PTR written,      ; bytes written
             ;0                        ; overlapped mode

           INVOKE CreateFileA,        ; open file
             NEAR32 PTR fileName,     ; file name
             GENERIC_WRITE,           ; access
             0,                       ; no sharing
             0,                       ; no predefined security
             CREATE_ALWAYS,           ; open even if file does exist
             0,                       ; no special attributes
             0                        ; no copied handle
           mov    hFile, eax          ; handle for file

         lea ebx, nbrArray
         mov i, 1
         cmp i, 5
         jnl EndLoop

         mov j, 1

A:         cmp j, 6
         jnl B
         mov ax,[ebx]
         itoa P, ax

           INVOKE WriteFile,
             hfile,                 ; file handle for screen
             NEAR32 PTR P,      ; address of prompt
             7,                      ; length of prmpt
             NEAR32 PTR written,      ; bytes written
             0                        ; overlapped mode
         inc j
         add ebx, 2
         jmp A

B:         inc i
         cmp i, 5
         jnl EndLoop
         mov j, 1
         jmp A


           INVOKE  CloseHandle,       ; close file handle

           INVOKE  ExitProcess, 0     ; exit with return code 0

PUBLIC _start                         ; make entry point public
END                                   ; end of source code

Who is Participating?
mtmikeConnect With a Mentor Commented:
This code is indeed not right:

mov bx,candidate
mov [ebx],bx

Note that bx and ebx refer to the same register.

ebx: 32-bit register
bx: first 16 bits of ebx
bl: first 8 bits of ebx
bh: second 8 bits of ebx

The same goes for eax (ax,al,ah), ecx (cx,cl,ch) and edx (dx,dl,dh).
Where did you get the idea to use sp as a data register?    

sp is your stack pointer, if you mess that up all kinds of very bad things will happen.

Use some register other than sp!

troycastor5Author Commented:
i used si but i still cant get the code to work..

could i use the stack to implement this?if yes,how?
A bunch of stuff regarding Assembly and primes can be found here:

mtmike is right.  When you do this:

mov bx, candidate

you destroy the value of ebx, and therefore destroy your pointer to where you want to put the results.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.