Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,812 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,194 people online right now. Registration is fast and FREE... Join Now!




8086 Assembly, printing out prime numbers. help?

2 Pages V  1 2 >  
Reply to this topicStart new topic

8086 Assembly, printing out prime numbers. help?

KelliB
11 Aug, 2008 - 01:35 PM
Post #1

New D.I.C Head
*

Joined: 19 Apr, 2008
Posts: 16


My Contributions
The teacher gave us this code to start out with:
CODE
; prime.asm
; sample program to demonstrate procedures
; calulates and prints prime numbers from 1 to 20

include 'emu8086.inc'

org  100h; set location counter to 100h

jmp CodeStart

DataStart:
   max dw 20
   space db " ", 0

CodeStart:
   mov bx, 1
  
   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

This code prints out 1-20, but it's suppose to print out the prime numbers.

The teacher also says: "Prior to calling IsPrime, put the value you want to test in the BX register. After the procedure returns the value of the DX register will tell you if the number is prime or not."


It seems so simple but I can't understand what I'm suppose to do. I've looked in the text for as much info as I could find but it doesn't help.

Can anyone help me?

This post has been edited by KelliB: 11 Aug, 2008 - 01:50 PM
User is offlineProfile CardPM
+Quote Post

jwwicks
RE: 8086 Assembly, Printing Out Prime Numbers. Help?
11 Aug, 2008 - 05:53 PM
Post #2

D.I.C Head
Group Icon

Joined: 31 Jul, 2008
Posts: 59



Thanked: 6 times
Dream Kudos: 200
My Contributions
Hello KelliB,

QUOTE(KelliB @ 11 Aug, 2008 - 02:35 PM) *

The teacher gave us this code to start out with:
This code prints out 1-20, but it's suppose to print out the prime numbers.

The teacher also says: "Prior to calling IsPrime, put the value you want to test in the BX register. After the procedure returns the value of the DX register will tell you if the number is prime or not."

It seems so simple but I can't understand what I'm suppose to do. I've looked in the text for as much info as I could find but it doesn't help.

Can anyone help me?


The code is just looping through the numbers from 1 to max which in this case is 20. The loop never calls the procedure isPrime. What you need to do is modify the section between :LoopStart and jle (JumpLessThanOrEqualTo) to call IsPrime. You'll need to do this before the call print_num. The value needs to be PUSHed into the BX register, it already is. Then call IsPrime. Then you'll need to CMP(compare) the DX register with 1. If DX is equal to 1 the number is prime so print it out otherwise continue the loop...

JW

This post has been edited by jwwicks: 11 Aug, 2008 - 05:55 PM
User is offlineProfile CardPM
+Quote Post

KelliB
RE: 8086 Assembly, Printing Out Prime Numbers. Help?
12 Aug, 2008 - 07:28 AM
Post #3

New D.I.C Head
*

Joined: 19 Apr, 2008
Posts: 16


My Contributions
Ugh.. That's so simple.. I can't believe I didn't realize it didn't call the procedure... Thanks

Well I've called IsPrime... but I still can't get it to print out the right numbers..
whatsthat.gif; I really suck at this.. but at least assembly isn't all that important for me to know..

This post has been edited by KelliB: 12 Aug, 2008 - 08:08 AM
User is offlineProfile CardPM
+Quote Post

KelliB
RE: 8086 Assembly, Printing Out Prime Numbers. Help?
15 Aug, 2008 - 09:51 AM
Post #4

New D.I.C Head
*

Joined: 19 Apr, 2008
Posts: 16


My Contributions
CODE
; prime.asm
; sample program to demonstrate procedures
; calulates and prints prime numbers from 1 to 20

include 'emu8086.inc'

org  100h; set location counter to 100h

jmp CodeStart

DataStart:
   max dw 20
   space db " ", 0

CodeStart:
   mov bx, 1
  
   LoopStart:
            
                    
       mov ax, bx
   ;call procedure IsPrime to test whether the number is
   ;prime or not
       call IsPrime
       cmp dx, 0
   ;if dx is equal to 0 jump
       je Equal
        
       mov ax, bx
       call print_num
      
   ; print a space
       mov si, offset space
       call print_string
      
    Equal:  
       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  
          
       ; increment count
           add cx, 1
              
       ; check remainder (in dx), if zero then we found a divisor
       ; and the number cannot be prime
           cmp dx, 1
          
       ;if dx is not equal to 1 restart loop
           jne LoopStart            
                    
        
        
       jmp PrimeLoop
      
       StopLabel:
      
       ret
   IsPrime ENDP
  
