Master the basic Arm assembly instructions, to write a simple arm assembler program, but also have to master the basic Arm Assembly pseudo operation (Directive). Now let's look at a simple assembler that calls the subroutine to complete the addition operation.
1. FileName: Test.s
2; function: realize two registers add together
3 area example,code,readonly; declaring code Snippets Example
4 ENTRY; Logo Program entry
5 CODE32 declaration 32-bit ARM instruction
6 START MOV R0, #0; set parameters
7 MOV R1, #10
8 LOOP BL add_sub; Calling subroutine Add_sub
9 B loop; jump to loop
Ten Add_sub
ADD R0,r0,r1; R0 = R0 + R1
MOV PC,LR; subroutine return
End of File
6th, 7 The parameters passed to the subroutine are stored in r0 and R1, and the 8th line invokes the subroutine. Line 11th, 12 is the code of the subroutine, completed 2 parameters added, and put the results after r0 back to the main program. 6th, 8, 10 lines of start, LOOP, Add_sub is the label, most often used to jump instruction B and BL, due to the assembly of grammatical requirements, the label must be below write (that is: not at the beginning of a space), otherwise the compiler will report an error. Accordingly, the assembler instruction must not below write.
It is obvious that the semicolon (;) is an annotation symbol in the assembler, which is equivalent to the//number of the C language. In addition, of course, we note that the 3rd, 4, 5, 13 lines are the symbols we have not learned, in fact, they are the focus of this article--arm assembly pseudo operation. First I'll explain the pseudo operations, and line 3rd defines a code snippet with a segment named example, and the property is read-only, which means that the 6--12 line is program code (not program data). Line 4th represents the entry point of the entire program (that is, the first instruction that the program runs) is the MOV instruction in line 6th (note 1). Line 5th indicates that the program code for line 6--12 is arm instruction, not thumb instruction. Line 13th indicates the end of the source code file, and the implication behind it is that if the programmer writes a compilation instruction after line 13th, the compiler will not even bother with the code, or compile them, and of course the code will not appear in the final executable file. Haha, so please be sure to remember that after the end pseudo operation to write code, it is useless work, write also white write. Do not disagree, according to experience, beginners will always make such a mistake.
Special Note: The meaning of line 9th is to let the program at the end of the run, the 9th line of the dead loop, so that the entire program is fixed in line 9th. This may be confusing: when you write an application, the end of the program ends, and the source code doesn't need to write a dead loop at all. But now you have to figure out: When you write an application, there are several things that the OS does for you after the end of the program. However, you do not have access to OS services now. If you don't write the 9th line of code yourself, then when you think the program is finished (line 8th is done), the CPU will not be smart enough to stop, it will continue to take the line 11th, continue to run, this is not what you want. In fact, this is not the worst, the worst of all, if your program does not have 11-13 lines, then the CPU to take out the instructions is in fact the random number in memory, but the CPU will be used as an instruction to execute, then, you think what will happen at this time. Haha, only God knows.
Of course, the pseudo operation is much more than these, and then we will introduce a number of pseudo operations often used.
Gbla: define global arithmetic variables (and, to be exact, global symbols), such as: Gbla TestVal
SETA: Assign values to global arithmetic symbols, such as: TestVal SETA 9;testval SETA testval + 1
DCD: Allocates word storage space for integers at compile time, for example: DCD 0x123456ab, this pseudo operation causes the compiler to allocate a word space in the final binary executable file and hold the integer in the space 0x123456ab
DCB: Allocates byte storage space for integers at compile time, for example: DCB ' A ', this pseudo operation will cause the compiler to allocate a byte space in the final binary executable file and store the ASCII code of character A in the space
If,else and endif: equivalent to C-language conditional compilation, for example:
Gbla TestVal
TestVal SETA 9
IF TestVal < 5
mov r0, #testval
ELSE
mov r1, #testval
ENDIF
IF:D Ef:testval
mov R2, #testval
ELSE
INFO 4, "You should define TestVal"
ENDIF
The result of the compiler compiling this segment of code is:
mov r1, #9
mov R2, #9
While and Wend : for example
Gbla TestVal
TestVal SETA 1
While TestVal <= 3
TestVal SETA TestVal + 1
mov r0, #testval
Wend
The result of the compiler compiling this segment of code is:
mov r0, #2
mov r0, #3
mov r0, #4
MACRO, Mend, and Mexit: the equivalent of a C-language macro replacement, for example:
MACRO
$label xmac $p 1, $p 2
; Code1
$label. Loop1
; Code2
BGE $label. Loop1
$label. Loop2
; code3
BL $p 1
BGT $label. Loop2
; Code4
ADR R0, $p 2
; code5
MEND
; Main program
ABC Xmac Subr1,de
The result of the compiler compiling this segment of code is:
; code1
Abc.loop1
; Code2
BGE ABC.LOOP1
Abc.loop2
; code3
BL SUBR1
BGT ABC.LOOP2
; code4
ADR R0, DE
; code5
EQU: The equivalent of the C language macro definition, for example: TestVal EQU 4
EXPORT: See the article "Atpcs and mixed Programming"
IMPORT: See the article "Atpcs and mixed Programming"
It is important to understand that the assembly pseudo operation is to provide some necessary information to the compiler to help the compiler to complete the program's compilation correctly. When the compilation completes, the Assembly pseudo operation completes its historical mission, it is impossible to leave even a little trace in the final executable program's binary code, and of course it is impossible to be "favored" by the CPU when the program is running. In short, remember that the assembly pseudo operation is for the compiler to look at, not to the CPU. This is the most important difference between assembly pseudo operation and assembly instruction.
In order to maintain the integrity of the content, the following is a more complete list of assembly pseudo operations. For a complete list, check the "Online Books" section of Ads yourself.
Symbolic definition (symbol definition) pseudo operation:
Data definition pseudo operation:
Assembly controls (Assembly control) pseudo operations:
Information Report (Reporting) pseudo operation:
Other (Miscellaneous) pseudo operations:
Note 1:entry is not meant to be so, the implication here is only entry side effects. As for its intention, subsequent articles will be explained