4.0 Programming and Memory Organization in the 8051
4.1 Assembly Language Fundamentals
Assembly language provides symbolic mnemonics for machine-level instructions. An assembler translates this low-level code into machine code (object code). An assembly statement has four optional fields: [label:] mnemonics [operands] [;comment].
- Instructions (e.g., MOV, ADD) tell the CPU what to do and are translated into opcodes.
- Directives (e.g., ORG, END) instruct the assembler during the assembly process.
The process to create and run a program is:
- Write the source code in an editor and save it as an .asm file.
- Use an assembler to convert the .asm file into an object (.obj) file and a list (.lst) file.
- A linker combines object files into an absolute object (.abs) file.
- An object-to-hex converter (OH) creates a .hex file ready to be burned into ROM.
4.2 Key Registers and Their Functions
- Accumulator (A): An 8-bit register used for all arithmetic and logic operations.
- B Register: An 8-bit register used primarily for multiplication (MUL AB) and division (DIV AB) operations. Also serves as a temporary storage register.
- “R” Registers (R0-R7): A set of eight 8-bit general-purpose registers used as auxiliary or temporary storage. They are organized into four selectable register banks.
- Data Pointer (DPTR): The only user-accessible 16-bit register, used to hold addresses for accessing external memory.
- Program Counter (PC): A 16-bit register that holds the address of the next instruction to be executed. It starts at 0000H on reset and can address up to 64KB of program memory.
- Stack Pointer (SP): An 8-bit register that points to the last used location on the stack. It is initialized to 07H, meaning the stack begins at RAM location 08H.
4.3 Memory and Data Organization
The 8051 has 128 bytes of internal RAM (addresses 00H to 7FH) allocated as follows:
- 00H – 1FH (32 bytes): Four register banks (Bank 0, 1, 2, 3), with 8 registers (R0-R7) per bank. Bank 0 is the default.
- 20H – 2FH (16 bytes): Bit-addressable memory space.
- 30H – 7FH (80 bytes): General-purpose read/write storage, known as the “scratch pad.”
Stack: The stack is a section of RAM used for temporary storage during subroutine calls (PUSH, POP). The SP register increments before a PUSH and decrements after a POP.
Endianness: The source describes Big-Endian (most significant byte at the lowest address) and Little-Endian (least significant byte at the lowest address) memory storage formats.
4.4 The Program Status Word (PSW) Register
The PSW is an 8-bit Special Function Register (SFR), also known as the flag register, that reflects the current status of the CPU.
| Bit | Symbol | Name | Description |
| PSW.7 | CY | Carry Flag | Set if there is a carry out from the D7 bit after an arithmetic operation. |
| PSW.6 | AC | Auxiliary Carry Flag | Set if there is a carry from D3 to D4, used for BCD arithmetic. |
| PSW.5 | F0 | Flag 0 | Available for general-purpose user use. |
| PSW.4 | RS1 | Register Bank Select 1 | Used with RS0 to select one of the four register banks. |
| PSW.3 | RS0 | Register Bank Select 0 | Used with RS1 to select one of the four register banks. |
| PSW.2 | OV | Overflow Flag | Set if the result of a signed number operation is too large. |
| PSW.1 | – | User Definable | Undefined, user-definable flag. |
| PSW.0 | P | Parity Flag | Set to 1 if the accumulator contains an odd number of 1s. |