Skip to main content

8086 ALP: sum of 100 even numbers

Short answer

Sum the first 100 even numbers (2+4+...+200) in a loop using DX as the current even, CX as counter, AX accumulating sum. The result 10100 is converted to decimal by repeated division by 10, pushing remainders, then popping and displaying as ASCII digits. Output: 10100.

Write an assembly language program for 8086 to calculate the addition of 100 natural even numbers and display the result in screen, in decimal format.

The first 100 even natural numbers are 2, 4, 6, ..., 200. Their sum = 100 × 101 = 10100, which fits in a 16-bit register. The program adds them in a loop (DX holds the current even number), then converts AX to decimal by repeated division by 10 (remainders are pushed on the stack and popped in reverse order) and displays the digits.

.MODEL SMALL
.STACK 100H

DISPSTR MACRO MSG            ; display a '$'-terminated string
        LEA  DX,MSG
        MOV  AH,09H
        INT  21H
        ENDM

CLRSCR  MACRO                ; clear screen and home the cursor
        MOV  AX,0600H        ; scroll whole window (AL=0 -> blank it)
        MOV  BH,07H          ; normal attribute
        MOV  CX,0000H        ; upper-left corner (row 0, col 0)
        MOV  DX,184FH        ; lower-right corner (row 24, col 79)
        INT  10H
        MOV  AH,02H          ; set cursor position
        MOV  BH,00H          ; page 0
        MOV  DX,0000H        ; row 0, col 0
        INT  10H
        ENDM

.DATA
    MSG   DB 'Sum of first 100 even numbers = $'

.CODE
MAIN PROC
    MOV  AX,@DATA
    MOV  DS,AX

    XOR  AX,AX             ; AX = sum = 0
    MOV  DX,2              ; first even number
    MOV  CX,100            ; 100 numbers
ADDLP:
    ADD  AX,DX             ; sum = sum + even number
    ADD  DX,2              ; next even number
    LOOP ADDLP             ; repeat 100 times, AX = 10100

    PUSH AX                ; save the sum
    DISPSTR MSG
    POP  AX
    MOV  BX,10             ; divisor
    XOR  CX,CX             ; digit counter
DIVLP:
    XOR  DX,DX             ; DX:AX / 10
    DIV  BX                ; AX = quotient, DX = remainder
    PUSH DX                ; save digit
    INC  CX
    OR   AX,AX             ; quotient zero?
    JNZ  DIVLP
SHOWLP:
    POP  DX                ; most significant digit first
    ADD  DL,30H            ; convert to ASCII
    MOV  AH,02H
    INT  21H               ; display digit
    LOOP SHOWLP
    MOV  AH,4CH            ; return to DOS
    INT  21H
MAIN ENDP

END MAIN

Output: Sum of first 100 even numbers = 10100

✓ Answer checked by Claude Opus 5.5 (high effort)

All Microprocessor chapter 3 questions →