title: a 8086 assembly language program to calculate the average of any n sum of numbers
body: <p>How do I write an 8086 assembly language program to calculate the average of any <code>n</code> numbers? I wrote two programs. One uses the <code>mov ah,01 int 21h</code> input function and so it only accepts one number. For this reason I cannot input a number like 10.</p>
  
  <p>I read from a buffer in the other one. It can accept a lot of digits, but the problem is that it loops continuously.</p>
  
  <p>Here is the first program:</p>
  
  <pre><code>.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
  </code></pre>
  
  <p>and here is the second:</p>
  
  <pre><code>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
  </code></pre>