Step Debugging in Emu8086: Single-Step, Breakpoints, Memory and Flags

Most beginner programs are not wrong in a big way; they are wrong in one register, one flag, or one address. The emulator’s debugging tools exist to find that one thing. This tutorial walks through them with a small program you can break on purpose. It assumes you can already write and run a program; the Hello World tutorial is the starting point, and Emu8086 setup is in the install guide.

A program to practise on

Assemble the program below. It counts CX down from 3 to 0, jumping back while CX is not zero. You will practise watching it, then change the starting value and confirm the loop runs a different number of times.

ORG 100h

MOV CX, 3
count_down:
DEC CX
JNZ count_down
RET

Single-stepping

Single-step executes one instruction at a time. Step through the program and watch CX fall from 3 to 2 to 1, then to 0. The jump should be taken twice and not taken on the third pass, when CX reaches zero. If you see the loop exit early, check whether an instruction between the decrement and the jump changed the flags: DEC sets the zero flag, and the jump reads it, so nothing may sit between them that also sets flags. The branches and loops tutorial explains this flag discipline.

What you should see

In this version, the jump should be taken twice and not taken on the third pass. If you still see the loop exit early, check whether an instruction between the decrement and the jump changed the flags: DEC sets the zero flag, and the jump reads it, so nothing may sit between them that also sets flags. The branches and loops tutorial explains this flag discipline.

Breakpoints

A breakpoint pauses execution when it reaches a chosen line, so you do not have to step through a long program to reach the interesting part. Set a breakpoint on the DEC CX line, run at full speed, and the program should pause there on every pass. Inspect CX at each pause: it should read 3 on the first pause, then 2, then 1, counting the pauses as you go. Remove the breakpoint when you are done, or the program will keep pausing there on later runs.

Memory and flags

Use the memory window to check variables and arrays directly, rather than inferring them from registers. The sorting tutorial is a good place to practise this: pause after each pass and read the array from memory. For flags, watch the zero flag after comparisons, the carry flag after subtraction, and the sign flag after operations that produce a negative-looking result. A jump that misbehaves is almost always a flag that is not what you assumed, and the flag display tells you in one glance.

A debugging checklist

  • Reproduce the fault with the smallest program that still shows it.
  • Single-step the first few instructions before assuming the later ones are at fault.
  • Check the flags at the jump, not just the registers.
  • Check that pushes and pops balance, so a RET returns to the right address.
  • When memory looks wrong, check the address the program actually used, not the one you meant it to use.

Next steps

Apply these tools to the sorting example and the subroutine example next; both reward careful tracing. The opcode reference helps you predict each instruction’s flag effects, and the home page links the series.

Similar Posts