Assembly language programming
Assembly language is a machine-oriented programming language represented by mnemonics (English abbreviations). Each mnemonic instruction has a corresponding machine code, that is, assembly language is a symbolic representation of machine language.
format
Assembly language statements are generally composed of 4 fields, namely label, opcode, operand and comment, which should be separated by a separator. Common separators are space "", colon ":" and semicolon ";", And there can be more than one space.
[标号:] 操作码 [操作数] [;注释]
In the above format, the items in [] are optional, where the label and the opcode are separated by ":", the opcode and the operand are separated by a space, and the operand and the comment are separated by ";", there are When there are multiple operands, the operand and the operand are separated by ",".
-
The label specification is as follows:
① The label is composed of 1 to 8 ASCII characters, and the first character must be a letter.
② The same label can only be defined once in a program, and cannot be defined repeatedly.
③ You cannot use symbols already defined in assembly language as labels, such as instruction mnemonics, pseudo-instructions, and symbol names of registers.
④ The existence of the label depends on whether other statements in the program access the statement. If there is no other statement access, no label is needed before the statement. -
Opcode field The
opcode is a mnemonic of the instruction, which indicates the nature of the instruction and is used to indicate what kind of operation the CPU performs. Opcodes are the only part of assembly language instructions that cannot be left blank. -
Operand field The
operand field is used to store the operand or address of the instruction.- Namely: #data and # data16.
- Direct address: direct, such as 30H; symbolic address defined by the pseudo-instruction, such as SUM; expression, such as SUM + 1, the name of the special function register, etc.
-
Comment field A
comment is a description of a statement or a program segment for the convenience of the reader's reading and understanding. It is not translated into machine code during assembly, and the machine is not executed.
Pseudo-instruction
Pseudo-instructions are used in the "machine assembly" process to control the assembly process or assign values to symbols and labels. These instructions do not belong to the instructions in the instruction system, and no machine code is generated during assembly, so they are called "pseudo-instructions".
-
ORG (Assembly Start Address Directive)
ORG is used to define the start address of the target program after assembly. The format is as follows:
[label:] ORG addr16
For example: ORG 2000H
START: MOV A, # 34H
ORG specifies the address of label START as 2000H, which means that the program should be stored from 2000H. In an assembly language source program, you can use the ORG command multiple times to specify the starting address of different program segments. The address should generally be from small to large and cannot be repeated. -
END (End of Assembly Directive)
END is used to indicate the end of the assembly language source program. It can only appear at the end of the program, and there is only one. The instruction format is as follows:
[label:] END -
EQU (assignment directive)
EQU is used to assign values to labels that appear in a program. The format is as follows:
Character name EQU number or assembly symbol
When the machine assembles, the assembly language will automatically assign the number or assembly symbol after EQU to the character name on the left. For example,
AA EQU R1; AA is equivalent to R1
K1 EQU 40H; K1 means 40H
must use the instruction to pay attention to the following points:
(1) The character name in the instruction is not a label that appears in the transfer instruction, but appears in the operand Character name.
(2) The character name in the EQU directive must be defined before use.
(3) The character name defined by EQU cannot appear in the expression, for example, the statement MOV A, A10 + 1 is wrong. -
DATA (Data Address Assignment Instruction)
DATA assigns specified character names to data addresses or code addresses. The format is as follows:
label name DATA expression -
B, DW, DS (define byte, word, space pseudo-instruction)
DB: Start from the specified address unit and store several bytes.
DW: Starting from the specified address unit, store several words (16-bit binary number, high 8 bits in the front, low 8 bits in the back).
DS: Starting from the designated address unit, reserve several units for backup. The instruction format is as follows:
[label:] DB byte constant; several items separated by commas, each item is a byte)
[label:] DW word constant; several items separated by commas, each item Is a word)
[label:] DS expression; its value indicates the number of reserved units)
分析下段程序。
ORG 2000H
DS 08H
DB 30H,8AH,10,‘B’
DW 54H,1F80H
解:该程序的DS伪指令定义8个存储单元(2000H~2007H)备用;DB伪指令定义了从地址2008H开始的4个单元的内容;DW伪指令定义了后续4个单元的内容。结果如下:
(2008H) = 30H, (2009H) = 8AH
(200AH) = 0AH, (200BH) = 42H
(200CH) = 00H, (200DH) = 54H
(200EH) = 1FH, (200FH) = 80H
- BIT (Bit Address Symbol Directive)
BIT is used to assign bit addresses to character names. The format is:
character name BIT bit address
For example:
KEY BIT P1.0; assign the bit address of P1.0 to the symbol name KEY
ST BIT 0D7H; define the bit address as D7H as the symbol name
Note: Bit address can be both It is an absolute address or a symbolic address. In addition, the "symbol name" defined by BIT cannot be redefined and changed once defined.
Assembler
step
General steps of
assembly language programming The assembly language programming can be roughly divided into the following steps:
(1) Clear design requirements.
(2) Determine the algorithm.
(3) Draw the program flow chart.
(4) Write the source program according to the program flow chart.
(5) On-board debugging.
(6) Optimization procedures.
structure
#####order
The on-chip RAM 20H single in BCD code compression element is split into two ACSII code stored 21H, 22H unit. There are 21H cells in the lower 4 bits and 22H cells in the upper 4 bits.
ORG 2000H
MOV A,20H
MOV B,#10H ;除以10H
DIV AB
ORL B,#30H ;低4位BCD码转换为ASCII码
MOV 21H,B
ORL A,#30H ;高4位BCD码转换为ASCII码
MOV 22H,A
END
##### Branch
求单字节有符号数的二进制补码。
GCMPT:JNB ACC.7,RETURN ;(A) > 0,不需转换
MOV C,ACC.7 ;符号位保存
CPL A ;(A)求反,加1
ADD A,#1
MOV ACC.7,C ;符号位回存A的最高位
RETURN:RET
cycle
设有100个单字节数。连续存放在内部M单元开始的数据存储器中,且总和也为单字节数,存放到N单元。试编写求这100个数之和的程序。
参考程序:
N EQU 30H
M EQU 31H
ORG 2020H
MOV A,#00H
MOV R0,#M
MOV R1,#64H
LOOP:ADD A,@R0
INC R0
DJNZ R1,LOOP
MOV N,A
SJMP $
END
On-site protection and restoration
Since the calling program and the subprogram are often compiled separately , the registers or storage units used by them often conflict. In order to avoid modifying the original contents of the register / storage unit used in the calling program during the running of the subprogram, the contents of the corresponding register / storage unit should be saved when the subprogram starts to run. The contents of these registers / storage units are restored to ensure the normal operation of the calling program.
在主程序中实现
现场保护与恢复工作在主程序中实现,特点是结构灵活。例如,
PUSH PSW ;保护现场(含当前工作寄存器组号)
PUSH ACC
PUSH B
MOV PSW,#10H ;切换当前工作寄存器组
LCALL addr16 ;子程序调用
POP B ;恢复现场
POP ACC
POP PSW ;含当前工作寄存器组切换
在子程序中实现
现场保护与恢复工作在子程序中实现,特点是程序结构规范、清晰。例如,
SUB1: PUSH PSW ;保护现场(含当前工作寄存器组号)
PUSH ACC
PUSH B
MOV PSW,#10H ;切换当前工作寄存器组
…
POP B ;恢复现场
POP ACC
POP PSW ;内含当前工作寄存器组切换
RET
###exercise
- What are the languages used for programming? What are their characteristics?
Answer: The programming languages of 51 series MCU can be divided into assembly language and high-level language (such as C language), their respective characteristics are as follows:
name | Features | Disadvantages | advantage | Applications |
---|---|---|---|---|
Assembly language | Write instructions with symbols (use mnemonics for opcodes, special symbols for operands) | The machine cannot be directly identified; the programmer must understand the structure and command system of the machine, which is not easy to promote and popularize; it cannot be transplanted, and it is not universal | It is easier for people to recognize, remember and read and write | Real-time control system |
High level language | Sentence programming based on English | The machine cannot be directly identified; long execution time | Easy to promote and communicate; does not depend on the machine, has versatility | Scientific computing and data processing |
- Let the definition of constants and data labels be:
ORG 2000H
DAT1: DB 1, 2, 3, 4, 5
DAT2: DB ‘ABCD’
DAT3: DW 1200H,-2
TAB: DW DAT1, DAT3
补充:A为65,也就是十六进制的41H
(1) Draw the storage format of the above data or address.
(2) Write the address of each label.
Answer: (1)
address | Content (H) |
---|---|
2000H | 01 |
02 | |
03 | |
04 | |
05 | |
2005H | 41 |
42 | |
43 | |
44 | |
2009H | 12 |
00 | |
FF | |
FE | |
200DH | 20 |
00 | |
20 | |
09 |
(2)DAT1=2000H; DAT2=2005H; DAT3=2009H; TAB =200DH
- Try to write a program to find the maximum number of unsigned numbers in the on-chip RAM 30H ~ 5FH unit, and store the result in the 60H unit.
answer:
ORG 0000H
MOV R0, #30H
MOV R7, #2FH
MOV A, @R0
AGAIN: INC R0
MOV B, A
CLR C
SUBB A, @R0
JNC L1
MOV A, @R0
AJMP L2
L1: MOV A, B
L2: DJNZ R7, AGAIN
MOV 60H, A
SJMP $
END
6. Try to write a program to count the number of occurrences of 55H in the 20H to 5FH units of the on-chip RAM and send the statistical results to the 60H unit.
answer:
TAB EQU 20H
ORG 0000H
START: MOV 60H, #0
MOV R0, #TAB
LOOP2: MOV A, #55H
CLR C
SUBB A, @R0
JNZ NEXT
INC 60H
NEXT: INC R0
CJNE R0, #60H, LOOP2
SJMP $
END
7. Write a program to clear all 3000H ~ 30FFH units in the off-chip data storage area.
answer:
ORG 0000H
START: MOV A, #0
MOV R7, #0
MOV DPTR, #3000H
LOOP: MOVX @DPTR, A
INC DPTR
DJNZ R7, LOOP
SJMP $
END