8086 Assembly Program to Display String ‘hello’

Displaying a string in assembly language is an essential task, often used in debugging or user interaction. This blog post explores an 8086 assembly language program that prints the string ‘hello’ to the console using DOS interrupt 21h.

data segment
msg1 db 'hello'
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov sp,0d000h
mov bx,offset msg1
mov cx,0005h
move:
mov ah,02h
mov dl,[bx]
int 21h
inc bx
dec cx
jnz move
mov ax,4c00h
int 21h
int 3
code ends
end start

Step-by-Step Explanation

1. Data Segment Initialization

  • msg1 db 'hello' stores the string ‘hello’ as a series of ASCII characters

2. Code Execution Flow

  • mov ax, data and mov ds, ax: Initializes the data segment by loading its address into AX and then into DS.
  • mov sp, 0d000h: Sets up the stack pointer.
  • mov bx, offset msg1: Points BX to the start of the string.
  • mov cx, 0005h: Sets the loop counter to 5 (length of ‘hello’).
  • Loop (move: label):
    • mov ah, 02h: Prepares for character output using DOS interrupt 21h.
    • mov dl, [bx]: Loads the current character from memory into DL.
    • int 21h: Calls the DOS interrupt to display the character.
    • inc bx: Moves to the next character.
    • dec cx: Decreases the counter.
    • jnz move: Jumps back if CX is not zero, continuing the loop.
  • mov ax,4c00h and int 21h: Terminates the program.
  • int 3: Generates a breakpoint interrupt.

Flowchart

Overall Process

  • The program initializes the necessary segments.
  • It loops through each character in ‘hello’ and prints it using DOS interrupt 21h.
  • The program successfully prints ‘hello’ on the console before terminating.

Output:

C:\TASM>masm an_hello.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987.  All rights reserved.
 
Object filename [an_hello.OBJ]:
Source listing  [NUL.LST]:
Cross-reference [NUL.CRF]:
 
  50318 + 450338 Bytes symbol space free
 
      0 Warning Errors
      0 Severe  Errors
 
C:\TASM>link an_hello.obj
 
Microsoft (R) Overlay Linker  Version 3.60
Copyright (C) Microsoft Corp 1983-1987.  All rights reserved.
 
Run File [AN_HELLO.EXE]:
List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment
 
C:\TASM>debug an_hello.exe
-g
hello
Program terminated normally
-q

Summary of Output

  • The string ‘hello’ is processed and displayed character by character.
  • The execution follows a structured loop, printing each character sequentially.
  • The final result is a proper termination with the string printed on screen.

One thought on “8086 Assembly Program to Display String ‘hello’”

  1. I don’t why this stuff has to be so difficult, Everyone writes different and difficult codes, I think I will never understand this. I think I have to start from very Basic.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.