Solved

prime number generator

Posted on 2003-11-16
7
895 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you have clients or friends from around the world, it becomes a challenge to arrange a meeting or effectively manage your time. This is where Outlook's capability to show 2 time zones in one calendar comes in handy.
This article outlines why you need to choose a backup solution that protects your entire environment – including your VMware ESXi and Microsoft Hyper-V virtualization hosts – not just your virtual machines.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 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