Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

prime # assembly language program

Name: Anonymous 2007-08-06 0:52 ID:bXqn4xmn

I am learning assembly language and need some help in figureing out how to get this program to display only prime numbers from 2 to 19. please help /prog/

; 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 19
   space db " ", 0

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

Name: Anonymous 2007-08-06 15:32 ID:JcN7d5Nm


;CALCULATE AND PRINT PRIMES 1-20
FACTORS .DB 0
DIVDEND .DB 0
NUMBER  .DB 0
INTMAIN LDY #20
NEXT    TYA
        PHA ;save .A
        JSR ISPRIME ;sets Z if prime
        PLA ;restore .A
        BNE AINT
        ;[.A here will have a prime number]
        ;[Depending on architecture, insert print routine here]
        ;[Note, convert to ASCII (or unicore) first
        NOP
AINT    ;[if you want do something for non primes, put it here]
        DEY
        BPL NEXT
        RTS
ISPRIME ;number to be tested in A
        LDA #0
        STA FACTORS
        TAX
        DEX ;don't bother dividing by itself
        STA NUMBER ;save it because DIVTEST destroys .A
NEXT    LDA NUMBER
        JSR DIVTEST
        BNE ISNT
        INC FACTORS
ISNT    DEX
        CMP #1 ;don't bother dividing by 1
        BNE NEXT
        LDA FACTORS
        CMP #1
        RTS
DIVTEST ;divide A by X, set Z if even
        ;division by 0 results in infinite loop
        ;.A is destroyed, so save it if you need it
        STA DIVDEND
        TXA
DIVLOOP SBC DIVDEND
        BPL DIVLOOP
        LDA DIVDEND
        RTS

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List