Skip to main content

8086 ALP: words on lines and word 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.

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.

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.

.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.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Microprocessor chapter 3 questions →