DEFINE_PRINT_STRING
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS


That's what my code looks like now but it's not printing out prime numbers.
It counts 1 2 3 and then seems to get stuck in a loop.
I don't know what to do...
User is offlineProfile CardPM
+Quote Post

RobinV
RE: 8086 Assembly, Printing Out Prime Numbers. Help?
19 Aug, 2008 - 06:08 AM
Post #5

New D.I.C Head
*

Joined: 21 May, 2008
Posts: 13


My Contributions
To be honest I cant be bothered to check your code.

But I like to debug it for you anyhow and tell you were the error is.
So, well I'd like it if you can Assemble that with whatever assembler you use so I can convert it back to Assembler using my Copy of IDA Free whatsthat.gif

Kind Regards,
Robin
User is offlineProfile CardPM
+Quote Post

KelliB
RE: 8086 Assembly, Printing Out Prime Numbers. Help?
19 Aug, 2008 - 08:12 AM
Post #6

New D.I.C Head
*

Joined: 19 Apr, 2008
Posts: 16


My Contributions
I don't know how to do that and I'm not told of any errors anyways.
User is offlineProfile CardPM
+Quote Post

RobinV
RE: 8086 Assembly, Printing Out Prime Numbers. Help?
19 Aug, 2008 - 08:13 AM
Post #7

New D.I.C Head
*

Joined: 21 May, 2008
Posts: 13


My Contributions
Ah, I thought "and then seems to get stuck in a loop." was the error. smile.gif
User is offlineProfile CardPM
+Quote Post

KelliB
RE: 8086 Assembly, Printing Out Prime Numbers. Help?
19 Aug, 2008 - 10:21 AM
Post #8

New D.I.C Head
*

Joined: 19 Apr, 2008
Posts: 16


My Contributions
Well I guess technically it is an error.. but what I meant is the complier doesn't tell me there's an error when it puts it all together.. I don't know.. I'm new to programming...
User is offlineProfile CardPM
+Quote Post

RobinV
RE: 8086 Assembly, Printing Out Prime Numbers. Help?
19 Aug, 2008 - 10:36 AM
Post #9

New D.I.C Head
*

Joined: 21 May, 2008
Posts: 13


My Contributions
QUOTE(KelliB @ 19 Aug, 2008 - 11:21 AM) *

Well I guess technically it is an error.. but what I meant is the complier doesn't tell me there's an error when it puts it all together.. I don't know.. I'm new to programming...



Compile it. And give the Compiled file to me.
Ill check were the Neverending loop happens for you then smile.gif
User is offlineProfile CardPM
+Quote Post

KelliB
RE: 8086 Assembly, Printing Out Prime Numbers. Help?
19 Aug, 2008 - 10:38 AM
Post #10

New D.I.C Head
*

Joined: 19 Apr, 2008
Posts: 16


My Contributions
I don't know how to send it to you.. Besides the compiler my school has me use is through a client site.. It's all stored virtually..
User is offlineProfile CardPM
+Quote Post

kapax
RE: 8086 Assembly, Printing Out Prime Numbers. Help?
19 Aug, 2008 - 12:45 PM
Post #11

New D.I.C Head
*

Joined: 2 Jul, 2008
Posts: 39



Thanked: 1 times
My Contributions
Nice to see some assembly in the forum.

Sorry, KelliB, it's late and I am not in mood to check your code, but i can give you advice. Try downloading TASM 5.0 (or MASM better). to make .OBJ file, use tasm.exe, and to link the prog, use tlink.exe. This way you will be able to make it work on your computer. And actually, since it is 16 bit assembly, it will run on virtual machine anyway because Windows is 32 bit.
User is offlineProfile CardPM
+Quote Post

KelliB
RE: 8086 Assembly, Printing Out Prime Numbers. Help?
19 Aug, 2008 - 03:28 PM
Post #12

New D.I.C Head
*

Joined: 19 Apr, 2008
Posts: 16


My Contributions
I barely know what you just said, but thank you for the advice.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 02:38PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month