86
MPMC Lab · Experiment 1 · 8086 · DOSBox

8086 Programming Environment and Basic Arithmetic

Set up DOSBox, MASM, LINK and DEBUG, then add two 16-bit numbers held in registers.

☰ All experiments

Aim

To install and configure the DOSBox, MASM, LINK and DEBUG tools to edit, assemble, debug and execute 8086 assembly language programs, and to write and execute a program that adds two 16-bit numbers stored in registers.

Learning objectives

1
Tool chain
Explain the roles of EDIT, MASM, LINK and DEBUG and the files each produces (.ASM → .OBJ → .EXE).
2
DOSBox setup
Mount a host folder as drive C: and run the tools from the DOS prompt.
3
Program skeleton
Write a correct .MODEL SMALL program with .STACK, .DATA, .CODE, a PROC and a DOS exit.
4
16-bit addition
Use ADD and ADC to add two 16-bit numbers and capture the carry.
5
DEBUG
Use R, T, P, G, D and U to inspect registers, flags and memory.
6
Flags
Interpret CF, ZF, SF, OF, AF and PF after an addition.

What this experiment demonstrates

  • How an assembly source file becomes an executable through assembling and linking.
  • How the 8086 adds 16-bit values in one instruction, and how the carry out of bit 15 is captured with ADC.
  • How a debugger exposes the CPU state after every instruction.

Sub-tasks

  • 1.1 Set up the environment (Hello program)
  • 1.2 Add two 16-bit numbers in registers

8086 at a glance

The Intel 8086 is a 16-bit microprocessor with a 16-bit data bus and a 20-bit address bus, so it can address 1 MB. Internally it has two units that work in parallel. The BIU fetches instructions into a 6-byte queue and forms addresses. The EU decodes and executes them.

BIU · Bus Interface Unit EU · Execution Unit Segment regsCS DS ES SSIPinstruction ptrΣ address adderseg×16 + offset 6-byte instruction queueprefetch while EU executesBus control20-bit address · 16-bit data General regsAX BX CX DXPointer/indexSP BP SI DI ALU16-bitFLAGSCF PF AF ZF SF OF… Physical address = Segment × 16 + Offset (e.g. 0B87H:0002H → 0B872H)
Fig 1. 8086 internal architecture: the BIU fetches and forms 20-bit addresses; the EU decodes and executes.

Registers

Fourteen 16-bit registers: four general-purpose registers (AX, BX, CX, DX, each split into high and low bytes), four pointer/index registers (SP, BP, SI, DI), four segment registers (CS, DS, SS, ES), the instruction pointer IP and the FLAGS register.

AHALAXAccumulator: arithmetic, MUL/DIV, I/OBHBLBXBase: memory pointer [BX]CHCLCXCount: LOOP, REP, shift count (CL)DHDLDXData: high word of MUL/DIV, I/O port SIsource index (string ops)DIdestination indexBPbase pointer (stack frame)SPstack pointer
Fig 2. 8086 general-purpose registers: AX, BX, CX and DX can be used as 16-bit registers or as two 8-bit halves.

Segmented memory

Every memory reference uses a segment (a paragraph number) and an offset:

Physical address = Segment × 10H + Offset Example: DS = 0B87H, offset = 0002H → 0B870H + 0002H = 0B872H

When DOS loads an .EXE, DS and ES point at the PSP (Program Segment Prefix), not at your data. That is why every program begins with MOV AX,@DATA / MOV DS,AX. A segment register cannot be loaded with an immediate value directly.

Development cycle

Source code is typed in an editor, assembled by MASM into a relocatable object file, linked into an executable, and then run or debugged.

EDITPROG.ASMMASM→ PROG.OBJLINK→ PROG.EXEDEBUGR T P G D URunC:\>PROG Errors at the MASM stage → fix in EDIT and re-assemble. Wrong results → single-step in DEBUG.
Fig 4. 8086 program development cycle inside DOSBox.
DOSBox commandEffect
mount c c:\masmMake the host folder appear as drive C:
c:Switch to drive C
edit prog.asmOpen the editor
masm prog;Assemble → PROG.OBJ (the ; accepts the default file names)
link prog;Link → PROG.EXE
debug prog.exeLoad into DEBUG

DEBUG commands

CmdMeaningExample
Rshow/modify registersr · r ax
Ttrace one instructiont · t 3
Pproceed over CALL/INT/LOOPp
Ggo (run)g
Ddump memoryd ds:0 l10
Uunassembleu
Eenter bytese ds:0 34 12
Qquitq
-15-14-13-12OF11DF10IF9TF8SF7ZF6-5AF4-3PF2-1CF0 Status flags: CF carry, PF parity, AF aux-carry, ZF zero, SF sign, OF overflow · Control flags: DF direction, IF interrupt, TF trap
Fig 3. The 8086 FLAGS register. DEBUG shows the flags as NV/OV UP/DN EI/DI PL/NG NZ/ZR NA/AC PO/PE NC/CY.

