Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly TASM程序不打印任何内容_Assembly_X86 16_Tasm - Fatal编程技术网

Assembly TASM程序不打印任何内容

Assembly TASM程序不打印任何内容,assembly,x86-16,tasm,Assembly,X86 16,Tasm,我在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

我在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
中的32位值除以
bx
。因此,在除法之前,您需要签名将
ax
扩展为
dx
,这可以通过
cwd
指令实现

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


您可能还希望将末尾的
ret
更改为
mov ax,4c00h/int 21h
,这是退出DOS程序的正确方法

字长除法将用操作数中的值除以
DX:AX
。您的代码没有预先设置
DX

这里最简单的解决方案是使用字节大小的除法
idiv bl
,它将
AX
除以
bl
中的值,将商保留在
AL
中,剩余部分保留在
AH

数组中非常小的数字加起来就是45。这将导致商为5,余数为0


程序的这一部分有两个问题

  • 当您想要使用来自
    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

你是怎么运作的?此外,您试图打印
al
值的方式也不起作用
int 21h/ah=02h
打印单个字符。如果你想打印一个整数,你必须将整数转换成一个字符串,用例如
int 21h/ah=09h
打印。我正在用TASM编译代码,然后运行可执行文件,我希望答案会出现在控制台上。我明白你的意思,但是,因为在我的情况下,答案只是一个数字,我认为它仍然可以完成任务。但是,第一个出现的字符串也没有打印出来,在我运行它之后控制台会冻结…@mihaita1205因为您的字大小除法在
DX
中对一个随机值进行操作,所以您遇到了除法异常(结果无法放入16位寄存器)。这就是为什么什么都没印出来。
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
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