2. Simple implementation of assembly code
- The reason for using the assembly is simple, which is the efficient assembly code. When the machine starts, it takes advantage of the high efficiency of the Assembly, initializes the hardware, and provides the conditions for loading the kernel.
- There are two types of ARM assembly directives commonly used today:
*arm Standard assembly: for ARM company assembler, suitable for use under Windows platform.
*GNU assembly: Used with the assembler in the GNU cross-compiler tool chain, suitable for Linux platform development.
3. Assembler Framework: Note that the following operating environment is the Redhat 6.4 + Eclipse/C + + +CDT plugin.
Basic Framework for assembly code
?
Where the assembly is used, the activation code, the efficiency of the demand for efficient place.
Above is the framework of the boot code.
Build the framework below:
Start. S
. text
. Global _start
_start:
???? mov R2, #2
???? MOV R3, #3
Makefile:
All:start.o
???? Arm-linux-ld-tgboot.lds-o start.elf $^
%.O:%. S
???? ARM-LINUX-GCC-G-o [email protected] $^-C
Clean
???? RM *.o *.elf
The structure of a simple project's operation:
Linker script:
gboot.lds:
Output_arch (ARM)
ENTRY (_start)
SECTIONS {
????. = 0x50008000;
????
????. = ALIGN (4);
????. Text:
???? {
???? START.O (. Text)
???? * (. Text)
????}
?
????. = ALIGN (4);
????. Data:
???? {
???? * (. Data)
????}
????
????. = ALIGN (4);
???? Bss_start =.;
????. Bss:
???? {
???? * (. BSS)
????}
???? Bss_end =.;
}
Results of the operation:
The following is the implementation of the assembly code:
mov r1, #6
MOV r2,r1
MOV R3, #10
@mvn: Value of Inverse value
???? MVN r0, #4 @r0:4 inversion to 5
???? MVN R1, #0b111000
???? MVN r2,r1 @r2:0b111000
Instances of Sub:
?
Example of add:
Of course, we can also specify the value of the parameter at execution time: for example, we specify r0=44,r2=66.
After the run:
?
An instance of and;
BIC Example: The third number is the source code, the source is 1, corresponding to the bit clear 0, the source bit is 0, the corresponding bit unchanged.
From the above execution results see, r1 the highest bit, and the lowest two bits of 1, the corresponding source value is 1, so, was cleared 0, the middle four, corresponding to the source of four 0, remained unchanged 1010. So the final output is 0b101000.
?
- Compare directives
CMP instruction operation: The results of the comparison are not maintained and go back to affect the CPSR corresponding bits: N or z bits.
We can see that the r1-1=1 is positive, and the highest bit of the CPSR four bit 2=0010,n,z bit is 0.
Here R1-3=-1,CPSR the highest four-bit 8=1000, that is, the N-bit is set to 1, indicating that the result is negative.
Here the R1-2=0,CPSR is set to 1, which means two numbers are equal.
Operation of the TXT instruction: Test bit, bitwise with, result is 0, z bit is set to 1, result is not 0,z position is 0
The bitwise and after values are not 0, so the z-bit of the CPSR is not set to 1.
The bitwise and subsequent results are 0, so the z-position of the CPSR is set to 1. The result is a high four-bit value of 4.
- Jump command:
B Instruction:
In the above example, the GT represents greater than the time of the jump, 6>5 so jumps to the label branch1 execution. The add R3,R1,R2 is not executed.
Above, the conditions of the jump are not set, sequential execution, do not jump. But it executes sequentially, so adding a B end jumps to end, performing an empty operation.
?
?
?
?
?
?
Bl: Jump with a link:
Lr:
Disassembly code:
From the above to see the LR saved is the BL back to the next address, assign him to the PC pointer to jump.
?
- Shift Instructions:
- LSL Left Shift command:
11 shift left two bits: 1100
- Ror Loop Right Shift:
The loop moves right, and the lowest bit is 1, which is cycled to the highest level.
?
?
?
?
?
- The program status word Access directive.
In the GNU assembly, we do not allow the above instruction to operate access to our program Word State Register instructions, so we need to move their values out, then access the operation, modify, etc., and then to move in. So design to two instructions: MSR and Mrs Instructions. Move out Mrs and move back to MSR.
After the execution:
At last:
We just have to change the value of the CPSR in the above operation.
- Memory Access Instructions:
The above is the kernel inside the instructions, memory is through the memory to access the quality instructions.
LDR directives: saving memory to Registers
STR instruction: Registers are saved to memory.
We set the R1 to the address of the Development Board memory, for example, to create a monitoring address in memory:
Next look at the changes after running the above instructions: for example, our 0xFF has been saved to 0x50008000.
The following is LDR:
The value of R2 is the value that is stored in the r0 and is taken out.
?
?
The engineering code for this Assembly operation:
. text
. Global _start
_start:
[Email protected] and STR operation
???? mov r0, #0xff
???? STR R0,[R1]
???? LDR R2,[R1]
[Email protected] program Status Word register Access
???? Mrs R0,CPSR
???? Orr R0, #0b100
???? MSR Cpsr,r0
[Email protected]: Loop right Shift
???? mov r1, #0b11
???? MOV r1,r1,ror#1
[Email protected]: Move left
???? mov r1, #0b11
???? MOV r1,r1,lsl#2
[Email protected] directive: Jump with Link
???? BL func1
[Email protected] directive:
???? mov r1, #6
???? mov R2, #7
???? CMP R1,R2
???? BGT [email protected] means jump when it is greater than
???? Add R3,R1,R2
???? b End
FUNC1:
???? mov r1, #23
???? mov pc,[email protected] function returns, fixed format.
?
BRANCH1:
???? Sub R3,r1,r2
End
???? Nop
[Email protected] directive:
???? mov r1, #0b101
???? TST R1, #0b01
?
???? mov r1, #0b101
???? TST R1, #0b10
[Email protected] Instructions:
???? mov r1, #2
???? CMP R1, #1
?
???? mov r1, #2
???? CMP R1, #3
?
???? mov r1, #2
???? CMP R1, #2
[Email protected]: bit clear command
???? mov r1, #0b1101011
???? Bic R2,r1, #0b1000011
usage of [email protected]: logic and
???? mov r1, #5
???? and R2,r1, #0
?
???? mov r1, #5
???? and R2,r1, #1
[Email protected]: addition:
???? Add R1,R0,R2
[Email protected]: subtraction, note minuend cannot be an immediate number
???? mov R2, #4
???? Sub R0,r2, #2
???? mov r1, #3
???? Sub R3,r1,r0
[Email protected] This is the comment, MOV instruction
???? mov r1, #6
???? MOV r2,r1
???? MOV R3, #10
[Email protected]: value of value inversion
???? MVN r0, #4 @r0:4 inversion to 5
???? MVN R1, #0b111000
???? MVN r2,r1 @r2:0b111000
2. Simple implementation of assembly code