
- •Chapter 1. Introduction
- •How to Develop A Program
- •What is an Assembler?
- •Modular Programming
- •Modular Program Development Process
- •Segments, Modules, and Programs
- •Translate and Link Process
- •Filename Extensions
- •Program Template File
- •Chapter 2. Architecture Overview
- •Memory Classes and Memory Layout
- •Classic 8051
- •Extended 8051 Variants
- •Philips 80C51MX
- •Intel/Atmel WM 251
- •CPU Registers
- •CPU Registers of the 8051 Variants
- •CPU Registers of the Intel/Atmel WM 251
- •Program Status Word (PSW)
- •Instruction Sets
- •Opcode Map
- •8051 Instructions
- •Additional 251 Instructions
- •Additional 80C51MX Instructions via Prefix A5
- •Chapter 3. Writing Assembly Programs
- •Assembly Statements
- •Directives
- •Controls
- •Instructions
- •Comments
- •Symbols
- •Symbol Names
- •Labels
- •Operands
- •Special Assembler Symbols
- •Immediate Data
- •Memory Access
- •Program Addresses
- •Expressions and Operators
- •Numbers
- •Characters
- •Character Strings
- •Location Counter
- •Operators
- •Expressions
- •Chapter 4. Assembler Directives
- •Introduction
- •Segment Directives
- •Location Counter
- •Generic Segments
- •Stack Segment
- •Absolute Segments
- •Default Segment
- •SEGMENT
- •RSEG
- •BSEG, CSEG, DSEG, ISEG, XSEG
- •Symbol Definition
- •CODE, DATA, IDATA, XDATA
- •esfr, sfr, sfr16, sbit
- •LIT (AX51 & A251 only)
- •Memory Initialization
- •DD (AX51 & A251 only)
- •Reserving Memory
- •DBIT
- •DSW (AX51 & A251 only)
- •DSD (AX51 & A251 only)
- •Procedure Declaration (AX51 & A251 only)
- •PROC / ENDP (AX51 & A251 only)
- •LABEL (AX51 and A251 only)
- •Program Linkage
- •PUBLIC
- •EXTRN / EXTERN
- •NAME
- •Address Control
- •EVEN (AX51 and A251 only)
- •USING
- •Other Directives
- •_ _ERROR_ _
- •Chapter 5. Assembler Macros
- •Standard Macro Directives
- •Defining a Macro
- •Parameters
- •Labels
- •Repeating Blocks
- •REPT
- •IRPC
- •Nested Definitions
- •Nested Repeating Blocks
- •Recursive Macros
- •Operators
- •NUL Operator
- •& Operator
- •< and > Operators
- •% Operator
- •;; Operator
- •! Operator
- •Invoking a Macro
- •C Macros
- •C Macro Preprocessor Directives
- •Stringize Operator
- •Predefined C Macro Constants
- •Examples with C Macros
- •C Preprocessor Side Effects
- •Chapter 6. Macro Processing Language
- •Overview
- •Creating and Calling MPL Macros
- •Creating Parameterless Macros
- •MPL Macros with Parameters
- •Local Symbols List
- •Macro Processor Language Functions
- •Comment Function
- •Escape Function
- •Bracket Function
- •METACHAR Function
- •Numbers and Expressions
- •Numbers
- •Character Strings
- •SET Function
- •EVAL Function
- •Logical Expressions and String Comparison
- •Conditional MPL Processing
- •IF Function
- •WHILE Function
- •REPEAT Function
- •EXIT Function
- •String Manipulation Functions
- •LEN Function
- •SUBSTR Function
- •MATCH Function
- •Console I/O Functions
- •Advanced Macro Processing
- •Literal Delimiters
- •Blank Delimiters
- •Identifier Delimiters
- •Literal and Normal Mode
- •MACRO Errors
- •Chapter 7. Invocation and Controls
- •Environment Settings
- •Running Ax51
- •ERRORLEVEL
- •Output Files
- •Assembler Controls
- •Controls for Conditional Assembly
- •Conditional Assembly Controls
- •Chapter 8. Error Messages
- •Fatal Errors
- •Non–Fatal Errors
- •Chapter 9. Linker/Locator
- •Overview
- •Combining Program Modules
- •Segment Naming Conventions
- •Combining Segments
- •Locating Segments
- •Overlaying Data Memory
- •Resolving External References
- •Absolute Address Calculation
- •Generating an Absolute Object File
- •Generating a Listing File
- •Bank Switching
- •Using RTX51, RTX251, and RTX51 Tiny
- •Linking Programs
- •Command Line Examples
- •Control Linker Input with µVision2
- •ERRORLEVEL
- •Output File
- •Linker/Locater Controls
- •Locating Programs to Physical Memory
- •Classic 8051
- •Extended 8051 Variants
- •Philips 80C51MX
- •Intel/Atmel WM 251
- •Data Overlaying
- •Program and Data Segments of Functions
- •Using the Overlay Control
- •Tips and Tricks for Program Locating
- •Locate Segments with Wildcards
- •Special ROM Handling (LX51 & L251 only)
- •Bank Switching
- •Common Code Area
- •Code Bank Areas
- •Bank Switching Configuration
- •Configuration Examples
- •Control Summary
- •Listing File Controls
- •Output File Controls
- •Segment and Memory Location Controls
- •High-Level Language Controls
- •Error Messages
- •Warnings
- •Non-Fatal Errors
- •Fatal Errors
- •Exceptions
- •Chapter 10. Library Manager
- •Using LIBx51
- •Interactive Mode
- •Create Library within µVision2
- •Command Summary
- •Creating a Library
- •Adding or Replacing Object Modules
- •Removing Object Modules
- •Extracting Object Modules
- •Listing Library Contents
- •Error Messages
- •Fatal Errors
- •Errors
- •Chapter 11. Object-Hex Converter
- •Using OHx51
- •OHx51 Command Line Examples
- •Creating HEX Files for Banked Applications
- •OHx51 Error Messages
- •Using OC51
- •OC51 Error Messages
- •Intel HEX File Format
- •Record Format
- •Data Record
- •Extended 8086 Segment Record
- •Extended Linear Address Record
- •Example Intel HEX File
- •Appendix A. Application Examples
- •ASM – Assembler Example
- •Using A51 and BL51
- •Using AX51 and LX51
- •Using A251 and L251
- •CSAMPLE – C Compiler Example
- •Using C51 and BL51
- •Using C51 and LX51
- •Using C251 and L251
- •BANK_EX1 – Code Banking with C51
- •Using C51 and BL51
- •Using C51 and LX51
- •BANK_EX2 – Banking with Constants
- •Using C51 and BL51
- •Using C51 and LX51
- •Using BL51
- •Using C51 and LX51
- •Philips 80C51MX – Assembler Example
- •Philips 80C51MX – C Compiler Example
- •Appendix B. Reserved Symbols
- •Appendix C. Listing File Format
- •Assembler Listing File Format
- •Listing File Heading
- •Source Listing
- •Macro / Include File / Save Stack Format
- •Symbol Table
- •Listing File Trailer
- •Appendix D. Assembler Differences
- •Differences Between A51 and A251/AX51
- •Differences between A51 and ASM51
- •Differences between A251/AX51 & ASM51
- •Glossary
- •Index

