Embedded Systems 简明教程
Embedded Systems - Assembly Language
汇编语言被开发用于为机器级代码指令提供 mnemonics 或符号。汇编语言程序由助记符组成,因此它们应该被翻译成机器代码。负责此转换的程序称为 assembler 。汇编语言通常被称为低级语言,因为它直接与CPU的内部结构一起工作。为了用汇编语言进行编程,程序员必须了解CPU的所有寄存器。
Assembly languages were developed to provide mnemonics or symbols for the machine level code instructions. Assembly language programs consist of mnemonics, thus they should be translated into machine code. A program that is responsible for this conversion is known as assembler. Assembly language is often termed as a low-level language because it directly works with the internal structure of the CPU. To program in assembly language, a programmer must know all the registers of the CPU.
不同的编程语言,如C、C++、Java和各种其他语言,被称为高级语言,因为它们不处理CPU的内部细节。相反,汇编器用于将汇编语言程序翻译成机器代码(有时也称为 object code 或 opcode )。类似地,编译器将高级语言翻译成机器代码。例如,要编写C语言程序,必须使用C编译器将程序翻译成机器语言。
Different programming languages such as C, C++, Java and various other languages are called high-level languages because they do not deal with the internal details of a CPU. In contrast, an assembler is used to translate an assembly language program into machine code (sometimes also called object code or opcode). Similarly, a compiler translates a high-level language into machine code. For example, to write a program in C language, one must use a C compiler to translate the program into machine language.
Structure of Assembly Language
汇编语言程序是一系列语句,它们要么是诸如ADD和MOV的汇编语言指令,要么是称为 directives 的语句。
An assembly language program is a series of statements, which are either assembly language instructions such as ADD and MOV, or statements called directives.
instruction 告诉CPU要做什么,而 directive (也称为 pseudo-instructions )则向汇编器发出指令。例如,ADD和MOV指令是CPU运行的命令,而ORG和END是汇编器指令。当使用ORG指令时,汇编器将操作码放在存储器位置0,而END则指示源代码的末尾。程序语言指令包含以下四个字段−
An instruction tells the CPU what to do, while a directive (also called pseudo-instructions) gives instruction to the assembler. For example, ADD and MOV instructions are commands which the CPU runs, while ORG and END are assembler directives. The assembler places the opcode to the memory location 0 when the ORG directive is used, while END indicates to the end of the source code. A program language instruction consists of the following four fields −
[ label: ] mnemonics [ operands ] [;comment ]
方括号([ ])表示该字段是可选的。
A square bracket ( [ ] ) indicates that the field is optional.
-
The label field allows the program to refer to a line of code by name. The label fields cannot exceed a certain number of characters.
-
The mnemonics and operands fields together perform the real work of the program and accomplish the tasks. Statements like ADD A , C & MOV C, #68 where ADD and MOV are the mnemonics, which produce opcodes ; "A, C" and "C, #68" are operands. These two fields could contain directives. Directives do not generate machine code and are used only by the assembler, whereas instructions are translated into machine code for the CPU to execute.
1.0000 ORG 0H ;start (origin) at location 0
2 0000 7D25 MOV R5,#25H ;load 25H into R5
3.0002 7F34 MOV R7,#34H ;load 34H into R7
4.0004 7400 MOV A,#0 ;load 0 into A
5.0006 2D ADD A,R5 ;add contents of R5 to A
6.0007 2F ADD A,R7 ;add contents of R7 to A
7.0008 2412 ADD A,#12H ;add to A value 12 H
8.000A 80FE HERE: SJMP HERE ;stay in this loop
9.000C END ;end of asm source file
-
The comment field begins with a semicolon which is a comment indicator.
-
Notice the Label "HERE" in the program. Any label which refers to an instruction should be followed by a colon.
Assembling and Running an 8051 Program
在此,我们将讨论汇编语言的基本形式。创建、汇编和运行汇编语言程序的步骤如下:
Here we will discuss about the basic form of an assembly language. The steps to create, assemble, and run an assembly language program are as follows −
-
First, we use an editor to type in a program similar to the above program. Editors like MS-DOS EDIT program that comes with all Microsoft operating systems can be used to create or edit a program. The Editor must be able to produce an ASCII file. The "asm" extension for the source file is used by an assembler in the next step.
-
The "asm" source file contains the program code created in Step 1. It is fed to an 8051 assembler. The assembler then converts the assembly language instructions into machine code instructions and produces an .obj file (object file) and a .lst file (list file). It is also called as a source file, that’s why some assemblers require that this file have the "src" extensions. The "lst" file is optional. It is very useful to the program because it lists all the opcodes and addresses as well as errors that the assemblers detected.
-
Assemblers require a third step called linking. The link program takes one or more object files and produces an absolute object file with the extension "abs".
-
Next, the "abs" file is fed to a program called "OH" (object to hex converter), which creates a file with the extension "hex" that is ready to burn in to the ROM.
Data Type
8051 微控制器包含一个 8 位的单数据类型,每个寄存器的大小也都是 8 位。程序员必须将大于 8 位的数据(00 到 FFH,或十进制的 255)分解,以便 CPU 能够处理它。
The 8051 microcontroller contains a single data type of 8-bits, and each register is also of 8-bits size. The programmer has to break down data larger than 8-bits (00 to FFH, or to 255 in decimal) so that it can be processed by the CPU.
DB (Define Byte)
DB 指令是汇编器中最广泛使用的数据指令。它用于定义 8 位数据。它还可用于定义十进制、二进制、十六进制或 ASCII 格式的数据。对于十进制,“D”位于十进制数字后是可选的,但对于“B”(二进制)和“Hl”(十六进制)来说,它则是必需的。
The DB directive is the most widely used data directive in the assembler. It is used to define the 8-bit data. It can also be used to define decimal, binary, hex, or ASCII formats data. For decimal, the "D" after the decimal number is optional, but it is required for "B" (binary) and "Hl" (hexadecimal).
要指示 ASCII,只需将字符放在引号中(“这样”)。汇编器会自动为数字/字符生成 ASCII 代码。DB 指令是唯一可用于定义大于两个字符的 ASCII 字符串的指令;因此,它应用于所有 ASCII 数据定义。下面给出了一些 DB 示例:
To indicate ASCII, simply place the characters in quotation marks ('like this'). The assembler generates ASCII code for the numbers/characters automatically. The DB directive is the only directive that can be used to define ASCII strings larger than two characters; therefore, it should be used for all the ASCII data definitions. Some examples of DB are given below −
ORG 500H
DATA1: DB 28 ;DECIMAL (1C in hex)
DATA2: DB 00110101B ;BINARY (35 in hex)
DATA3: DB 39H ;HEX
ORG 510H
DATA4: DB "2591" ;ASCII NUMBERS
ORG 520H
DATA6: DA "MY NAME IS Michael" ;ASCII CHARACTERS
可以在 ASCII 字符串周围使用单引号或双引号。DB 还用于在字节大小块中分配内存。
Either single or double quotes can be used around ASCII strings. DB is also used to allocate memory in byte-sized chunks.
Assembler Directives
8051 的一些指令如下:
Some of the directives of 8051 are as follows −
-
ORG (origin) − The origin directive is used to indicate the beginning of the address. It takes the numbers in hexa or decimal format. If H is provided after the number, the number is treated as hexa, otherwise decimal. The assembler converts the decimal number to hexa.
-
EQU (equate) − It is used to define a constant without occupying a memory location. EQU associates a constant value with a data label so that the label appears in the program, its constant value will be substituted for the label. While executing the instruction "MOV R3, #COUNT", the register R3 will be loaded with the value 25 (notice the # sign). The advantage of using EQU is that the programmer can change it once and the assembler will change all of its occurrences; the programmer does not have to search the entire program.
-
END directive − It indicates the end of the source (asm) file. The END directive is the last line of the program; anything after the END directive is ignored by the assembler.
Labels in Assembly Language
汇编语言中的所有标签都必须遵守以下规则:
All the labels in assembly language must follow the rules given below −
-
Each label name must be unique. The names used for labels in assembly language programming consist of alphabetic letters in both uppercase and lowercase, number 0 through 9, and special characters such as question mark (?), period (.), at the rate @, underscore (_), and dollar($).
-
The first character should be in alphabetical character; it cannot be a number.
-
Reserved words cannot be used as a label in the program. For example, ADD and MOV words are the reserved words, since they are instruction mnemonics.