ADS assembly language 1

Source: Internet
Author: User
Tags define local

Arm compilers generally support assembly language programming, C/C ++ programming, and mixed programming. This chapter introduces some basic concepts of arm program design, such as pseudo commands of arm assembly language, statement format of assembly language, and program structure of assembly language, this article also introduces the mixed programming of C/C ++ and assembly languages.

Main content of this chapter:
-Pseudo commands supported by the arm Compiler
-Statement format in assembly language
-Assembly language program structure
-Related program examples
4.1 pseudo commands supported by arm Assembler
In arm assembly language programs, there are some special command mnemonic, which are different from the command system mnemonic and have no corresponding operation code. Generally, these special command mnemonic are called pseudo commands, the operations they complete are called pseudo operations. The role of pseudo-commands in the source program is to make various preparations for the completion of the assembly program. These pseudo-commands only play a role in the assembly process. Once the Assembly is completed, the mission of the pseudo-commands will be completed.

In arm assembler, there are several pseudocommands: symbolic definition pseudocommands, data definition pseudocommands, assembly control pseudocommands, macro commands, and other pseudocommands.
4.1.1 symbolic definition Directive
The symbolic definition pseudocommand is used to define variables in arm assembler programs, assign values to variables, and define register aliases. There are several common symbolic definition commands:
-Gbla, gbll, and gsung are used to define global variables.
-This parameter is used to define local variables such as LCIA, lcll, and lcls.
-Seta, SETl, and sets used to assign values to variables.
-Define the name rlist for the general register list.
1. gbla, gbll, and GBLS
Syntax format:
Gbla (gbll or gbll) global variable name
The gbla, gbll, and GBLS pseudo commands are used to define global variables in an arm program and initialize them. Where:
The gbla directive defines a global numeric variable and initializes it to 0;
The gbll pseudo command is used to define a global logical variable and initialize it to F (false );
The gsf-pseudo command is used to define a global string variable and the initialization is empty;
Since the above three pseudo commands are used to define global variables, the variable names must be unique throughout the program.
Example:
Gbla test1; defines a global digital variable named test1.
Test1 Seta 0xaa; assign this variable to 0xaa
Gbll Test2; defines a Global Logical variable named Test2
Test2 SETl {true}; assign the variable to true
G‑test3; defines a global string variable named test3.
Test3 sets "testing"; assign the variable "testing"
2. LCIA, lcll, and lcls
Syntax format:
Local variable name of llinoleic (lcll or lcls)
The ltls, lcll, and lcls pseudo commands are used to define local variables in an arm program and initialize them. Where:
The llinoleic directive is used to define a local numeric variable and initialize it to 0;
The lcll pseudo command is used to define a local logical variable and initialize it to F (false );
The lcls pseudo command is used to define a local string variable and the initialization is empty;
The preceding three pseudo commands are used to declare local variables. The variable names must be unique within the scope of their function.
Example:
LCIA test4; declare a local numeric variable named test4
Test3 Seta 0xaa; assign the variable 0xaa
Lcll test5; declare a local logical variable named test5
Test4 SETl {true}; assign the variable to true
Lcls test6; defines a local string variable named test6
Test6 sets "testing"; assign the variable "testing"
3. seta, SETl, and sets
Syntax format:
Variable name Seta (SETl or sets) Expression
Pseudocommands seta, SETl, and sets are used to assign values to a defined global or local variable.
Seta pseudo-directive is used to assign values to a mathematical variable;
SETl is used to assign values to a logical variable;
Sets pseudo commands are used to assign values to a string variable;
The variable name is a defined global or local variable, and the expression is the value to be assigned to the variable.
Example:
LCIA test3; declare a local numeric variable named test3
Test3 Seta 0xaa; assign the variable 0xaa
Lcll test4; declare a local logical variable named test4
Test4 SETl {true}; assign the variable to true
4. rlist
Syntax format:
Name rlist {register list}
The rlist pseudocommand can be used to define a name for a general register list. The name defined by this pseudocommand can be used in the arm command LDM/STM. In the LDM/STM command, the register access order in the list is from low to high based on the Register number, and is irrelevant to the Register order in the list.

