PadhaikoAll subjects

Past exam question: 2082 Chaitra (Regular), Q7

Microprocessors · 5 marks

Open in the Padhaiko app

Topic: 8086 Programs: Arithmetic, Strings, I/O, Number Display

Question

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:

  1. Screen Clear: BIOS Video Interrupt INT 10H with AH = 00H and AL = 03H resets the video mode to standard 80x25 16-color text and clears the display buffer.
  2. String Input: DOS Interrupt INT 21H with AH = 0AH reads a buffered string from standard input.
    • Buffer structure: Byte 0 = Max buffer length, Byte 1 = Actual characters read, Byte 2+ = Character bytes.
  3. 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) using INT 21H, AH = 02H.
  4. Display String Length: Convert the binary length in Byte 1 to two ASCII decimal digits and print.

Complete 8086 Assembly Language Program:

8086 String Characters in Newline & Length8086 ASM
.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 MAIN

Execution Walkthrough:

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.

All past exam questions for Microprocessors