A51
MPMC Lab · Experiment 6 · 8051 · Assembly

8051 Assembly Language Programming

8-bit addition in internal RAM, subtraction in external RAM, block transfer, and bit manipulation.

☰ All experiments

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

1
Addressing modes
Use immediate, register, direct, register-indirect (@R0/@R1) and external (MOVX @DPTR) addressing.
2
PSW flags
Interpret CY, AC, OV and P after ADD/SUBB.
3
External memory
Access XRAM with MOVX and the 16-bit DPTR.
4
Loops
Write counted loops with DJNZ and pointers.
5
Bit operations
Use SETB, CLR, CPL, MOV C,bit, ANL/ORL C and JB/JNB on bit-addressable RAM and port pins.
6
Memory map
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

SFRs (80H–FFH, direct) Scratch-pad RAM 30H–7FH Bit-addressable 20H–2FH 4 register banks00H–1FH (R0–R7 × 4) FFH7FH2FH1FH00H PSW (D0H)CYD7ACD6F0D5RS1D4RS0D3OVD2—D1PD0 RS1 RS0 select bank: 00→00H, 01→08H, 10→10H, 11→18H External RAM (64 KB): MOVX A,@DPTR / MOVX @DPTR,A only Code ROM (64 KB): MOVC A,@A+DPTR (look-up tables) Bit address = (byte − 20H) × 8 + bit e.g. 21H.3 → 0BH SP resets to 07H: the stack grows upward from 08H
Fig 3. 8051 on-chip memory map and the PSW.

Addressing modes

ModeExampleMeaning
ImmediateMOV A,#25HA ← 25H
RegisterMOV A,R2A ← R2
DirectMOV A,40HA ← RAM[40H]
Register-indirectMOV A,@R0A ← RAM[R0]
ExternalMOVX A,@DPTRA ← XRAM[DPTR]
Indexed (code)MOVC A,@A+DPTRA ← CODE[A+DPTR]

Arithmetic and flags

ADD A,src A ← A + src CY = carry out of bit 7, AC = carry out of bit 3 ADDC A,src A ← A + src + CY SUBB A,src A ← A − src − CY (no plain SUB: always CLR C first!) CY = borrow, OV = signed overflow F5H + 2AH = 11FH → A = 1FH, CY = 1 30H − 45H = −15H → A = EBH, CY = 1 (two's complement)

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

SETB bit / CLR bit / CPL bit set / clear / toggle MOV C,bit / MOV bit,C copy via the carry ("Boolean accumulator") ANL C,bit · ORL C,/bit AND / OR (/ = complemented operand) JB bit,label · JNB bit,label branch on a bit Bit address of 21H.3 = (21H − 20H) × 8 + 3 = 0BH

Apparatus & environment

ItemSpecificationQty
PC with Keil µVisionC51/A51 tool chain (evaluation version is fine)1
8051 trainer boardNuvoton W78E052D, 11.0592 MHz crystal1
Nuvoton ISP-ICP utilityAPROM programming over USB-UART (CH340)—
USB cableUSB-A to the board's serial/ISP port1
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)

  1. Create a Keil project for the W78E052D and add main.a51.
  2. Enter the program for the sub-task and build it.
  3. Start a debug session. Open View ▸ Memory and enter the input data (D:40H, X:2000H …).
  4. Single-step (F11) and watch A, PSW and the memory window.
  5. Run to the end (SJMP $) and read the results in memory.
  6. Change the inputs (for example to cause a carry or a borrow) and repeat. Record each case.
Click a step to tick it off. Your progress is saved in this browser.

Doing it in this virtual lab

  1. Choose a sub-task chip. The program opens in the µVision window.
  2. Press 🔨 Build (F7). Errors are listed in Build Output: click one to jump to the line.
  3. Press ▶ Run (F5) and watch the board. Use ¼× to 1/1000× slow motion to see fast events.
  4. Use Step (F11) and Step Over (F10), and click line numbers to set breakpoints.
  5. 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-taskInputsExpectedSimulatorYour valueCheckNote

Edge cases to test

  1. 6.1: use FFH + 01H. Check A, CY, AC and P.
  2. 6.2: delete CLR C after running 6.1 (CY = 1). The result is off by one.
  3. 6.2: use 45H − 30H (no borrow) and 80H − 01H (OV = 1: signed overflow).
  4. 6.3: set DEST EQU 34H (overlap) and explain the corrupted copy.
  5. 6.3: N EQU 0. How many bytes does DJNZ copy?
  6. 6.4: write SETB P1 (a byte, not a bit) and read the error.
  7. Remove HERE: SJMP HERE and run: the PC runs into empty flash.

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: A = 30H, CY = 0. After SUBB A,#45H, what is A (hex)?

Self-evaluation

Tick what you can now do confidently.

Overall confidence:

Feedback summary

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