As the title suggests, I have trouble dividing in assembly. The user inputs 4 grades and the program suppose to get an average. The program will print the correct result if the sum of the four grades happen to be divisible by 4. It will output a big number when it is not. The code uses C for it's output. How do I fix this error? Here is the code.
.586
.MODEL FLAT
INCLUDE io.h ; header file for input/output
.STACK 4096
.DATA
g1 DWORD ?
g2 DWORD ?
g3 DWORD ?
g4 DWORD ?
jj BYTE 4
prompt1 BYTE "Enter grade 1", 0
prompt2 BYTE "Enter grade 2", 0
prompt3 BYTE "Enter grade 3", 0
prompt4 BYTE "Enter grade 4", 0
string BYTE 40 DUP (?)
resultLbl BYTE "The average is", 0
sum BYTE 11 DUP (?), 0
sum2 BYTE 11 DUP (?), 0
.CODE
MainProc PROC
input prompt1, string, 40 ; read ASCII characters
atod string ; convert to integer
mov g1, eax ; store in memory
input prompt2, string, 40 ; repeat for second number
atod string
mov g2, eax
input prompt3, string, 40 ;Repeat for third character
atod string
mov g3, eax
input prompt4, string, 40 ;Repeat for third character
atod string
mov g4, eax
mov eax, g1
add eax, g2
add eax, g3
add eax, g4
div jj
dtoa sum, eax ; convert to ASCII characters
output resultLbl, sum ; output label and sum
mov eax, 0 ; exit with return code 0
ret
_MainProc ENDP
END ; end of source code