I wrote a program for calculating the mean value of an array of integers in TASM, but the console won't display anything, even though the algorithm seems to work fine. Does anybody have a clue what the problem is?
DATA SEGMENT PARA PUBLIC 'DATA'
msg db "The average is:", "$"
sir db 1,2,3,4,5,6,7,8,9
lng db $-sir
DATA ENDS
CODE SEGMENT PARA PUBLIC 'CODE'
MAIN PROC FAR
ASSUME CS:CODE, DS:DATA
PUSH DS
XOR AX,AX
PUSH AX
MOV AX,DATA
MOV DS,AX ;initialization part stops here
mov cx, 9
mov ax, 0
mov bx, 0
sum:
add al, sir[bx] ;for each number we add it to al and increment the nr of
;repetions
inc bx
loop sum
idiv bx
MOV AH, 09H ;the printing part starts here, first with the text
LEA DX, msg
INT 21H
mov ah, 02h
mov dl, al ;and then with the value
int 21h
ret
MAIN ENDP
CODE ENDS
END MAIN
idiv bx
The word-sized division will divide DX:AX
by the value in the operand BX
. Your code did not setup DX
beforehand!
The easiest solution here is to use the byte-sized division idiv bl
that will divide AX
by the value in BL
leaving the quotient in AL
and the remainder in AH
.
The very small numbers in the array add up to 45. This will result in a quotient of 5 and a remainder of 0.
MOV AH, 09H ;the printing part starts here, first with the text LEA DX, msg INT 21H mov ah, 02h mov dl, al ;and then with the value int 21h
This part of the program has 2 problems.
AL
, it has been destroyed by the DOS system call that will leave with the value AL="$"
.This solution addresses all these issues:
idiv bl
push ax ;Save quotient in AL
lea dx, msg
mov ah, 09h
int 21h ;This destroys AL !!!
pop dx ;Get quotient back straight in DL
add dl, "0" ;Make it a character
mov ah, 02h
int 21h