[ ]
MPMC Lab · Experiment 4 · 8086 · Arrays

Data Handling and Array Operations (8086)

Block transfer of N bytes/words, the smallest and largest values in an array, and counting the 0s and 1s in a byte.

☰ All experiments

Aim

To write and execute 8086 programs that transfer a block of N bytes/words, find the smallest and largest numbers in an array, and count the number of 0s and 1s in a byte.

Learning objectives

1
Indirect addressing
Use SI/DI/BX as pointers with [SI], [DI] and [BX+SI].
2
Loops
Control loops with CX and the LOOP instruction.
3
String instructions
Use MOVSB/MOVSW with REP, CLD/STD, and set ES correctly.
4
Comparison
Use CMP with JA/JB/JAE/JBE (unsigned) and JG/JL (signed).
5
Bit testing
Shift or rotate bits into CF and count them.
6
Edge cases
Handle an array of length 1, overlapping blocks and equal elements.

What this experiment demonstrates

  • Pointer-based processing of arrays, the core of any data handling.
  • The 8086 string instructions that copy a whole block with one REP prefix.
  • Shifting bits through the carry flag to examine them one at a time.

Sub-tasks

  • 4.1a Block transfer of N bytes (REP MOVSB)
  • 4.1b Block transfer of N words (loop)
  • 4.2 Smallest and largest number in an array
  • 4.3 Count the 0s and 1s in a byte

Addressing modes

ModeExampleEffective address
DirectMOV AL,ARRoffset of ARR
Register indirectMOV AL,[SI]SI
Based/indexed + dispMOV AL,ARR[SI]ARR + SI
Based indexedMOV AL,[BX+SI]BX + SI

Only BX, BP, SI and DI can appear inside brackets. BP defaults to the stack segment SS.

String instructions

REP MOVSB (DF = 0) 1122334455 DS:SI 112233···· ES:DI each step: [ES:DI] ← [DS:SI] SI ← SI ± 1, DI ← DI ± 1 (DF) CX ← CX − 1, repeat while CX ≠ 0 Set ES = DS first (MOV ES,AX) andCLD for forward copying.
Fig 6. Block transfer with string instructions.
MOVSB / MOVSW [ES:DI] ← [DS:SI]; SI,DI ± 1 or 2 LODSB / STOSB AL ← [DS:SI] / [ES:DI] ← AL REP repeat CX times REPE/REPNE: also stop on ZF CLD / STD DF = 0 (forward) / DF = 1 (backward)

Finding min / max

min = max = ARR[0] for i = 1 … N−1: if ARR[i] < min: min = ARR[i] (JAE skip) if ARR[i] > max: max = ARR[i] (JBE skip)

Use JA/JB for unsigned data and JG/JL for signed data. As a signed value, 0F0H is −16, so the "largest" element changes.

Counting bits

MOV CX,8 NEXT: SHR AL,1 ; bit 0 → CF JC ONE ; CF = 1 → count a one INC ZEROS … LOOP NEXT Check: ones + zeros = 8

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. Enter the program, assemble and link.
  2. In DEBUG, dump the source block or array with d ds:0.
  3. Trace through one or two loop iterations with t, watching SI, DI, CX and the flags.
  4. Use p to step over a whole REP MOVSB or LOOP.
  5. Run to completion with g and dump the results.
  6. Change the data (array values, N) and repeat. Record each run.
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. 4.1a without MOV ES,AX. Where did the bytes go?
  2. 4.1a with STD instead of CLD (and SI/DI unchanged). What happens?
  3. 4.2 with a single-element array (N − 1 = 0, so CX = 0 → LOOP runs 65536 times!). Guard with JCXZ.
  4. 4.2 with all elements equal.
  5. 4.2 with 0F0H in the array: compare JAE/JBE with JGE/JLE.
  6. 4.3 with 00H and 0FFH.
  7. 4.1b: replace ADD SI,2 with INC SI. What goes wrong?

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: how many 1-bits are there in 0E7H?

Self-evaluation

Tick what you can now do confidently.

Overall confidence:

Feedback summary

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