Prime numbers in asm

This is a discussion on Prime numbers in asm within the Tech Board forums, part of the Community Boards category; a little background info. Im a complete and utter noob when it comes to programming. I am still in uni ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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,676
    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
    32,854
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 02:57 PM
  2. prime numbers and time consuming
    By Gustaff in forum C Programming
    Replies: 21
    Last Post: 08-30-2010, 02:29 PM
  3. Recuration Prime Numbers
    By megazord in forum C Programming
    Replies: 17
    Last Post: 05-17-2010, 09:56 AM
  4. Perfect and Prime numbers
    By taggrath in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2009, 03:13 AM
  5. Replies: 18
    Last Post: 11-04-2005, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21