Example:
Reglist rlist {R0-R5, R8, R10}; the register list name is defined as reglist, which can be accessed through this name in the arm instruction LDM/STM.
4.1.2 data definition Directive
Data Definition pseudocommands are generally used to allocate storage units for specific data and initialize allocated storage units. Common Data Definition Commands include the following:
-DCB is used to allocate a continuous byte storage unit and initialize it with the specified data.
-Dcw (dcwu) is used to allocate a continuous half-character storage unit and initialize it with the specified data.
-DCD (DCDU) is used to allocate a continuous word storage unit and initialize it with the specified data.
-Dcfd (dcfdu) is used to allocate a continuous word storage unit for a double-precision floating point number and initialize it with the specified data.
-DCFS (dcfsu) is used to allocate a continuous word storage unit for Single-precision floating point numbers and initialize them with the specified data.
-Dcq (dcqu) is used to allocate an 8-byte continuous storage unit and initialize it with the specified data.
-Space is used to allocate a continuous storage unit.
-Map is used to define the first address of a structured memory table.
-Field is used to define the data fields of a structured memory table.
1. DCB
Syntax format:
DCB expression
The DCB pseudo command is used to allocate a continuous byte storage unit and initialize it with the expression specified in the pseudo command. The expression can be 0 ~ A number or string of 255. DCB can also be replaced by "=.
Example:
Str dcb "this is a test !" ; Allocate and initialize a continuous byte storage unit.
2. dcw (or dcwu)
Syntax format:
Dcw (or dcwu) Expression
The dcw (or dcwu) pseudo command is used to allocate a continuous half-character storage unit and initialize it with the expression specified in the pseudo command. The expression can be a program label or a numeric expression ..
The word storage units allocated with dcw are semi-aligned, while those allocated with dcwu are not strictly semi-aligned.
Example:
Est est dcw, 3; Allocate and initialize a continuous half-word storage unit.
3. DCD (or DCDU)
Syntax format:
DCD (or DCDU) Expression
The DCD (or DCDU) pseudo command is used to allocate a continuous word storage unit and initialize it with the expression specified in the pseudo command. The expression can be a program label or a numeric expression. DCD can also be replaced.
The word storage units allocated with DCD are word-aligned, while the word storage units allocated with DCDU are not strictly word-aligned.
Example:
Datatest DCD, 6; allocate a continuous word storage unit and initialize it.
4. dcfd (or dcfdu)
Syntax format:
Label dcfd (or dcfdu) Expression
The dcfd (or dcfdu) pseudo command is used to allocate a continuous word storage unit to a double-precision floating point number and initialize it with the expression specified in the pseudo command. Each double-precision floating point occupies two units.
The word storage units allocated with dcfd are word-aligned, while the word storage units allocated with dcfdu are not strictly word-aligned.
Example:
Fdatatest dcfd 2e115,-5e7; allocates a continuous word storage unit and initializes it to the specified double precision.
5. DCFS (or dcfsu)
Syntax format:
DCFS (or dcfsu) Expression
The DCFS (or dcfsu) pseudo command is used to allocate a continuous word storage unit to a single-precision floating point number and initialize it with the expression specified in the pseudo command. Each single-precision floating point occupies one cell.
The word storage units allocated with DCFS are word-aligned, while the word storage units allocated with dcfsu are not strictly word-aligned.
Example:
Fdatatest DCFS 2e5,-5e-7; allocates a continuous word storage unit and initializes it to the specified single precision number.
6. dcq (or dcqu)
Syntax format:
Dcq (or dcqu) Expression
The dcq (or dcqu) pseudo command is used to allocate an 8-byte continuous storage area and initialize it with the expression specified in the pseudo command.
The storage units allocated with dcq are word-aligned, while those allocated with dcqu are not strictly word-aligned.
Example:
Datatest dcq 100; allocates a continuous storage unit and initializes it to a specified value.
7. Space
Syntax format:
Label Space expression
Space pseudo commands are used to allocate a continuous storage area and initialize it to 0. The expression indicates the number of bytes to be allocated. Space can also be replaced by "%.
Example:
DataSpace space 100; allocate 100 bytes of storage units and initialize them to 0.
8. Map
Syntax format:
Map expression {, base address register}
The map directive is used to define the first address of a structured memory table. Map can also be replaced by "^.
The expression can be a program label or a mathematical expression. The base register is optional. When the base register option does not exist, the expression value is the first address of the memory table. When this option exists, the first address of the memory table is the sum of the expression value and base address register.

