分数负数MASM

时间:2016-04-19 01:35:58

标签: assembly x86 masm irvine32

我编写了一个程序,它总结了用户的一堆数字并返回平均值,但是当我尝试划分负数时,程序会卡住。我的印象是允许使用负数,所以我不确定这里的问题是什么。

INCLUDE Irvine32.inc

; Constants
min             =       -100
max             =       -1
one             =       1

.data
num             DWORD   ?
nums            DWORD   ?
nums_message    BYTE    "Total numbers: ",0
sum             DWORD   ?
sum_message     BYTE    "Sum: ",0
average         DWORD   ?
average_message BYTE    "Average: ",0
current_loop    DWORD   ?
spaces          BYTE    "     ",0
invalid_msg     BYTE    "Out of range, Number must be between [-100...-1]",0
temp            DWORD   ?

.code
main proc

    ; Enter numbers to be displayed
    mov     ecx, 0
    enter_num:
    mov     edx, OFFSET prompt_2
    call    WriteString
    call    ReadInt
    mov     num, eax

    ; Check if non-negative
    mov     eax, num
    mov     ebx, 0
    cmp     eax, ebx
    jge     average_nums
    inc     ecx

    ; Validate user input
    validation_1:
        mov     eax, num
        mov     ebx, min
        cmp     eax, ebx
        jge     validation_2

    ; If validation fails
    invalid:
        mov     edx, OFFSET invalid_msg
        call    WriteString
        call    CrLf
        jmp     enter_num

    validation_2:
        mov     eax, num
        mov     ebx, max
        cmp     eax, ebx
        jg      invalid

    ; Sum the numbers
    sum_nums:
        mov     eax, sum
        add     eax, num
        mov     sum, eax
        jmp     enter_num

    ; Calculate the average
    average_nums:
        ; Display total numbers
        mov     edx, OFFSET nums_message
        call    WriteString
        mov     eax, ecx
        call    WriteInt
        call    CrLf

        ; Display sum
        mov     edx, OFFSET sum_message
        call    WriteString
        mov     eax, sum
        call    WriteInt
        call    CrLf

        ; Display Average
        mov     edx, OFFSET average_message
        call    WriteString
        cdq
        mov     eax, sum
        mov     ebx, nums
        div     ebx
        call    WriteInt
        call    CrLf

    exit; ; Exit to operating system
main endp

; Insert additional procedures

end main

0 个答案:

没有答案