如何编写 8086 汇编语言程序来计算任何n
数字的平均值?我写了两个程序。一个使用mov ah,01 int 21h
输入函数,因此它只接受一个数字。出于这个原因,我不能输入像 10 这样的数字。
我从另一个缓冲区中读取。它可以接受很多数字,但问题是它不断循环。
这是第一个程序:
.MODEL SMALL
.DATA
VAL1 DB ?
NL1 DB 0AH,0DH,'ENTER HOW MANY NO U WANT:','$'
NL2 DB 0AH,0DH,'ENTER NO:','$'
NL3 DB 0AH,0DH,'AVEARGE:','$'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA DX,NL1
MOV AH,09H
INT 21H
MOV AH,01H
INT 21H
SUB AL,30H
MOV CL,AL
MOV BL,AL
MOV AL,00
MOV VAL1,AL
LBL1:
LEA DX,NL2
MOV AH,09H
INT 21H
MOV AH,01H
INT 21H
SUB AL,30H
ADD AL,VAL1
MOV VAL1,AL
LOOP LBL1
LBL2:
LEA DX,NL3
MOV AH,09H
INT 21H
MOV AX,00
MOV AL,VAL1
DIV BL
ADD AX,3030H
MOV DX,AX
MOV AH,02H
INT 21H
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
这是第二个:
TITLE "THIS PROGRAM CALCULATES ANY N NUMBER OF STUDENT AVERAGE SCORES"
.MODEL SMALL
.DATA
VAL1 DB ?
DISPLAY1 DB 0AH,0DH,'HOW MANY NUMBER OF STUDENT SCORES DO YOU WANT TO INPUT? :','$'
DISPLAY2 DB 0AH,0DH,'ENTER NO:','$'
DISPLAY3 DB 0AH,0DH,'AVEARGE:','$'
BUFFER DB 3,4 DUP(?)
.CODE
MAIN PROC
.STARTUP
LEA DX,DISPLAY1 ; loads the message in the variable display 1
MOV AH,09H ; interrupt function to display the message
INT 21H
MOV AH,0AH ; Read into buffer
INT 21H
SUB AL,30H
MOV CL,AL
MOV BL,AL ; moves the content of al to bl register
MOV AL,00 ; sets the value of al to zero, the value of al is now in bl register
MOV VAL1,AL ; stores al in val1 NB: al is still zero.
LBL1:
LEA DX,DISPLAY2 ; displays message in display 2
MOV AH,09H
INT 21H
MOV AH,0AH ;Read into buffer
LEA DX,BUFFER
INT 21H
SUB AL,30H
ADD AL,VAL1 ; add val1 to al i.e, it now adds the previous value to the new one.
MOV VAL1,AL ; saves the added value to val 1.
LOOP LBL1 ; continues too add.
LBL2:
LEA DX,DISPLAY3 ; displays character in display 3
MOV AH,09H
INT 21H
MOV AX,00 ; sets ax value back to 00
MOV AL,VAL1 ; moves the total value back to al
DIV BL ; divides the total value with the inputed number of values tht was stored in bl
ADD AX,3030H ; convert to ASCII
MOV DX,AX ; now moves the content of ax register to data register
MOV AH,09H ; this displays the result
INT 21H
.EXIT
MAIN ENDP
END MAIN