prime number generator

hi
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...


.386
.MODEL FLAT

ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD

INCLUDE io.h

STD_OUTPUT    EQU  -11

GENERIC_WRITE EQU  40000000h
CREATE_ALWAYS    EQU  2

GetStdHandle PROTO NEAR32 stdcall,
    nStdHandle:DWORD


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,
    fHandle:DWORD

.DATA

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: '


.CODE
_start:
           INVOKE GetStdHandle,       ; handle for console output
             STD_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

EndWhileBegin:



           ;INVOKE GetStdHandle,       ; handle for console output
             ;STD_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

EndLoop:



           INVOKE  CloseHandle,       ; close file handle
             hFile

           INVOKE  ExitProcess, 0     ; exit with return code 0

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


troycastor5Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

grg99Commented:
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!



0
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?
0
mtmikeCommented:
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).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VernonNemitzCommented:
A bunch of stuff regarding Assembly and primes can be found here:

http://www.experts-exchange.com/Programming/Programming_Languages/Assembly/Q_20677156.html

0
terageekCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Assembly

From novice to tech pro — start learning today.