我编写了一个程序来计算TASM中整数数组的平均值,但是控制台不会显示任何内容,即使算法似乎工作正常。有人知道问题是什么吗?
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
字大小的除法将DX:AX
除以操作数BX
中的值。你的代码没有预先设置DX
!
这里最简单的解决方案是使用字节大小的分割idiv bl
,它将AX
除以BL
中的值,在AL
中留下商,其余部分在AH
中。
数组中的非常小的数字加起来为45.这将产生5的商和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
这部分程序有2个问题。
AL
的结果时,它已被DOS系统调用销毁,该调用将保留值AL="$"
。该解决方案解决了所有这些问题
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
idiv bx
将dx:ax
中的32位值除以bx
。因此,在划分之前,您需要将ax
签名扩展到dx
,您可以实现例如cwd
。与'0'
指令。
另一个问题是你需要在al
之前将dl
添加到int 21h/ah=02h
(或ret
)中的值,以便将其转换为字符。请注意,此方法仅适用于单位数值。
您可能还想将mov ax,4c00h / int 21h
最后更改为qazxswpoi,这是退出DOS程序的正确方法。