16-bit addition

ADD dest, src dest ← dest + src (sets CF, ZF, SF, OF, AF, PF) ADC dest, src dest ← dest + src + CF (used to add the carry) 1234H + 5678H = 68ACH CF = 0 · 0F000H + 2000H = 1 1000H → AX = 1000H, CF = 1

The result of a 16-bit addition can need 17 bits. The 17th bit goes to CF, and ADC CX,0 (or JC) captures it.

Apparatus & environment

ItemSpecificationQty
ComputerWindows / Linux / macOS PC1
DOSBox0.74-3 (DOS emulator)—
MASMMicrosoft Macro Assembler 5.10 (MASM.EXE)—
LINKOverlay Linker 3.64 (LINK.EXE)—
DEBUGDOS DEBUG.EXE—
EditorEDIT.COM or any text editor—

Safety rules & good practice

  • 💾 Keep your .ASM files in the mounted folder (e.g. C:\MASM) and save often. DOSBox does not autosave.
  • 🧭 Never assemble system files or run unknown .EXE files inside the lab folder.
  • ⌨ Use Ctrl+F9 to close a hung DOSBox and Ctrl+F10 to release the mouse.
  • 🧮 Check the number base: MASM treats 10 as decimal and 10H as hex. Hex constants must start with a digit (0FFH).
  • 🛑 Always end an .EXE with MOV AH,4CH / INT 21H. Without it the CPU runs into garbage and DOSBox may hang.

Procedure (hardware / real tools)

  1. Install DOSBox. Copy MASM.EXE, LINK.EXE, DEBUG.EXE and EDIT.COM into a folder such as C:\MASM.
  2. Start DOSBox and type mount c c:\masm, then c:.
  3. Type edit prog.asm, enter the program, save it (Alt-F, S) and exit.
  4. Assemble: masm prog;. Read the error count. Fix any errors and repeat.
  5. Link: link prog;. Note the L4021 warning if there is no stack segment.
  6. Load into the debugger: debug prog.exe. Type r to see the registers.
  7. Trace with t until the ADD executes. Note AX, CF, ZF and the other flags.
  8. Run to the end with g ("Program terminated normally"), then q.
  9. Repeat with operands that produce a carry (e.g. 0F000H + 2000H) and record both results.
Click a step to tick it off. Your progress is saved in this browser.

Doing it in this virtual lab

  1. Pick a sub-task chip. Its program opens in the blue EDIT window.
  2. Press Assemble + Link (F7). Errors appear in red: click one to jump to the line.
  3. Press Step (T) (F8) to execute one instruction at a time. Watch the registers, flags and memory change, and read the Operation panel.
  4. Or open the DOS prompt tab and type the real commands: mount c c:\masm, c:, masm prog;, link prog;, debug prog.exe, r, t, d ds:0, g, q.
  5. Change the input values in the Inputs card (the program text updates), run again, and press ● Record.

Pre-Lab Test 6 questions · diagnostic

Check your prerequisite knowledge. Each answer gets instant feedback. Score at least 4/6 before using the simulator.

Observation table

Rows are added by ● Record in the simulator: inputs, the expected (theoretical) value and the simulator result. Type your own hand calculation in "Your value" to check it. Add rows by hand for hardware-bench readings. The table is saved in this browser.

#Sub-taskInputsExpectedSimulatorYour valueCheckNote

Edge cases to test

  1. Assemble before mounting drive C. What does DOSBox say?
  2. Remove MOV DS,AX from the Hello program. What gets printed, and why?
  3. Remove the $ terminator from the message.
  4. Delete the last two lines (MOV AH,4CH / INT 21H). What does the simulator report?
  5. Use 0F000H + 2000H: check CF and CX.
  6. Use 7FFFH + 0001H: CF = 0 but OF = 1. Explain why.
  7. Write MOV AX, FFFFH (no leading 0) and read the MASM error.

Analysis & conclusion

Saved automatically in this browser.

Post-Lab Test 9 questions · instant hints

A wrong answer shows a hint so you can try again. Only first attempts count towards the score.

9. Numerical: AX = 9ABCH and BX = 8765H. After ADD AX,BX, what is AX (in hex)?

Self-evaluation

Tick what you can now do confidently.

Overall confidence:

Feedback summary

Finish the post-lab test to see your personalised summary.