Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Other Programming > Assembly Language
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Assembly Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 24th, 2009, 05:22 PM
Registered User
Join Date: May 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Prime number program help

I need help with a program that finds and displays all of the prime numbers (eg, 1, 2, 3, 5, 7, 11, 13, 17, 19) from 1 to 20. I have most of the code, but am unsure where to go from here, the output displays all numbers from 1 to 20. Here is what I have so far:
include 'emu8086.inc'
org  100h ; set location counter to 100h
jmp CodeStart
   max dw 20
   space db " ", 0
   mov bx, 1
   call IsPrime  
   cmp dx, 0
       ; 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
   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
           ; 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
   IsPrime ENDP
  #2 (permalink)  
Old May 24th, 2009, 08:24 PM
Registered User
Join Date: May 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Nevermind, I was able to figure out the solution on my own.
  #3 (permalink)  
Old January 10th, 2010, 07:29 AM
Authorized User
Join Date: Jan 2010
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts

Originally Posted by daddymac1213 View Post
Nevermind, I was able to figure out the solution on my own.
Can you post the solution?

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation For Phone Number and Mobile Number dhruthi.ram99 Javascript How-To 12 October 30th, 2011 07:24 AM
Setup Project: Program not added in Start>Program arif_1947 VS.NET 2002/2003 2 March 31st, 2005 06:40 AM
prime numbers code error.. amahja56 C++ Programming 1 January 19th, 2005 01:33 PM
Prime Number Counting. Help!! ajm235 C++ Programming 3 August 27th, 2004 12:00 PM

All times are GMT -4. The time now is 08:49 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.