Map pseudo commands are usually used together with field pseudo commands to define a structured memory table.
Example:
Map 0x100, R0; defines the first address of the structured memory table as 0x100 + R0.
9. Filed
Syntax format:
Field expression
Field pseudoinstructions are used to define the data fields in a structured memory table. Filed can also be replaced.
The expression value is the number of bytes occupied by the current data field in the memory table.
Field pseudo commands are often used together with map pseudo commands to define a structured memory table. The map pseudo command defines the first address of the memory table. The field pseudo command defines each data field in the memory table and can specify a label for each data field for reference by other commands.

Note that the map and field commands are only used to define the data structure and do not actually allocate storage units.
Example:
Map 0x100; defines the first address of the structured memory table as 0x100.
A field 16; defines the length of a as 16 bytes and the position is 0x100
B Field 32; defines the length of B as 32 bytes and the position is 0x110
S field 256; defines the length of S as 256 bytes, and the position is 0x130
4.1.3 Assembly control (Assembly Control) Directive
The Assembly control pseudocommand is used to control the execution process of the assembly program. The commonly used assembly control pseudocommands include the following:
-If, else, endif
-While and Wend
-Macro and mend
-Mexit
1. If, else, endif
Syntax format:
If logical expression
Command sequence 1
Else
Command Sequence 2
Endif
If, else, And endif pseudo-commands determine whether to execute a sequence of commands Based on the condition. If the logical expression after if is true, execute command sequence 1; otherwise, execute command sequence 2. Here, else and command sequence 2 can be absent. In this case, when the logic expression after if is true, execute command sequence 1; otherwise, continue to execute subsequent commands.

If, else, And endif commands can be nested.
Example:
Gbll test; declares a Global Logical variable named test.
......
If test = true
Command sequence 1
Else
Command Sequence 2
Endif
2. While and Wend
Syntax format:
While logical expression
Command Sequence
Wend
The while and Wend commands determine whether to execute a command sequence cyclically Based on the condition. When the logic expression after while is true, the command sequence is executed. After the command sequence is executed, the value of the logic expression is judged. If the logic expression is true, the execution continues, until the value of the logical expression is false.

The while and Wend commands can be nested.
Example:
Gbla counter; declares a global mathematical variable named counter.
Counter Seta 3; the variable counter controls the number of cycles.
......
While counter <10
Command Sequence
Wend
3. Macro and mend
Syntax format:
$ Macro name $ parameter 1, $ parameter 2 ,......
Command Sequence
Mend
Macro and mend pseudo commands can define a piece of code as a whole, called macro commands, and then the code can be called multiple times through macro commands in the program. The $ label is replaced with the User-Defined symbol when the macro command is expanded,

Macro commands can use one or more parameters. When macro commands are expanded, these parameters are replaced by corresponding values.
The usage and functions of macro commands are similar to those of subprograms. subprograms can provide modular program design, save storage space, and increase the running speed. However, when using the subroutine structure, the field needs to be protected, which increases the overhead of the system. Therefore, when the code is short and many parameters need to be passed, macro commands can be used to replace the subroutine.