84 |
Chapter 3. Writing Assembly Programs |
|
|
Expressions and Operators
An operand may be a numeric constant, a symbolic name, a character string or an expression.
Operators are used to combine and compare operands within your assembly program. Operators are not assembly language instructions nor do they generate x51 assembly code. They represent operations that are evaluated at assembly-time. Therefore, operators can only handle calculations of values that are known when the program is assembled.
An expression is a combination of numbers, character string, symbols, and 3 operators that evaluate to a single 32-bit binary number (for A51: 16-bit binary
number). Expressions are evaluated at assembly time and can, therefore, be used to calculate values that would otherwise be difficult to determine beforehand.
The following sections describe operators and expressions and how they are used in x51 assembly programs.
Numbers
Numbers can be specified in hexadecimal (base 16), decimal (base 10), octal (base 8), and binary (base 2). The base of a number is specified by the last character in the number. A number that is specified without an explicit base is interpreted as decimal number.
Shaded directives and options are available only in AX51 and A251.

Keil Software — A51/AX51/A251 Macro Assembler and Utilities |
85 |
|
|
The following table lists the base types, the base suffix character, and some examples:
|
Base |
Suffix |
Legal Characters |
Examples |
|
|
|
Hexadecimal |
H, h |
0 – 9, A – F, a – f |
0x1234 0x99 1234H 0A0F0h 0FFh |
|
|
|
Decimal |
D, d |
0 – 9 |
1234 65590d 20d 123 |
|
|
|
Octal |
O, o, Q, q |
0 – 7 |
177o 25q 123o 177777q |
|
|
|
Binary |
B, b |
0 and 1 |
10011111b 101010101b |
|
|
|
The first character of a number must be a digit between 0 and 9. Hexadecimal |
|
||||
|
numbers which do not have a digit as the first character should be prefixed with |
|
||||
|
3 |
|||||
|
a 0. The Ax51 assembler supports also hex numbers written in C notation. |
|||||
|
The dollar sign character ($) can be used in a number to make it more readable, |
|||||
|
however, the dollar sign character cannot be the first or last character in the |
|
||||
|
|
|||||
|
number. A dollar sign used within a number is ignored by the assembler and has |
|
||||
|
no impact on the value of the number. For example: |
|
||||
|
|
|
|
|
||
|
1111$0000$1010$0011b |
is equivalent to |
1111000010100011B |
|
|
|
|
1$2$3$4 |
|
is equivalent to |
1234 |
|
|
Colon Notation for Numbers (A251 only)
The A251 assembler supports the notation page:number for specifying absolute addresses. Numbers specified with this notation receive the memory type EDATA when page is 0 or ECODE for all other pages. In this way, you can use such numbers for referencing any memory location. For example:
ABSVAL1 |
EQU |
0:20H |
; symbol to address location 20H |
ABSVAL2 |
EQU |
0:80H |
; symbol to address location 80H in EDATA space |
PORT0 |
EQU |
S:80H |
; symbol to SFR space 80H |
ENTRY |
EQU |
10:2000H |
; entry point at location 102000H |
|
MOV |
WR0,ABSVAL1 |
|
|
MOV |
R1,ABSVAL2 |
|
|
MOV |
PORT0,R1 |
|
|
EJMP |
ENTRY |
|
|
MOV |
WR0,0:20H |
; access to ABSVAL1 |
|
MOV |
R1,0:80H |
; access to ABSVAL2 |
|
MOV |
S:80H,R1 |
|
|
EJMP |
10:2000H |
|
The colon notation is accepted in several A251 controls and is converted as described.
Shaded directives and options are available only in AX51 and A251.

