Common Errors in Writing Assembly Language

1. Analysis of Reasons for Assembly Software Failure:

This article uses the macro assembler A51 from the Keil C51 software package as the compiler. When writing assembly language for microcontrollers, it is important to pay attention to specific syntax. For detailed information, please refer to relevant reference books. Syntax errors can lead to assembly failures. Common assembly errors include:

1. Duplicate Labels:

This often occurs when copying and pasting code without modifying labels, resulting in multiple identical labels, which are not allowed.

2. Punctuation Entered in Full-width:

Assembly programs require punctuation to be in half-width; otherwise, assembly will fail. You can switch to half-width mode when entering punctuation such as:,; or enter punctuation while in uppercase, which is also an easily made and hard-to-detect error.

3. Omitting 0 Before the Value #FFH:

According to the requirement, a 0 should be added before a-f, written as #0FFH.

4. Confusing the Letter O and the Number 0:

Sometimes these two characters look identical; be careful!

5. Omitting ‘:’ After the Label:

6. Using Special Characters in Labels:

Labels cannot use mnemonic instructions, pseudo-instructions, special function register names, and symbols like ‘#’, ‘@’ used in the 8051 instruction system. The length should be between 2 and 6 characters, and the first letter must be an English letter. For example, characters like T1, T2, A, B have specific meanings and cannot be used as labels.

7. AJMP Jump Exceeds 2K Address:

AJMP is a short jump command and has a 2K address range limitation.

8. Exceeding Address Range:

JB P3.2,EXIT jumps outside the range of -128 to 127 addresses. This is the most common error! Your program might compile successfully, but after adding a piece of code, it prompts an error. You can convert

JB P3.2,EXIT

to

JNB P3.2,LD01

AJMP EXIT

LD01: AJMP EXIT

9. Confusing the Letter I and the Number 1:

This is a common issue for careless programmers.

10. Creating Nonexistent Assembly Language Instructions:

Such innovations are not welcome during programming; these instructions are not supported by the assembly program and not recognized by the chip.

11. It is best to use half-width for symbols ‘:’ and ‘;’.

2. Program Errors:

1. Duplicate Register Calls:

For example, if R4=5 is set in the main program, indicating that the main program will execute 5 times, and one of the delay subroutines also uses R4, it can cause R4’s value to become disordered, resulting in the program not executing correctly.

2. Unfamiliarity with Hardware:

Microcontrollers typically use pull-down outputs, often driving peripherals to work with port output 0, which is opposite to common positive logic, making it easy to confuse.

Advice for Writing Assembly Language:

Develop good programming habits, such as aligning labels, parameters, and comments; this makes the code aesthetically pleasing and less prone to errors. Labels should preferably use meaningful English, making them more intuitive, and comments should be as detailed and accurate as possible for future reference and for calling as subroutine modules in other programs. Also, pay attention to accumulating typical program modules; even the most complex programs consist of small program modules. In the beginner stage, practice writing typical programs such as delay subroutines, lookup table subroutines, and key debounce subroutines to create a lasting impression for future reference.

3. Common Symbols and Their Meanings in the Instruction System:

(1) Ri: Working registers 0 and 1, i=0 or 1. That is, R0~R1.

(2) Rn: Working registers R0~R7, n=0~7.

(3) @Ri: Indirect addressing of 8-bit memory units 00H~FFH by register Ri.

(4) direct: 8-bit direct address, which can be internal RAM 00H~7FH or SFR 80H~FFH.

(5) #data: 8-bit immediate number.

(6) #data16: 16-bit immediate number.

(7) addr16: 16-bit destination address used for LCALL and LJMP instructions, capable of calling or transferring to any location in 64K program memory space.

(8) addr11: 11-bit destination address used for ACALL and AJMP instructions, which can be called or transferred within the 2K byte page of the address of the next instruction.

(9) rel: Signed 8-bit offset address used for SJMP and all conditional transfer instructions. Its range is -128 to +127 bytes relative to the 1st byte address of the next instruction.

(10) DPTR: Data pointer, can be used as a 16-bit address register.

(11) bit: Bit address. Bit-addressable in internal RAM and addressable bits in SFR.

(12) A: Accumulator Acc.

(13) B: General-purpose register, mainly used in multiplication MUL and division DIV instructions.

(14) Cy: Carry flag or accumulator in Boolean processor.

(15) @: Prefix for indirect registers or base address registers. For example, @Ri, @DPTR, @A+PC, @A+DPTR.

Common Errors in Writing Assembly Language

Leave a Comment