Basic arm assembler knowledge
1.Basic Components of Assembler
In an arm assembly language program, the program organizes code in blocks. Segments are relatively independent commands or code sequences with specific names. The types of CIDR blocks include code segments, data segments, and common CIDR blocks. Code segments are executed to store the data required for code running. Common CIDR blocks do not contain user code and data, all common segments share one space. The section is defined by the "area" pseudo operation and describes the relevant attributes, as shown in figure
Code segment Definition
Area init, code, readonly
...
Data Segment Definition
Area stack1, Data, readwrite, noinit, align = 3
......
And so on
An assembler should have at least one code segment, which can have zero or multiple data segments. In terms of format, an assembler requires at least one entry (for the specific content of the entry, refer to the pseudo operator entry), and at the end of the Assembly source file, write end to indicate the end Of the source file.
For example, a basic assembly source program
Area init, code, readonly; defines a code segment
Entry; mark the entry point of the program
Start LDR r0, 0x3ff5000; the start number can be required or not
LDR R1, 0xff
STR R1, [R0]
LDR r0, = 0x3ff5000
LDR R1, 0x01
STR R1, [R0]
......
End; end pseudo operation indicates that the source file ends.
When the assembler program is long, it can be divided into multiple code segments and multiple data segments. When multiple segments are compiled and linked, an executable image file is formed. An executable image file consists of the following parts:
One or more code segments. The code segment attribute is read-only (read-only data is also stored in the code segment? RO)
Zero or multiple data segments of initialization data, which can be read and written (store initialized variable data, RW)
Zero or multiple data segments that do not contain initialization data can be read and written (all uninitialized variables, I .e. zi)
The linker arranges the corresponding positions of each segment in the memory according to the system default or user-defined rules. Therefore, the relative positions between the middle segments of the source program and those in the executable image files are generally not the same.
2.Notes for Assembly Statements
Assembly statement format
[Label] operation, [operand], [; Comment]
The label must start with a line.
Operation includes commands, pseudo operations, macro commands, or pseudo commands. Each operation prompt must be in uppercase or lowercase. There must be spaces before the write operation enable.
Operand indicates the operation object. constants, variables, labels, registers, or expressions can be used. Different objects must be separated by commas.
Example:
Area ex2, code, readonly; there must be spaces before the operation enable
Gbla data
Data seta, 0x20; no space before data variable name
Add r0, R1, R2
Add r0, R1, R2
Add r0, R1, R2
Add r0, R1, R2; the lower-case register is correct, and the command mnemonic is case-insensitive.
3.Common symbols
In assembly languages, various symbols are often used to represent variables, constants, and addresses.
Definition of variables: use pseudo operations such as gbla, gbll, and GBLS to define global numeric variables, logical variables, and character variables; LCIA, lcll, and lcls define local numerical variables, logical variables, and character variables. The corresponding variables use seta, SETl, and sets to assign values. Note that the length of a string cannot exceed 512 bytes.
Example:
Gbla data
Data Seta 0x20
Lcls str1
Str1 sets "pen"
Lcll LC
LC SETl {true}
A constant is the amount that cannot be changed during running. Arm supports numerical constants, logical constants, and string constants. Use equ in the Assembly to define a numerical constant, as shown in figure
Test equ 10; the value of test is 20.
ADDR equ 0x55, code32;
For details about how to use equ, refer to the pseudo operation equ.
A numeric constant is generally an integer of 32. It can be in decimal or hexadecimal notation (n = 2 ~ 9) for example, 8_247 is an octal number.
4.Common pseudo Operators
Symbol-defined pseudo operation
Gbla, gbll, gstrap
Ltls, lcll, and lcls
Seta, SETl, and sets
Rlist
Rlist is used to define the general register list name. The name defined by this pseudo operation can be used in the arm command LDM/STM. In LDM/STM, the register order in the access list is the register number order from low to high. For example
Reglist rlist {r0-r5, R8, R10}; Define register list name as reglist
Use in programs
Stmfd SP !, Reglist; store the list to the stack
Ldmia R5, reglist; load list
Data Definition pseudo operations
DCB allocates and initializes a continuous byte storage unit.
Dcw (dcwu) allocates and initializes a continuous half-word storage unit.
DCD (DCDU) allocates a continuous word storage unit and initializes it.
Dcdo, DCI, dcq (dcqu)
DCFS (dcfsu) allocates a continuous word storage unit for a single precision Floating Point Number and initializes it.
Dcfd (dcfdu) allocates a continuous word storage unit for a double-precision floating point number and initializes it.
Space allocates a continuous storage unit
Field, MAP, ltorg
For example:
Str dcb "this is a test"; allocate a continuous byte storage unit and initialize
Data dcw, 3; allocate a continuous half-word storage unit and initialize it
Data DCD, 6; allocate a continuous word storage unit and initialize
Fdata DCFS 2e5,-5e-7; allocate a continuous word storage unit and initialize it to the specified single precision number.
Dspce space 100; allocates 100 bytes of storage units and initializes them to 0.
Control pseudo operations
If else endif
While Wend
Macro mend; mexit
False operations of information reporting
Assert
Info
OPT
Other common pseudo operations
Area align code16/code32 entry end equ Export (Globle) Import extern get (include) incbin rn rout
Area
Format: area segment name attribute 1, attribute 2 ,......
Common attributes include:
Code: defines a code segment. The default value is readonly.
Data: defines the data segment. The default value is readwrite.
Readonly: specify this segment as read-only
Readwrite: specify this segment as read/write
Align: The expression is used as the align expression. By default, the code segments and data segments of ELF (executable link files) are aligned by words. The value range of the expression is 0 ~ 31, the corresponding alignment is a power of 2.
Common: defines a common segment that does not contain any user's code or data. Common segments with the same name in each source file share the same storage unit.
Align
Format: Align [expression [, offset]
The align pseudo operation can add the padding byte to make the current position conform to a certain align.
Example:
......
Data1 DCB "strin"; address alignment cannot be guaranteed after Definition
Align 4; make sure the current address is 4-byte aligned
......
Example:
Area cache, code, align = 3; specify the 23 = 8-byte alignment for the command in this code segment
......
MoV PC, LR; after the program jumps, it is 4-byte alignment. After the return, it needs to continue 8-byte alignment.
Align 8; the current position is 8 bytes aligned again
......
Note the difference between using align in area and using align separately. The format and calculation method are different.
Entry
Specifies the entry point of the assembler. A program can be composed of one or more source files. A source file consists of one or more program segments. A program has at least one entry point and multiple entry points, but only one entry can be found in a single source file. When multiple entries exist, the real entry point of the program is specified by the linker. During compilation, the program is connected according to the program entry point. When there is only one entry point, the Compilation Program defines the address of this entry point as the starting point of the program after the system is reset.
End
Write at the end of the source file, indicating the end Of the source program.
Export
Format: Export number [, weak]
Declare a global label that can be referenced in other files. Weak indicates that when other labels with the same name are met, other labels take precedence.
Area init, code, readonly
Export stest
......
End
Import
Format: Import Label [, weak]
Indicates that the referenced label is in another source file, but must be referenced in the current file. Weak indicates that no error is reported when this label cannot be found. Generally, the value of this label is set to 0. If B or BL is used, this command is set to NOP.
Unlike extern, this label is added to the symbol table of the current source file regardless of whether the current file references this label.
Area init, code, readonly
Import main;
......
End
Extern
Similar to import, the label is not added to the symbol table of the current source file if the current file does not reference this label.
Get(OrInclude)
Include a source file to the current source file and compile it at the current location.
Area init, code, readonly
Get a1.s
Get C:/a2.s
......
End
Incbin
When a target file or data file is included in the current file, the file content is stored in the current location, and the compiler does not compile the file content.
Area init, code, readonly
Get a1.s; contains a1.s and compiles a1.s.
Incbin C:/d.txt; contains d.txt. NO content is compiled.
Get a2.s; contains a2.s and compiles the content
End
Rn
Define an alias for a register.
Temp RN, R0; define an alias temp for R0