Aim
To write and execute 8051 assembly programs that add two 8-bit numbers in internal RAM, subtract two 8-bit numbers in external RAM, transfer a block of N bytes in internal RAM, and perform bit-manipulation operations.
Learning objectives
Use immediate, register, direct, register-indirect (@R0/@R1) and external (MOVX @DPTR) addressing.
Interpret CY, AC, OV and P after ADD/SUBB.
Access XRAM with MOVX and the 16-bit DPTR.
Write counted loops with DJNZ and pointers.
Use SETB, CLR, CPL, MOV C,bit, ANL/ORL C and JB/JNB on bit-addressable RAM and port pins.
Locate register banks, the bit area, scratch-pad RAM and SFRs.
What this experiment demonstrates
- The 8051 memory spaces: internal RAM, SFRs, external RAM and code.
- Why SUBB needs CLR C first.
- The 8051 Boolean processor: single-bit logic without masking.
Sub-tasks
- 6.1 Add two 8-bit numbers in internal RAM
- 6.2 Subtract two 8-bit numbers in external RAM
- 6.3 Block transfer of N bytes in internal RAM
- 6.4 Bit-manipulation operations
Memory map & PSW
Addressing modes
| Mode | Example | Meaning |
|---|---|---|
| Immediate | MOV A,#25H | A ← 25H |
| Register | MOV A,R2 | A ← R2 |
| Direct | MOV A,40H | A ← RAM[40H] |
| Register-indirect | MOV A,@R0 | A ← RAM[R0] |
| External | MOVX A,@DPTR | A ← XRAM[DPTR] |
| Indexed (code) | MOVC A,@A+DPTR | A ← CODE[A+DPTR] |
Arithmetic and flags
Bit manipulation
Bytes 20H–2FH hold 128 individually addressable bits (bit addresses 00H–7FH). SFRs whose address ends in 0 or 8 (P0, P1, P2, P3, ACC, B, PSW, TCON, IE…) are also bit-addressable (80H–FFH).
Apparatus & environment
| Item | Specification | Qty |
|---|---|---|
| PC with Keil µVision | C51/A51 tool chain (evaluation version is fine) | 1 |
| 8051 trainer board | Nuvoton W78E052D, 11.0592 MHz crystal | 1 |
| Nuvoton ISP-ICP utility | APROM programming over USB-UART (CH340) | — |
| USB cable | USB-A to the board's serial/ISP port | 1 |
| Power supply | +5 V regulated (board adapter) | 1 |
Safety rules & good practice
- ⚡ Switch the board off before connecting or removing modules, ribbon cables or jumpers.
- 🔌 Check the supply: +5 V only. Reverse polarity or more than 6 V destroys the controller.
- 🧲 Handle ICs by the edges and touch a grounded surface first (ESD).
- 🔁 Do not drive a port pin connected to a switch with 0 while the switch is pressed to VCC. Read inputs only after writing 1s.
- 🔥 If a component gets hot or smells, switch off immediately and inform the instructor.
Procedure (hardware / real tools)
- Create a Keil project for the W78E052D and add main.a51.
- Enter the program for the sub-task and build it.
- Start a debug session. Open View ▸ Memory and enter the input data (D:40H, X:2000H …).
- Single-step (F11) and watch A, PSW and the memory window.
- Run to the end (SJMP $) and read the results in memory.
- Change the inputs (for example to cause a carry or a borrow) and repeat. Record each case.
Doing it in this virtual lab
- Choose a sub-task chip. The program opens in the µVision window.
- Press 🔨 Build (F7). Errors are listed in Build Output: click one to jump to the line.
- Press ▶ Run (F5) and watch the board. Use ¼× to 1/1000× slow motion to see fast events.
- Use Step (F11) and Step Over (F10), and click line numbers to set breakpoints.
- Edit the code (delay, pattern, pins), rebuild, and observe the change. Press ● Record to log results.
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
- 6.1: use FFH + 01H. Check A, CY, AC and P.
- 6.2: delete
CLR Cafter running 6.1 (CY = 1). The result is off by one. - 6.2: use 45H − 30H (no borrow) and 80H − 01H (OV = 1: signed overflow).
- 6.3: set DEST EQU 34H (overlap) and explain the corrupted copy.
- 6.3: N EQU 0. How many bytes does DJNZ copy?
- 6.4: write
SETB P1(a byte, not a bit) and read the error. - Remove
HERE: SJMP HEREand run: the PC runs into empty flash.
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.