;AVERAGE OF DYNAMICALLY ANY SINGLE, DOUBLE, TRIPLE DIGIT NUMBERS USING LOOP AND DISPLAY SUM IN ASSEMBLY ORG 100H .MODEL SMALL .STACK 100H .DATA RANGE DB 13,10, 'ENTER RANGE: \$' R DW ? EN DB 13,10,'ENTER A NUMBER: \$' N DW ? UN DB 13,10, 'SUM = \$' SUM DW 0 AV DB 13,10, 'AVERAGE = \$' AVG DW ? .CODE MAIN PROC MOV AX,@DATA MOV DS,AX MOV DX,OFFSET RANGE MOV AH,9 INT 21H MOV AH,1 INT 21H MOV AH,0 MOV R,AX SUB R,48 MOV CX,R L: PUSH CX MOV DX,OFFSET EN MOV AH,9 ;FOR PRINTING STRING INT 21H ;INPUT CODE STARTS HERE MOV N,0 ;N = 0 MOV BL,10 ;BL = 10 INPUT: ;LOOP LABEL MOV AH,1 ;INPUT A CHARACTER => STORES IN AL INT 21H ;INTERUPT FOR TEXT CMP AL,13 ;COMPARE INPUT WITH ENTER KEY JE NEXT ;IF JUMP EQUAL TO THEN ITS JUMP ON LABEL SUB AL,30H ;SUB AL = AL - 48 = AL - 30H MOV AH,0 ;AH = 0 MOV CX,AX ;CX = AX MOV AX,N ;AX = N MUL BL ;MULTIPLY => AX = AX * BL ADD AX,CX ;ADD => AX = AX + CX MOV N,AX ;N = AX JMP INPUT ;INPUT CODE ENDS HERE NEXT: MOV BX,N ;BX = N ADD SUM,BX ;SUM = SUM + BX POP CX LOOP L MOV DX,0 MOV AX,SUM MOV BX,R DIV BX ;AX = AX / BX = SUM / R = 400 / 4 = 100 MOV AVG,AX LEA DX,UN ;LOAD EFFECTIVE ADDRESS MOV AH,9 INT 21H ;OUTPUT CODE STARTS HERE MOV AX,SUM ;AX = SUM = 25 MOV DX,0 ;DX = 0 MOV BX,10 ;BX = 10 MOV CX,0 ;CX = 0 => COUNTER REGISTER L1: DIV BX ;DIVIDE => AX = AX / BX ;IN CASE OF 8 BIT REGISTER => AL = QUOTIENT, AH = REMAINDER ;IN CASE OF 16 BIT REGISTER => AX = QUOTIENT, DX = REMAINDER ;AX = 2 , DX = 5 PUSH DX ;5 SAVE IN STACK MOV DX,0 ;DX = 0 MOV AH,0 ;AX AH = 00000000, AL = QUOTIENT INC CX ;CX = CX + 1 CMP AX,0 ;(2 == 0) JNE L1 ;JUMP NOT EQUAL => AX != 0 MOV AH,2 ;OUTPUT/PRINT A SINGLE CHARACTER L2: POP DX ;FIRST TIME POP 2 AND SECOND TIME POP 5 = 25 ADD DX,48 INT 21H LOOP L2 ;OUTPUT CODE ENDS HERE ;PRINT AVERAGE MOV DX,0 LEA DX,AV ;LOAD EFFECTIVE ADDRESS MOV AH,9 INT 21H ;OUTPUT CODE STARTS HERE MOV AX,AVG ;AX = AVG MOV DX,0 ;DX = 0 MOV BX,10 ;BX = 10 MOV CX,0 ;CX = 0 => COUNTER REGISTER LL1: DIV BX ;DIVIDE => AX = AX / BX ;IN CASE OF 8 BIT REGISTER => AL = QUOTIENT, AH = REMAINDER ;IN CASE OF 16 BIT REGISTER => AX = QUOTIENT, DX = REMAINDER PUSH DX MOV DX,0 ;DX = 0 MOV AH,0 ;AX AH = 00000000, AL = QUOTIENT INC CX ;CX = CX + 1 CMP AX,0 ;(2 == 0) JNE LL1 ;JUMP NOT EQUAL => AX != 0 MOV AH,2 ;OUTPUT/PRINT A SINGLE CHARACTER LL2: POP DX ADD DX,48 INT 21H LOOP LL2 ;OUTPUT CODE ENDS HERE RET MAIN ENDP END MAINP