Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

prime number generator

Posted on 2003-11-16
7
Medium Priority
?
930 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What monsters are hiding in your child's room? In this article I will share with you a tech horror story that could happen to anyone, along with some tips on how you can prevent it from happening to you.
Let's take a look into the basics of ransomware—how it spreads, how it can hurt us, and why a disaster recovery plan is important.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 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