5.0 Programming with 8051 Assembly Language
Assembly language provides direct, low-level control over the microcontroller’s hardware. While high-level languages like C are also used, understanding assembly is essential for grasping how the 8051 truly works. This section covers the fundamental structure of an 8051 assembly program, the workflow for creating an executable file, and the key assembler directives that guide the process.
5.1 Structure of an Assembly Language Statement
An assembly language program is a series of statements, each consisting of up to four optional fields:
[ label: ] mnemonics [ operands ] [ ;comment ]
- Label: A name given to a line of code, allowing it to be referenced from other parts of the program (e.g., as a target for a jump instruction). A label must be followed by a colon (:).
- Mnemonic: The assembly language instruction itself (e.g., MOV, ADD, SJMP). This is translated by the assembler into a machine code opcode.
- Operands: The data, registers, or addresses that the instruction will operate on. An instruction may have zero, one, or two operands.
- Comment: Begins with a semicolon (;). Comments are ignored by the assembler and are used solely for documenting the code for human readers.
5.2 The Development Workflow
Creating and running an 8051 program is a multi-step process that involves several software tools.
- Editing: First, use a text editor to write your assembly language code. The source file is typically saved with a .asm extension. This file is a standard ASCII text file.
- Assembling: Next, an assembler program reads your .asm source file. It translates the assembly mnemonics into machine code opcodes, producing an object file (.obj). The assembler may also generate a list file (.lst), which shows the generated machine code alongside your original source code and is useful for debugging.
- Linking: A linker program takes one or more .obj files and combines them into a single absolute object file (.abs). This step is crucial for larger projects that are split into multiple source files.
- Hex Conversion: Finally, an object-to-hex converter utility (OH) processes the .abs file to create a hexadecimal file (.hex). This .hex file contains the final machine code in a format that can be loaded (“burned”) into the 8051’s on-chip ROM.
5.3 Essential Assembler Directives
Assembler directives, also called pseudo-instructions, are not instructions for the CPU. Instead, they are commands for the assembler, guiding how it translates the code.
- ORG (Origin): Sets the starting memory address for the code that follows. For example, ORG 0H tells the assembler to place the next instruction at memory location 0000H, which is the 8051’s reset location.
- EQU (Equate): Defines a constant by associating a symbolic name (a label) with a value. For example, COUNT EQU 25 allows you to use the word COUNT in your code instead of the number 25. This does not occupy any memory.
- DB (Define Byte): Defines an 8-bit data value and allocates memory for it. Data can be specified in decimal, hexadecimal (39H), binary (00110101B), or as an ASCII string (‘Hello’).
- END: Indicates the end of the source file. The assembler will ignore any text that appears after the END directive.
With an understanding of the language structure, the next critical concept is how the CPU can access the data specified in the operands. These methods are known as addressing modes.