General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 24th, 2009, 05:30 PM
daddymac1213 daddymac1213 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 2 daddymac1213 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m
Reputation Power: 0
Assembly language help, prime number program

I need help with this program that I am writing. I need to write a program that finds and displays all prime numbers (eg., 1, 2, 3, 5, 7, 11, 13, 17, 19) between 1 and 20. The problem that I am having is that it outputs all numbers between 1 and 20.
Code:
include 'emu8086.inc'

org  100h ; set location counter to 100h

jmp CodeStart

DataStart:
   max dw 20
   space db " ", 0

CodeStart:
   mov bx, 1
   
   call IsPrime 
   
   cmp dx, 0

   
   LoopStart:   
   
       ; must be a prime
       mov ax, bx
       call print_num
       
       ; print a space
       mov si, offset space
       call print_string
       
       add bx, 1
       cmp bx, max

   jle LoopStart
   
   ret
       
    
   IsPrime PROC
       ; uses a loop to determine if number in bx is prime
       ; upon return if bx not prime dx will be 0, otherwise dx > 0

       ; we only have to test divisors from 2 to bx/2
       
       ; prepare to divide dx:ax / 2
       mov ax, bx         
       mov dx, 0 
       mov cx, 2  
       div cx
       
       ; move result into si for loop
       mov si, ax
       
       ; assume the value is prime
       mov dx, 1
       
       ; start loop at 2
       mov cx, 2
       
       PrimeLoop:
       
           ; compare loop count(in cx) and max loop value (in si)
           cmp cx, si
           
           ; jump out of loop if count(cx) > si
           ja StopLabel
       
           ; divide test value (in bx) by loop count (in cx)
           mov ax, bx
           mov dx, 0            
           div cx
           
           ; check remainder (in dx), if zero then we found a divisor
           ; and the number cannot be prime
           cmp dx, 0
           
           ; if dx = 0 then we found a divisor and can stop looking
           je StopLabel
           
           ; increment count
           add cx, 1
       
       jmp PrimeLoop
       
       StopLabel:
       
       ret
   IsPrime ENDP
   
DEFINE_PRINT_STRING
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS

Reply With Quote
  #2  
Old May 24th, 2009, 07:23 PM
daddymac1213 daddymac1213 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 2 daddymac1213 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m
Reputation Power: 0
Smile I figured out the program on my own.

Nevermind, I was able to figure out the solution on my own.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Assembly language help, prime number program


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap