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

# 8086 ALP: words on separate lines, centered

Microprocessor · Chapter 3: Programming with 8086 Microprocessor

- Asked in: 2072 Magh, 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-separate-lines-center
- Whole chapter: https://www.bctengineer.com/md/semester_4/microprocessor/chapter-3

## Question

Write a program in 8086 to read a string and display each word in a separate line in the center of the screen.

## Short answer

Count words to compute vertical start row (25-n)/2, center each word horizontally at column (80-length)/2, print each word on its own line. Example good morning nepal prints three centered lines starting row 11.

## Answer

**Assumptions:** 80 × 25 text screen. Each word is centred horizontally (column = (80 − length)/2) and the block of words is centred vertically (first row = (25 − number of words)/2). Words are separated by spaces.

**Method:** `COUNT_WORDS` first counts the words to find the starting row. Then each word is located (skip spaces, find its length), the cursor is positioned with INT 10h function 02h, and the word is printed character by character. The row is incremented for the next word.

```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: $'
    ROW   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

    CALL COUNT_WORDS       ; AL = number of words
    MOV  DH,0
    CMP  AL,25
    JAE  ROWSET            ; too many words -> start at row 0
    MOV  DH,25
    SUB  DH,AL
    SHR  DH,1              ; start row = (25 - n) / 2
ROWSET:
    MOV  ROW,DH
    CLRSCR
    LEA  SI,BUF+2
WLOOP:
SKIP:MOV AL,[SI]
    CMP  AL,' '
    JNE  CHK
    INC  SI                ; skip blanks
    JMP  SKIP
CHK:CMP  AL,0DH
    JE   FINISH            ; end of string
    MOV  DI,SI             ; find the length of this word
    XOR  CX,CX
LEN:MOV  AL,[DI]
    CMP  AL,' '
    JE   GOT
    CMP  AL,0DH
    JE   GOT
    INC  DI
    INC  CX
    JMP  LEN
GOT:MOV  DL,80
    SUB  DL,CL
    SHR  DL,1              ; column = (80 - length) / 2
    MOV  DH,ROW
    MOV  BH,0
    MOV  AH,02H            ; position cursor
    INT  10H
PR: MOV  DL,[SI]           ; print the word
    MOV  AH,02H
    INT  21H
    INC  SI
    LOOP PR
    INC  ROW               ; next word on next row
    JMP  WLOOP
FINISH:
    MOV  AH,02H            ; park cursor at bottom
    MOV  BH,0
    MOV  DX,1800H
    INT  10H
    MOV  AH,4CH            ; return to DOS
    INT  21H
MAIN ENDP

; ---- count words of the string in BUF; returns AL = number of words
COUNT_WORDS PROC
    LEA  SI,BUF+2
    XOR  BX,BX             ; BL = count, BH = 'inside a word' flag
CW1:MOV  AL,[SI]
    CMP  AL,0DH            ; Enter marks the end of the string
    JE   CWEND
    CMP  AL,' '
    JE   CWSP
    CMP  BH,0
    JNE  CWNXT             ; already inside a word
    INC  BL                ; a new word starts here
    MOV  BH,1
    JMP  CWNXT
CWSP:MOV BH,0
CWNXT:INC SI
    JMP  CW1
CWEND:MOV AL,BL
    RET
COUNT_WORDS ENDP

END MAIN
```

**Example:** `good morning nepal` → three lines `good`, `morning`, `nepal`, each centred, starting at row 11.

## Check yourself

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

1. How is the column for centring a word calculated on an 80-column screen?
   A. 80 x length
   B. (80 - length) / 2
   C. 80 / length
   D. (80 + length) / 2
   Answer: B. Subtracting the word length from 80 and halving leaves equal margins on both sides. (Key point: Column formula)

2. How is the first row chosen so the block of words is vertically centred?
   A. Row 0 always
   B. (25 + words) / 2
   C. (25 - number of words) / 2
   D. 25 - number of words
   Answer: C. COUNT_WORDS gives n, and the start row is (25 - n)/2; for 3 words this is row 11. (Key point: Starting row)

3. Which call places the cursor before a word is printed?
   A. INT 21h function 09h
   B. INT 10h function 02h
   C. INT 10h function 06h
   D. INT 16h function 00h
   Answer: B. INT 10h function 02h sets the cursor row (DH) and column (DL). (Key point: Cursor positioning)

4. Why must COUNT_WORDS run before printing?
   A. To clear the screen
   B. To read the string
   C. To convert letters to upper case
   D. The number of words is needed to calculate the vertical starting row
   Answer: D. The vertical centre depends on how many lines the words will occupy. (Key point: Why count words first)

## Related in Microprocessor

- [8086 ALP: words on lines and word count](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-words-lines-count) — Microprocessor, chapter 3
- [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: 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
- [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