The sequence of commands contained between macro and mend is called the macro definition body. In the first line of the macro definition body, the prototype of the macro (including the macro name and required parameters) is declared ), then, the command sequence can be called through the macro name in the assembler. When the source program is compiled, the assembler expands macro calls, uses the instruction sequence in the macro definition to replace macro calls in the program, and passes the actual parameter values to the formal parameters in the macro definition.

Macro and mend can be nested.
4. mexit
Syntax format:
Mexit
Mexit is used to jump out of the macro definition.
4.1.4 other commonly used pseudocommands
Some other pseudocommands are often used in Assembler programs, including the following:
-Area
-Align
-Code16 and code32
-Entry
-End
-Equ
-Export (or global)
-Import
-Extern
-Get (or include)
-Incbin
-Rn
-Rout
1. Area
Syntax format:
Area segment name attribute 1, attribute 2 ,......
The area directive is used to define a code segment or data segment. If the segment name starts with a number, it must be enclosed by "|", for example, | segment test |.
Attribute fields indicate the relevant attributes of the code segment (or data segment). Multiple Attributes are separated by commas. Common attributes are as follows:
-Code attribute: used to define a code segment. The default value is readonly.
-Data Attribute: used to define data segments. The default value is readwrite.
-Readonly attribute: specifies that the current segment is read-only, and the default segment is readonly.
-Readwrite attribute: specifies that the current segment is readable and writable, and the default attribute of the data segment is readwrite.
-Align attribute: Used as an align expression. By default, the code segment and Data Segment of the elf (executable connection file) are aligned by word, and the value range of the expression is 0 ~ 31. The alignment is equal to the power of the 2nd expression.

-Common attribute: This attribute defines a common segment and does not contain any user code or data. Common segments with the same name in each source file share the same storage unit.
An assembly language program must contain at least one segment. When the program is too long, it can be divided into multiple segments and data segments.
Example:
Area init, code, readonly
Command Sequence
The pseudo command defines a code segment named init and the attribute is read-only.
2. Align
Syntax format:
Align {expression {, offset }}
The align directive can add the padding byte so that the current location can meet certain requirements. |. The value of the expression is used to specify the alignment mode. The possible value is the power of 2, such as 1, 2, 4, 8, and 16. If no expression is specified, the current position is aligned to the position of the next word. Offset is also a numeric expression. If this field is used, the alignment of the current position is: 2 expression power + offset.

Example:
Area init, code, readonly, alien = 3; specify that the subsequent commands are 8-byte aligned.
Command Sequence
End
3. code16 and code32
Syntax format:
Code16 (or code32)
The code16 pseudo-Directive notifies the compiler that the subsequent directive sequence is a 16-bit thumb directive.
The code32 pseudo-instruction notifies the compiler that the subsequent instruction sequence is a 32-bit arm instruction.
If the Assembly source program contains both the arm instruction and the thumb instruction, the code16 pseudoinstruction can be used to notify the compiler of the 16-bit thumb instruction sequence, code32 pseudo-instruction notifies the compiler of a 32-bit arm instruction sequence. Therefore, the two pseudo commands can be used in the code that uses the arm command and the thumb command for mixed programming, but note that they only notify the compiler of the type of the subsequent commands, the status of the processor cannot be switched.

Example:
Area init, code, readonly
......
Code32; Notify the compiler of 32-bit arm commands.
LDR r0, = next + 1; place the jump address in the register R0
BX R0; the program jumps to a new position for execution, and switches the processor to the thumb working state.
......
Code16; Notify the compiler of the 16-bit thumb command
Next LDR R3, = 0x3ff
......
End; end of the program
4. Entry
Syntax format:
Entry
The entry pseudo command is used to specify the entry point of the assembler. In a complete assembler, there must be at least one entry (or multiple entries. When there are multiple entries, the true entry point of the program is specified by the linker ), however, a single source file can have only one entry (or no entry ).

