assembly - Dividing a negative number MASM -
i've written program that's sums bunch of numbers user , returns average, program gets stuck when try divide negative numbers. under impression dividing negative number allowed, i'm not sure problem here.
include irvine32.inc ; constants min = -100 max = -1 1 = 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 between [-100...-1]",0 temp dword ? .code main proc ; enter numbers 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 numbers sum_nums: mov eax, sum add eax, num mov sum, eax jmp enter_num ; calculate 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 operating system main endp ; insert additional procedures end main
Comments
Post a Comment