|
86 |
|
|
Chapter 3. Writing Assembly Programs |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Number in Colon Notation |
Replaced with |
|
|
|
|
VAL1 EQU 0:20H |
VAL1 EQU EDATA 20H |
|
|
|
|
VAL2 EQU 0FF:1000H |
VAL2 EQU ECODE 0FF1000H |
|
|
|
|
ORG 0FE:2000H |
?modulename?number SEGMENT ECODE AT 0FE2000H |
|
|
|
|
|
RSEG ?modulename?number |
|
|
|
|
ORG 0:400H |
?modulename?number SEGMENT EDATA AT 400H |
|
|
|
|
|
RSEG ?modulename?number |
|
|
|
|
CSEG AT 0FE:2000H |
?modulename?number SEGMENT ECODE AT 0FE2000H |
|
|
|
|
|
RSEG ?modulename?number |
|
|
|
|
BVAR1 BIT 0:20H.1 |
BVAR1 BIT 20H.1 |
|
|
|
|
BVAR1 BIT 0:30H.1 |
BVAR1 EQU EBIT 30H.1 |
|
3 |
|
|
PUSH.B #13 |
PUSH BYTE #13 |
|
|
|
PUSH.W #13 |
PUSH WORD #13 |
|
|
|
|
|
|
|
|
|
|
NOTE |
|
|
The colon notation is provided for source compatibility with other 251 macro assemblers. If you do not need to port your code to other assemblers, it is recommended to use directly the replacement sequence in your assembler source file.
Characters
The Ax51 assembler allows you to use ASCII characters in an expression to generate a numeric value. Up to two characters enclosed within single quotes (') may be included in an expression. More than two characters in single quotes in an expression will cause the Ax51 assembler to generate an error. Following are examples of character expressions:
'A' |
evaluates to 0041h |
'AB' |
evaluates to 4142h |
'a' |
evaluates to 0061h |
'ab' |
evaluates to 6162h |
'' |
null string evaluates to 0000h |
'abc' |
generates an ERROR |
Characters may be used anywhere in your program as a immediate data operand. For example:
LETTER_A |
EQU |
'A' |
TEST: |
MOV |
@R0, #'F' |
|
SUBB |
A, #'0' |
Shaded directives and options are available only in AX51 and A251.

Keil Software — A51/AX51/A251 Macro Assembler and Utilities |
87 |
|
|
Character Strings
Character strings can be used in combination with the DB directive to define messages that are used in your x51 assembly program. Character strings must be enclosed within single quotes ('). For example:
KEYMSG: |
DB |
'Press any key to continue.' |
generates the hexadecimal data (50h, 72h, 65h, 73h, 73h, 20h, … 6Eh, 75h, 65h, 2Eh) starting at KEYMSG. You can mix string and numeric data on the same line. For example:
|
|
|
|
3 |
EOLMSG: |
DB |
'End of line', 00h |
|
|
appends the value 00h to the end of the string 'End of line'. |
||||
|
|
|
|
|
Two successive single quote characters can be used to insert a single quote into a string. For example:
MSGTXT: |
DB |
'ISN''T A QUOTE REQUIRED HERE?'. |
Location Counter
The Ax51 assembler maintains a location counter for each segment. The location counter contains the offset of the instruction or data being assembled and is incremented after each line by the number of bytes of data or code in that line.
The location counter is initialized to 0 for each segment, but can be changed using the ORG directive.
The dollar sign character ($) returns the current value of the location counter. This operator allows you to use the location counter in an expression. For
example, the following code uses |
$ to calculate the length of a message string. |
||
|
|
|
|
MSG: |
DB |
'This is a message', 0 |
|
MSGLEN |
EQU |
$ – MSG |
|
You can also use |
$ in an instruction. For example, the following line of code |
||
will repeat forever. |
|
|
|
|
|
|
|
|
JMP |
$ |
; repeat forever |
Shaded directives and options are available only in AX51 and A251.