Example:
Area init, code, readonly
Entry; specifies the entry point of the application.
......
5. End
Syntax format:
End
The end pseudo command is used to notify the compiler that it has reached the end of the source program.
Example:
Area init, code, readonly
......
End; end of the application
6. equ
Syntax format:
Name equ expression {, type}
The equ pseudocommand is used to define an equivalent character name for constants and labels in a program, similar to # define in C. Equ can be replaced.
The name is the character name defined by the equ pseudo command. When the expression is a 32-bit constant, you can specify the Data Type of the expression. There are three types:
Code16, code32, and data
Example:
Test equ 50; defines the value of the label test as 50
ADDR equ 0x55, code32; defines the value of ADDR as 0x55, and this is a 32-bit arm command.
7. Export (or global)
Syntax format:
Export number {[Weak]}
The export directive is used to declare a global label in a program. This label can be referenced in other files. Export can be replaced by global. The label is case sensitive in the program. The [Weak] Option declares that other names with the same name are higher than those with the same name.

Example:
Area init, code, readonly
Export stest; declares a globally referenced stest.
......
End
8. Import
Syntax format:
Import label {[Weak]}
The import directive is used to notify the compiler of the label defined in other source files, but must be referenced in the current source file, and whether or not the label is referenced in the current source file, this label is added to the symbol table of the current source file.

The label is case-sensitive in the program. The [Weak] Option indicates that when no such label is defined for all source files, the compiler does not give an error message, in most cases, this label is set to 0. If this label is referenced by B or bl instructions, the B or bl commands are set to NOP.

Example:
Area init, code, readonly
Import main; notifies the compiler that the current file must reference the Mark Main, which is defined in other source files.
......
End
9. extern
Syntax format:
Extern {[Weak]}
The extern directive is used to notify the compiler of the label defined in other source files, but must be referenced in the current source file. If the current source file does not actually reference this label, this label will not be added to the symbol table of the current source file.

The label is case-sensitive in the program. The [Weak] Option indicates that when no such label is defined for all source files, the compiler does not give an error message, in most cases, this label is set to 0. If this label is referenced by B or bl instructions, the B or bl commands are set to NOP.

Example:
Area init, code, readonly
Extern main; notifies the compiler that the current file must reference the Mark Main, which is defined in other source files.
......
End
10. Get (or include)
Syntax format:
Get File Name
The get pseudocommand is used to include a source file to the current source file and compile the included source file at the current position. You can use include instead of get.
A common method in assembler is to define some macro commands in a source file, use equ to define the symbolic names of constants, and use map and field to define structured data types, then, use the get pseudo command to include the source file to other source files. The usage is similar to "include" in C.

The get pseudo-command can only be used to contain source files. The incbin pseudo-command must be used to contain the target file.
Example:
Area init, code, readonly
Get a1.s; notifies the compiler that the current source file contains the source file a1.s.
Ge t C:/a2.s; notifies the compiler that the current source file contains the source file C:/a2.s
......
End
11. incbin
Syntax format:
Incbin file name
The incbin directive is used to include a target file or data file into the current source file. The contained file is stored in the current file without any changes. The Compiler starts to process the file later.
Example:
Area init, code, readonly
Incbin a1.dat; notifies the compiler that the current source file contains the a1.dat file.
Incbin C:/a2.txt; notifies the compiler that the current source file contains the file C:/a2.txt
......
End
12. Rn
Syntax format:
Name rn expression
Rn pseudo commands are used to define an alias for a register. This method allows programmers to conveniently remember the register function. The name is the alias defined for the register, and the expression is the register encoding.
Example:
Temp rn R0; define an alias temp
Temp1 RN 1; define an alias temp1 for r1
13. rout
Syntax format:
{Name} rout
The rout directive defines the scope of a local variable. When this pseudo command is not used in the program, the scope of the local variable is the area, and after rout is used, the range of the local variable is between the current rout and the next rout.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.