Thread: Prime numbers in asm

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    Prime numbers in asm

    a little background info. Im a complete and utter noob when it comes to programming. I am still in uni and through the semester ive learned enough to somewhat know what i am doing but am far from great like you guys

    tl;dr
    yea, first post of many.

    heres thee question:

    Write a procedure named IsPrime that sets the Zero flag if the 32-bit integer passed in the EAX register is prime. Optimize the program’s loop to run as efficiently as possible. Write a test program that prompts the user for an integer, calls IsPrime, and displays a message indicating whether or not the value is prime. Continue prompting the user for integers and calling IsPrime until the user enters -1.

    heres what i have so far:

    include 'irvine 32'

    org 100h ; set location counter to 100h

    jmp CodeStart

    DataStart:
    max dw " "
    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
    I am so lost so thanks for your patience

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Based on the code, you don't look lost.

    The code doesn't seem to match the person who posted the code. Red flag. Also, this should be in Tech.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Nice assembler code... why are you asking in a C forum?

    Oh wait... it's scoop and poop, isn't it?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    This is one of the older versions of that code. According to the OP there, it's DeVry's custom assembly. From other posts I've seen, it looks like the prof gives them this code, then they go modify it.

    @aestetics: Your best bet here is probably your course materials, notes, books, etc. I'm not familiar with that assembly language. But for starters, somewhere in the main loop you actually need to call IsPrime. You also need to call whatever function you use to get input, and check if it's -1, and if so exit.

    When I find myself coding assembly, I often write out what I want to do in pseudo code, then write some C code to do it, then translate to assembly. Since it's so easy to make mistakes in assembly and so hard to debug them, work in very small chunks, and test often.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's an impressive list of screw ups for a first post.

    Crappy title
    How To Ask Questions The Smart Way

    No code tags, wrong forum, some others

    Anyway, lazy mode, all I'm gonna do is fix the title and move to a better forum
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime numbers program
    By Prestige in forum C Programming
    Replies: 3
    Last Post: 10-16-2010, 01:57 PM
  2. prime numbers and time consuming
    By Gustaff in forum C Programming
    Replies: 21
    Last Post: 08-30-2010, 01:29 PM
  3. Recuration Prime Numbers
    By megazord in forum C Programming
    Replies: 17
    Last Post: 05-17-2010, 08:56 AM
  4. Perfect and Prime numbers
    By taggrath in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2009, 02:13 AM
  5. Replies: 18
    Last Post: 11-04-2005, 02:41 PM