TASM程序不打印任何东西

问题描述 投票:2回答:2

我编写了一个程序来计算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
assembly x86-16 tasm
2个回答
1
投票
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="$"
  • 要将结果显示为字符,您仍需要添加“0”。这将从5转换为“5”。

该解决方案解决了所有这些问题

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

0
投票

idiv bxdx:ax中的32位值除以bx。因此,在划分之前,您需要将ax签名扩展到dx,您可以实现例如cwd。与'0'指令。

另一个问题是你需要在al之前将dl添加到int 21h/ah=02h(或ret)中的值,以便将其转换为字符。请注意,此方法仅适用于单位数值。


您可能还想将mov ax,4c00h / int 21h最后更改为qazxswpoi,这是退出DOS程序的正确方法。

© www.soinside.com 2019 - 2024. All rights reserved.