Three instructions carry most beginner programs: MOV copies a value, ADD adds to it and SUB subtracts from it. They look trivial, and that is exactly why they deserve a careful tutorial: almost every later bug is a misunderstanding of one of these three. You need Emu8086 installed first; see the install guide.
MOV copies, it does not move
Despite its name, MOV leaves the source unchanged and overwrites the destination. MOV AX, BX makes AX equal to BX, and BX keeps its old value. There is one famous restriction: you cannot move one memory location directly to another. At least one side of a MOV must be a register.
Adding and subtracting with numbers in memory
The program below adds two numbers stored after the code and leaves the answer in a third variable. Because the program ends with RET, the data after it is never executed as code. Step through it and watch AL change at each arithmetic instruction.
ORG 100h MOV AL, num1 ADD AL, num2 MOV result, AL RET num1 DB 10 num2 DB 25 result DB 0
What you should see
After the program runs, the result byte should hold 35. In the register window, AL should also hold 35 at the moment RET executes. If you change num1 and num2 and re-run, the result should follow: for example 20 and 7 give 27.
Flags: the part beginners skip
ADD and SUB update the flags; MOV does not. That matters as soon as you branch: a conditional jump after a MOV tests the flags left by some earlier instruction, not the move you just did. If a jump behaves oddly, check whether a MOV sits between the comparison and the jump. The branches and loops tutorial builds on exactly this point.
Subtraction and borrowing
The program below subtracts 3 from 10, then subtracts a larger number from a smaller one so you can see the borrow in action. The second result wraps around in unsigned arithmetic, which is normal 8-bit behaviour, not an error in the emulator.
ORG 100h MOV AL, 10 SUB AL, 3 MOV BL, AL MOV AL, 3 SUB AL, 10 RET
What you should see
BL should hold 7 after the first subtraction. After the second subtraction, AL should hold the wrapped-around value for 3 minus 10 in 8 bits, and the carry flag should be set, signalling the borrow. That flag is what a later JB or JAE instruction would test.
Next steps
Continue with branches and loops, where these flag effects decide which path a program takes, and registers and segments if any register behaviour above surprised you. The opcode reference lists the flag effects of each instruction, and the home page links the whole series.
