Aim
To write and execute 8086 assembly programs that subtract two 16-bit numbers, add two 16-bit numbers stored in memory, and perform AND, OR and XOR operations on data stored in memory.
Learning objectives
Define variables with DB/DW and access them by name (direct addressing).
Use SUB/SBB and interpret CF as a borrow and SF/OF as sign information.
Locate a 16-bit word in a memory dump (low byte at the lower address).
Apply AND (mask), OR (set) and XOR (toggle) and predict the result bit by bit.
Predict ZF, SF, PF, CF and OF after arithmetic and logic operations.
Compare simulator results with hand calculations in a table.
What this experiment demonstrates
- Arithmetic and logic instructions operate between a register and memory: memory-to-memory is not allowed.
- How the borrow appears in CF, and how a negative result is stored in two's complement.
- Bit masking, the basis of all I/O bit control.
Sub-tasks
- 2.1 Subtract two 16-bit numbers
- 2.2 Add two 16-bit numbers stored in memory
- 2.3 AND, OR and XOR on data in memory
Data definitions
The 8086 is little-endian: the low byte of a word is stored at the lower address. d ds:0 in DEBUG shows 4C 9A for 9A4CH.
Subtraction
Logical operations
| A | B | AND | OR | XOR |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
AND, OR and XOR always clear CF and OF, and set ZF, SF and PF from the result. XOR AX,AX is a common way to clear a register.
Flags register
Apparatus & environment
| Item | Specification | Qty |
|---|---|---|
| Computer | Windows / Linux / macOS PC | 1 |
| DOSBox | 0.74-3 (DOS emulator) | — |
| MASM | Microsoft Macro Assembler 5.10 (MASM.EXE) | — |
| LINK | Overlay Linker 3.64 (LINK.EXE) | — |
| DEBUG | DOS DEBUG.EXE | — |
| Editor | EDIT.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
10as decimal and10Has 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)
- Mount the MASM folder in DOSBox and switch to C:.
- Enter the program for the sub-task in EDIT and save it.
- Assemble and link (
masm prog;,link prog;). - Load into DEBUG and type
d ds:0to see the operands in memory (remember little-endian). - Single-step with
tand note the flags after SUB/ADD/AND/OR/XOR. - Run with
g, thend ds:0again and read the results. - Change the operands, repeat, and compare with your hand calculations.
Doing it in this virtual lab
- Pick a sub-task chip. Its program opens in the blue EDIT window.
- Press Assemble + Link (F7). Errors appear in red: click one to jump to the line.
- Press Step (T) (F8) to execute one instruction at a time. Watch the registers, flags and memory change, and read the Operation panel.
- 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. - 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-task | Inputs | Expected | Simulator | Your value | Check | Note |
|---|
Edge cases to test
- Write
MOV NUM1,NUM2. Which MASM error appears? - Write
MOV AL,NUM1with NUM1 a DW. Read the error, then fix it with BYTE PTR. - Make NUM2 > NUM1 in 2.1: explain the DIFF value and CF.
- Use 8000H − 0001H: why is OF = 1?
- In 2.3 use OPR1 = OPR2: which result is 00H and which flag shows it?
- Use a value with a leading letter, such as
F234H: read the error.
Analysis & conclusion
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.
Self-evaluation
Tick what you can now do confidently.