MASM素数计划不断崩溃

时间:2014-11-08 18:56:06

标签: assembly masm

我正在尝试制作一个程序,询问用户他们想要看多少素数,然后显示出来。当我运行该程序时,它打印出2然后崩溃,我认为问题是主要程序,但我找不到。

;constants
    MIN=0
    MAX=201


.data

    prime       DWORD   ? ; number of prime numbers to view
    case        DWORD   1 ; holds number being checked to be prime
    count       DWORD   0 ; used to keep track of spacing
    userName    BYTE 33 DUP(0) ;string to be entered by user
    greet       BYTE    "Hello and welcome my name is The Sean Harrington. ",0
    prompt_1    BYTE    "What is your name? ",0
    prompt_2    BYTE    "Hello ",0
    prompt_3    BYTE    "How many prime numbers would you like to see?",0 
    prompt_4    BYTE    "Enter a number between 1 and 200. ",0
    prompt_5    BYTE    "You have entered an invalid input. ",0
    space       BYTE    "   ",0
    bye         BYTE    "Good Day ",0



.code
main PROC



;Introduction
    call    Intro ;calls intro procedure


;user data
    call    Userdata; calls user data entry procedure


;show primes
    call    priming

;goodbye
    call    theending




    exit    ; exit to operating system
main ENDP












;------------------------------------------------------
;Intro
;
;Greets user asks for their name and greets them by name
;Receives:Nothing
;Modifies; edx, ecx
;------------------------------------------------------
Intro PROC
    mov     edx,        OFFSET greet ;greets user
    call    WriteString
    call    CrLf

    mov     edx,        OFFSET prompt_1 ;asks for users name
    call    WriteString
    mov     edx, OFFSET userName
    mov     ecx, 32
    call    ReadString

    call    CrLf
    mov     edx,        OFFSET prompt_2 ;says hello to user by name
    call    WriteString
    mov     edx,        OFFSET userName
    call    WriteString
    ret
Intro ENDP










;------------------------------------------------------
;Userdata
;
;Asks user to enter how many numbers to viewed
;Receives:Nothing
;Modifies; eax
;------------------------------------------------------
Userdata PROC


;Get Userdata
    call    CrLf
    mov     edx,        OFFSET prompt_3 ;asks user how many prime numbers they would like to view
    call    WriteString
    call    CrLf
    mov     edx,        OFFSET prompt_4 ;asks user how many prime numbers they would like to view
    call    WriteString
    call    ReadInt
    call    valid
    mov     prime,eax
    ret
Userdata ENDP









;------------------------------------------------------
;valid
;
;veriefies uderdata input
;Receives:eax as number to be tested
;Modifies; none
;------------------------------------------------------
valid PROC
check:
    cmp     eax, MAX ;compares number entered to min and max
    JGE     invalid
    cmp     eax, MIN
    JLE     invalid
    ret






invalid:
    call    CrLf
    mov     edx,        OFFSET prompt_5 ;tells the user their unput is invalid
    call    WriteString
    call    CrLf
    mov     edx,        OFFSET prompt_3 ;asks user how many prime numbers they would like to view
    call    WriteString
    call    CrLf
    mov     edx,        OFFSET prompt_4 ;asks user how many prime numbers they would like to view
    call    WriteString
    call    ReadInt
    jmp     check
    ret

valid ENDP




;------------------------------------------------------
;priming
;
;prints out the number of prime numbers requested
;Receives:nothing
;Modifies; ecx,
;------------------------------------------------------

priming PROC

    mov     ecx, prime ;sets loop counter

primeloop:
    call    isprime
    loop    primeloop
    ret
priming ENDP


;------------------------------------------------------
;isprime
;
;goes through numbers and prints out next prime
;Receives:nothing
;Modifies; ecx,eax, edx
;------------------------------------------------------
isprime PROC
 testtime: ;resets prime test for new number
    inc     case
    mov     eax, case
    cmp     eax, 2
    je      show
    mov     ebx, case
    jmp     checker



checker: ; divides number by all smaller numbers to see if there is a remainder
    mov     eax, case
    dec     ebx
    div     ebx
    cmp     edx,0
    je      show
    cmp     ebx, 2
    je      testtime
    jmp     checker     





show: ;shows the next prime number

    mov     eax,case
    call    WriteDec
    mov     edx, OFFSET space
    call    WriteString
    inc     case

    inc     count ;used for tracking spacing
    cmp     count,10
    je      break
    jmp     ending

break: ;used if the fib number is multiple of 5 to put in a line break
    mov     count,0
    call    CrLf

    jmp     ending

ending:
    ret
isprime ENDP

;------------------------------------------------------
;theending
;
;says goodbye to user
;Receives:nothing
;Modifies; edx
;------------------------------------------------------

theending PROC
    mov     edx,    OFFSET bye ;says goodbye to user by name
    call    WriteString
    mov     edx,        OFFSET userName
    call    WriteString
    call    CrLf
    ret

theending ENDP




END main

0 个答案:

没有答案