Past exam question: 2082 Chaitra (Regular), Q7
Open in the Padhaiko appQuestion
Write an assembly language program for 8086 that accepts string from the user and display each character in newline of a clear screen. Also display the total length of string.
Solution
Program Design & DOS/BIOS Interrupt Services:
- Screen Clear: BIOS Video Interrupt
INT 10HwithAH = 00HandAL = 03Hresets the video mode to standard 80x25 16-color text and clears the display buffer. - String Input: DOS Interrupt
INT 21HwithAH = 0AHreads a buffered string from standard input.- Buffer structure: Byte 0 = Max buffer length, Byte 1 = Actual characters read, Byte 2+ = Character bytes.
- Display Characters on Newlines:
- Loop through the buffer from index 0 to (Length - 1).
- Display character using
INT 21H, AH = 02H. - Print Carriage Return (
0DH) and Line Feed (0AH) usingINT 21H, AH = 02H.
- Display String Length: Convert the binary length in Byte 1 to two ASCII decimal digits and print.
Complete 8086 Assembly Language Program:
.MODEL SMALL
.STACK 100H
.DATA
PROMPT DB 'Enter a string: $'
LBL_LEN DB 'Total string length: $'
NEWLINE DB 0DH, 0AH, '$'
BUFFER DB 50 ; Max 50 characters
DB ? ; Actual length returned by DOS
DB 50 DUP(?) ; Character buffer
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
; 1. Clear screen using BIOS INT 10H
MOV AH, 00H
MOV AL, 03H
INT 10H
; 2. Display input prompt
LEA DX, PROMPT
MOV AH, 09H
INT 21H
; 3. Read string from keyboard
LEA DX, BUFFER
MOV AH, 0AH
INT 21H
; 4. Clear screen again for clean output
MOV AH, 00H
MOV AL, 03H
INT 10H
; 5. Print each character on a new line
MOV CL, BUFFER[1] ; CL = Actual character count
MOV CH, 00H ; CX = Loop counter
JCXZ SHOW_LEN ; If empty string, skip printing chars
LEA SI, BUFFER[2] ; SI points to first character
PRINT_LOOP:
MOV DL, [SI] ; Load character
MOV AH, 02H ; DOS display character service
INT 21H
; Print Carriage Return and Line Feed
MOV DL, 0DH
MOV AH, 02H
INT 21H
MOV DL, 0AH
MOV AH, 02H
INT 21H
INC SI ; Advance to next character
LOOP PRINT_LOOP
SHOW_LEN:
; 6. Display total length message
LEA DX, LBL_LEN
MOV AH, 09H
INT 21H
; 7. Convert binary length to 2 decimal digits and display
MOV AL, BUFFER[1] ; AL = length
MOV AH, 00H
MOV BL, 10
DIV BL ; AL = Quotient (tens), AH = Remainder (units)
ADD AX, 3030H ; Convert both digits to ASCII
MOV BX, AX ; Save in BX
MOV DL, BL ; Display tens digit
MOV AH, 02H
INT 21H
MOV DL, BH ; Display units digit
MOV AH, 02H
INT 21H
; 8. Terminate program
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAINExecution Walkthrough:
- If user enters
"Institute of Engineering",BUFFER[1]is set to 3. - The program clears the screen, outputs
'I'followed by CR+LF,'O'followed by CR+LF, and'E'followed by CR+LF. - It then prints
"Total string length: 03".
Answer:
The program uses BIOS INT 10H (AH=00H) to clear the screen, DOS INT 21H (AH=0AH) to read the string into a buffer, iterates through the characters displaying each followed by CR/LF (0DH/0AH), and converts the length byte to ASCII decimal for display.