---
title: "8086 ALP: words on lines and word count"
subject: "Microprocessor"
chapter: 3
key: "semester_4/microprocessor:alp-8086-words-lines-count"
source: https://www.bctengineer.com/q/semester_4/microprocessor/alp-8086-words-lines-count
site: BCT Engineering
---

# 8086 ALP: words on lines and word count

Microprocessor · Chapter 3: Programming with 8086 Microprocessor

- Asked in: 2070 Bhadra, 8 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/microprocessor/alp-8086-words-lines-count
- Whole chapter: https://www.bctengineer.com/md/semester_4/microprocessor/chapter-3

## Question

Write an assembly language program for 8086 to read a string. Display each word in separate lines in a cleared screen, count how many words are there and display the count.

## Short answer

Read string, clear screen, scan words, print each word followed by CRLF, increment counter, display count in decimal. Example I love Nepal prints three lines and Number of words = 3.

## Answer

**Method:** clear the screen after input, then scan the string. Blanks are skipped; each word is displayed character by character and followed by a new line (CR, LF), and the word counter `WCNT` is incremented. At the end the count is displayed in decimal using `PRINT_DEC`. The string ends with the Enter code 0DH.

```asm
.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
    MSG1  DB 'Enter a string: $'
    MSG2  DB 'Number of words = $'
    WCNT  DB 0
    BUF   DB 80, ?, 80 DUP(0)   ; DOS buffered-input area (max, actual, chars)
    CRLF  DB 13,10,'$'

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

    DISPSTR MSG1           ; prompt
    LEA  DX,BUF            ; read the string with DOS function 0Ah
    MOV  AH,0AH
    INT  21H

    CLRSCR
    LEA  SI,BUF+2
NEXT:
    MOV  AL,[SI]
    CMP  AL,0DH
    JE   DONE              ; end of string
    CMP  AL,' '
    JNE  WSTART
    INC  SI                ; skip blank
    JMP  NEXT
WSTART:                    ; a word starts here
    INC  WCNT              ; count it
PR: MOV  DL,[SI]
    INC  SI
    MOV  AH,02H
    INT  21H               ; display character
    MOV  AL,[SI]
    CMP  AL,' '
    JE   ENDW
    CMP  AL,0DH
    JNE  PR
ENDW:
    DISPSTR CRLF           ; word finished -> new line
    JMP  NEXT
DONE:
    DISPSTR MSG2
    MOV  AL,WCNT
    XOR  AH,AH
    CALL PRINT_DEC         ; display number of words
    MOV  AH,4CH            ; return to DOS
    INT  21H
MAIN ENDP

; ---- print the unsigned number in AX in decimal (registers preserved)
PRINT_DEC PROC
    PUSH AX
    PUSH BX
    PUSH CX
    PUSH DX
    MOV  BX,10
    XOR  CX,CX             ; digit counter
PD1:XOR  DX,DX
    DIV  BX                ; AX = quotient, DX = remainder (digit)
    PUSH DX
    INC  CX
    OR   AX,AX
    JNZ  PD1
PD2:POP  DX                ; digits come out in correct order
    ADD  DL,30H            ; to ASCII
    MOV  AH,02H
    INT  21H
    LOOP PD2
    POP  DX
    POP  CX
    POP  BX
    POP  AX
    RET
PRINT_DEC ENDP

END MAIN
```

**Example:** `I love Nepal` → lines `I`, `love`, `Nepal`, then `Number of words = 3`.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. When is the word counter WCNT incremented?
   A. For every character
   B. For every space
   C. When a new word starts after skipped blanks
   D. Only at the end of the string
   Answer: C. WCNT is incremented at WSTART, once for each word found after blanks are skipped. (Key point: Word counting)

2. How is each word shown on its own line?
   A. A CR,LF string is displayed after each word
   B. The cursor is set with INT 10h
   C. Words are stored in separate buffers
   D. A tab is printed between words
   Answer: A. After the word is printed, DISPSTR CRLF moves to the next line. (Key point: Separate lines)

3. How does the scan detect the end of the input?
   A. A '$' character
   B. A zero byte
   C. The count in BUF+1 reaching 0
   D. The Enter code 0DH
   Answer: D. Function 0Ah stores 0DH after the last typed character. (Key point: String end)

4. When is the screen cleared?
   A. After the string is read, before the words are displayed
   B. Before the prompt is shown
   C. After the count is displayed
   D. After each word
   Answer: A. The screen is cleared after input so only the words and the count appear. (Key point: Order of actions)

## Related in Microprocessor

- [8086 ALP: count vowels and display string](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-count-vowels-display) — Microprocessor, chapter 3
- [8086 ALP: words on separate lines, centered](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-words-separate-lines-center) — Microprocessor, chapter 3
- [8086 ALP: largest and smallest, display](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-largest-smallest-display) — Microprocessor, chapter 3
- [8086 ALP: count vowels, consonants, numerals](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-count-vowels-consonants-numerals) — Microprocessor, chapter 3
- [8086 ALP: alternate case display](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-alternate-case) — Microprocessor, chapter 3
- [8086 ALP: display only alphabetic characters](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-alphabetic-only) — Microprocessor, chapter 3

## Related in other subjects

- [ADC804 with 8255 control word and program](https://www.bctengineer.com/question.md?key=semester_5%2Finstrumentation_ii%3Aadc804-8255-program) — Instrumentation II, chapter 4
