- 2072 Magh · 8 marks
8086 ALP: words on separate lines, centered
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.
Write a program in 8086 to read a string and display each word in a separate line in the center of the screen.
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.
.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.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- 8086 ALP: words on lines and word count · Microprocessor, chapter 3
- 8086 ALP: count vowels and display string · Microprocessor, chapter 3
- 8086 ALP: alternate case display · Microprocessor, chapter 3
- 8086 ALP: display only alphabetic characters · Microprocessor, chapter 3
- 8086 ALP: largest and smallest, display · Microprocessor, chapter 3
- 8086 ALP: count vowels, consonants, numerals · Microprocessor, chapter 3