Lesson 29 of 48 intermediate

emu8086/MASM Setup and Syntax Basics

Get your assembly toolkit running — write, assemble, and debug 8086 programs on a modern PC

Open interactive version (quiz + challenge)

Real-world analogy

emu8086 is like a flight simulator for microprocessors. A real 8086 chip is expensive and hard to wire up, just like a real airplane. The simulator gives you all the instruments (registers, flags, memory view) on screen, lets you practice (write and run code), and even lets you crash safely (bugs do not fry hardware). MASM is the control tower that translates your flight plan (source code) into actual flight commands (machine code).

What is it?

emu8086 is a free integrated development environment that emulates the 8086 microprocessor, letting you write, assemble, and debug assembly programs on a modern PC. MASM (Microsoft Macro Assembler) syntax is the Intel-style assembly language it uses, featuring simplified segment directives (.MODEL, .STACK, .DATA, .CODE) that organize programs into logical sections. Together, they provide a complete hands-on platform for learning 8086 assembly.

Real-world relevance

Every microprocessor course in universities uses either emu8086, TASM (Turbo Assembler), or DOSBox+MASM for lab work. Industry professionals use the same MASM syntax when writing inline assembly in C/C++ for performance-critical code, boot loaders, or driver ISRs. Understanding the toolchain is essential for debugging embedded systems and reverse engineering.

Key points

Code example

; Complete emu8086 EXE template
; This is the standard skeleton for every program
.MODEL SMALL
.STACK 100h

.DATA
  greeting DB 'Welcome to emu8086!', 0Dh, 0Ah, '$'
  name_buf DB 20 DUP('$')
  prompt   DB 'Enter your name: $'

.CODE
MAIN PROC
  ; Step 1: Initialize data segment
  MOV AX, @DATA
  MOV DS, AX

  ; Step 2: Print the greeting
  MOV AH, 09h
  LEA DX, greeting
  INT 21h

  ; Step 3: Print prompt
  MOV AH, 09h
  LEA DX, prompt
  INT 21h

  ; Step 4: Read a string (simplified — read chars)
  LEA SI, name_buf
  MOV CX, 19        ; max 19 characters
read_loop:
  MOV AH, 01h       ; DOS: read char with echo
  INT 21h
  CMP AL, 0Dh       ; Enter key?
  JE  done_read
  MOV [SI], AL
  INC SI
  LOOP read_loop

done_read:
  ; Step 5: New line
  MOV AH, 02h
  MOV DL, 0Dh
  INT 21h
  MOV DL, 0Ah
  INT 21h

  ; Step 6: Print back the name
  MOV AH, 09h
  LEA DX, name_buf
  INT 21h

  ; Step 7: Exit to DOS
  MOV AH, 4Ch
  INT 21h
MAIN ENDP
END MAIN

Line-by-line walkthrough

  1. 1. .MODEL SMALL / .STACK 100h — tell the assembler we want a small-model EXE with a 256-byte stack
  2. 2. .DATA section — define three strings: a greeting, an input buffer (20 bytes filled with '$' as terminator), and a prompt
  3. 3. MOV AX, @DATA / MOV DS, AX — essential setup: the CPU does not automatically set DS, so we manually point it to our data segment
  4. 4. MOV AH, 09h / LEA DX, greeting / INT 21h — DOS function 09h prints a '$'-terminated string at DS:DX
  5. 5. The read_loop uses DOS function 01h to read one character at a time, storing each in the buffer and advancing SI
  6. 6. CMP AL, 0Dh / JE done_read — when the user presses Enter (carriage return = 0Dh), we stop reading
  7. 7. After reading, we print CR+LF (0Dh, 0Ah) to move to a new line on the screen
  8. 8. MOV AH, 09h / LEA DX, name_buf — print back the name. It works because we prefilled the buffer with '$' terminators
  9. 9. MOV AH, 4Ch / INT 21h — standard DOS exit. Function 4Ch terminates the program and returns control to the DOS prompt

Spot the bug

.MODEL SMALL
.STACK 100h
.DATA
  msg DB 'Test$'
.CODE
MAIN PROC
  MOV AH, 09h
  LEA DX, msg
  INT 21h
  MOV AH, 4Ch
  INT 21h
MAIN ENDP
END MAIN
Need a hint?
The program compiles fine but prints garbage instead of 'Test'. What is missing before accessing data?
Show answer
DS is never initialized. Without MOV AX, @DATA / MOV DS, AX, the DS register points to whatever default value DOS set (usually the PSP), not the .DATA segment. LEA DX, msg calculates the offset correctly, but DS:DX points to the wrong segment. Add the two-line DS initialization at the start of MAIN PROC.

Explain like I'm 5

Imagine you want to learn to drive a race car. Instead of buying a real car (an actual 8086 chip and circuit board), you use a video game racing simulator (emu8086). The simulator has a steering wheel, pedals, and a dashboard (registers, memory, flags) that work just like the real thing. You can practice, crash, and try again without any risk. The instruction manual that tells you the rules of the road is MASM syntax.

Fun fact

MASM has been around since 1981 — it was one of the first products Microsoft sold alongside MS-DOS. Despite being over 40 years old, Microsoft still ships a version of MASM (ml64.exe) with every copy of Visual Studio. The MASM syntax and directives you learn today work almost identically in modern x64 assembly programming.

Hands-on challenge

Set up emu8086 on your computer (or use an online 8086 emulator). Write a .COM program (using ORG 100h) that prints your name and the current lesson number. Then convert it to an .EXE program (using .MODEL SMALL) and verify both produce the same output.

More resources

Open interactive version (quiz + challenge) ← Back to course: Microprocessor A–Z