Solved

prime number generator

Posted on 2003-11-16
7
886 Views
Last Modified: 2010-07-27
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


0
Comment
Question by:troycastor5
7 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 9761088
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
 

Author Comment

by:troycastor5
ID: 9761308
i used si but i still cant get the code to work..

could i use the stack to implement this?if yes,how?
0
 
LVL 5

Accepted Solution

by:
mtmike earned 250 total points
ID: 9763157
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
 

Expert Comment

by:VernonNemitz
ID: 9825609
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
 
LVL 3

Expert Comment

by:terageek
ID: 9879064
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
In  today’s increasingly digital world, managed service providers (MSPs) fight for their customers’ attention, looking for ways to make them stay and purchase more services. One way to encourage that behavior is to develop a dependable brand of prod…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now