Current location: Hot Scripts Forums » Programming Languages » Other Languages » Assembly language help, prime number program

Assembly language help, prime number program

Reply
  #1 (permalink)  
Old
New Member
 
Join Date: May 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Angry Assembly language help, prime number program

I need help with a program to find and print\display all prime numbers between 1 and 20. The output should be 1, 2, 3, 5, 7, 11, 13, 17, 19. The output that I am currently getting displays all of the numbers between 1 and 20. Here is the code that I have so far:

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

Any help would be greatly appreciated.
Reply With Quote
  #2 (permalink)  
Old
New Member
 
Join Date: May 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Nevermind, I was able to figure out the solution to my problem.
Reply With Quote
  #3 (permalink)  
Old
Nico's Avatar
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,130
Thanks: 12
Thanked 95 Times in 90 Posts
Can you post the solution? In case someone else has the same problem.
Reply With Quote
  #4 (permalink)  
Old
New Member
 
Join Date: Aug 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
can you define print_num and print_string for me please im new to this one
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to build a binary tree in assembly language? phingphing Other Languages 0 11-04-08 07:15 AM
what is the assembly program of multiplication table? theakrista Other Languages 1 10-01-08 08:34 AM
Asembly Language Help needed..Urgent..plzzzz paritoshcool Other Languages 1 11-27-07 07:07 AM


All times are GMT -5. The time now is 12:45 AM.
vBulletin® Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.