±
MPMC Lab · Experiment 2 · 8086 · ALU

Basic Arithmetic and Logical Operations (8086)

16-bit subtraction, addition of memory operands, and AND / OR / XOR on data in memory.

☰ All experiments

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

1
Memory operands
Define variables with DB/DW and access them by name (direct addressing).
2
Subtraction & borrow
Use SUB/SBB and interpret CF as a borrow and SF/OF as sign information.
3
Little-endian
Locate a 16-bit word in a memory dump (low byte at the lower address).
4
Logical instructions
Apply AND (mask), OR (set) and XOR (toggle) and predict the result bit by bit.
5
Flags
Predict ZF, SF, PF, CF and OF after arithmetic and logic operations.
6
Verification
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

NUM1 DW 9A4CH ; word (2 bytes) stored as 4C 9A RES DW ? ; reserve a word (initialised to 0) FLAG DB 0 ; byte ARR DB 5 DUP(0) ; 5 bytes of 0

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

SUB dest, src dest ← dest − src CF = 1 if a borrow was needed (src > dest, unsigned) SBB dest, src dest ← dest − src − CF (multi-word subtraction) 9A4CH − 3B7FH = 5ECDH CF = 0 3B7FH − 9A4CH = A133H CF = 1 (= −5ECDH in two's complement)

Logical operations

ABANDORXOR
00000
01011
10011
11110
5AH = 0101 1010 AND 3CH = 0001 1000 = 18H (keep bits) 3CH = 0011 1100 OR 3CH = 0111 1110 = 7EH (set bits) XOR 3CH = 0110 0110 = 66H (toggle bits)

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

-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.

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. Mount the MASM folder in DOSBox and switch to C:.
  2. Enter the program for the sub-task in EDIT and save it.
  3. Assemble and link (masm prog;, link prog;).
  4. Load into DEBUG and type d ds:0 to see the operands in memory (remember little-endian).
  5. Single-step with t and note the flags after SUB/ADD/AND/OR/XOR.
  6. Run with g, then d ds:0 again and read the results.
  7. Change the operands, repeat, and compare with your hand calculations.
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. Write MOV NUM1,NUM2. Which MASM error appears?
  2. Write MOV AL,NUM1 with NUM1 a DW. Read the error, then fix it with BYTE PTR.
  3. Make NUM2 > NUM1 in 2.1: explain the DIFF value and CF.
  4. Use 8000H − 0001H: why is OF = 1?
  5. In 2.3 use OPR1 = OPR2: which result is 00H and which flag shows it?
  6. Use a value with a leading letter, such as F234H: read the 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: what is 0C5H AND 3AH? (hex)

Self-evaluation

Tick what you can now do confidently.

Overall confidence:

Feedback summary

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