1. ARM Assembly Overview
1. Assembly usage location
Compilation location:
-- Startup code: Bootloader initializes the CPU and coprocessor, etc., and the C language operating environment is not established at this time. At this time, assembly language is used to perform initialization operations;
-- Efficiency requirements: high assembly efficiency. In the Linux kernel, assembly is required where there are special requirements for efficiency.
2. Compilation classification
(1) ARM standard assembly
ARM Standard Assembly Introduction:
-- Usage scenario: Applicable to ARM's assembler, suitable for use on Windows platform, such as ADS;
(2) GNU Assembler
GNU Assembler Introduction:
-- Usage scenario: Assembler for cross-compilation toolchain on Linux platform;
3. ARM assembler framework
ARM Assembly Framework:
-- ARM assembly framework example:
.section .data.section .bss .section .text .global _start _start:
-- Program entry: "_start:" is the entry of the assembler program, equivalent to main();
-- Mark the entry: Use ".global _start" to mark the program entry so that the outside world can recognize that this is the program entry;
-- Mark the code section: ".section .text" marks this as a code section;
-- Mark the bss segment: Use ".section .bss" to mark the bss segment. If there is no bss segment and data segment, start directly from .text;
4. Build an assembly development and debugging environment
(1) Assembler preparation
Program code:
-- Define code section: .text;
-- Define program entry: .globl _start;
-- Code example:
.text .globl _start _start: mov r1,#1 mov r2,#2 mov r3,#3
Makefile code:
-- Link elf format file: Set the program start position 6410 board is 0x50008000 address;
-- Specify the program start address in arm-linux-ld: -Ttext 50008000;
-- If you use a linker script to specify the address: Note that the third line specifies the program start address;
SECTIONS { . = 0x50008000; . = ALIGN(4); .text : { led.o (.text) *(.text) } . = ALIGN(4); .rodata : { *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.rodata*))) } . = ALIGN(4); .data : { *(.data) } . = ALIGN(4); .bss (NOLOAD) : { *(.bss) . = ALIGN(4); } }
-- Code example:
all:start.o arm-linux-ld -Ttext 0x50008000 -o start.elf $^ %.o:%.S arm-linux-gcc -g -o $@ $^ -c clean: rm -rf *.o *.elf
(2) Start JLink debugging
JLink debug start:
-- Ensure the driver is installed: Note that you need to install the Windows driver;
-- Connect JLink: Connect JLink in the lower right corner of the virtual machine;
-- Start JLinkGDBServer:
[root@localhost JLink_Linux_V434a]# ./JLinkGDBServer SEGGER J-Link GDB Server V4.34a JLinkARM.dll V4.34a (DLL compiled Aug 31 2011 11:51:40) Listening on TCP/IP port 2331 J-Link connected Firmware: J-Link ARM V8 compiled Aug 24 2011 17:23:32 Hardware: V8.00 S/N: 17935099 Feature(s): RDI,FlashDL,FlashBP,JFlash J-Link found 2 JTAG devices, Total IRLen = 5 JTAG ID: 0x07B76F0F (ARM11)
(2) Eclipse debugging environment
Build eclipse debugging environment:
-- Import project: Select Makefile Project With Existing Code;
-- Select the imported code location:
-- clean code: select "Project" --> "Clean";
-- Build project: Select "Menu" --> Project --> Build All option;
--Configure Debug parameters:
-- Execute debugging: Press F6 to step through the debugging. You can view the register values in the Register view. You can see that r1 and r2 are assigned 1 and 2.
2. ARM instruction classification
ARM Assembly Manual:
The difference between GNU assembly and ARM standard assembly: The manual above is the ARM standard assembly manual, and we wrote the GNU assembly manual, which is somewhat different;
-- Case difference: ARM standard assembly is all uppercase, GNU assembly can be lowercase;
1. Arithmetic and logical instructions
(1) MOV instruction
Introduction to MOV instruction: assignment operation;
-- Syntax format: MOV
-- Syntax analysis: dest is the destination register, op1 can be an immediate value, a register, an address, etc., which is equivalent to dest = op1;
Assembler comments: Use the "@" symbol to add comments in the assembly;
Sample code:
.text .global _start _start: @mov command example mov r1, #8 @Assign 8 to r1 mov r2, r1 @assign the value in r1 to r2 mov r3, #10 @Assign 10 to register r3
(2) MVN command
MVN instruction introduction: negate assignment operation;
-- Syntax format: MVN
-- Syntax analysis: Invert the operand op1 and assign it to dest;
Command example:
-- Code:
.text .global _start _start: @mvn command example mvn r1, #0b10 @0b10 Binary number inversion, assign to r1 mvn r2, #5 @5 decimal number inversion, assign to r2 mvn r3, r1 @Assign the value of register r1 to r3
(3) SUB instruction
SUB instruction introduction: subtraction operation;
-- Syntax format: SUB
-- Syntax analysis: dest stores the subtraction result, op1 is the subtrahend, op2 is the minuend, dest = op1 - op2;
-- Note: dest op1 cannot use immediate data, but op2 can use immediate data;
Code example:
.text .global _start _start: @sub directive examples @sub r1, #4, #2 Incorrect example, the subtraction number cannot be an immediate number, it must be a register mov r2, #4 sub r1, r2, #4 mov r0, #1 sub r3, r1, r0
(4) ADD instruction
ADD instruction introduction: addition operation;
-- Syntax format: ADD
-- Syntax analysis: dest stores the result of addition, op1 and op2 are the two numbers to be added, dest = op1 + op2;
-- Note: dest op1 cannot use immediate data, but op2 can use immediate data;
Code example:
@add directive example mov r2, #1 add r1, r2, #3
(5) AND instruction
AND instruction introduction: logical and operation;
-- Syntax format: AND
-- Syntax analysis: dest stores the result of logical AND operation, op1 and op2 are two numbers to be ANDed, dest = op1 & op2;
-- Note: dest op1 cannot use immediate data, must use registers, op2 can use immediate data;
Code example:
.text .global _start _start: @and Directive Examples mov r1, #5 and r2, r1, #0 mov r1, #5 mov r2, r1, #1
(6) BIC instruction
BIC instruction introduction: bit clear instruction operation;
-- Syntax format: AND
-- Syntax analysis: dest stores the result of clearing the bit, op1 is the object to be cleared, and op2 is the mask;
-- Example: "bic r0, r0, #0b1011", clear bits 0, 1, and 3 in r0, keep the rest of the bits unchanged, and put the result in r0;
-- Note: dest op1 cannot use immediate data, must use registers, op2 can use immediate data;
-- Binary representation: % in the mask represents binary in standard assembly, but cannot be used in GNU assembly. GNU assembly uses 0b to represent binary;
Code example:
.text .global _start _start: @bic directive examples mov r1, #0b101011 bic r2, r1, #0b101 @ Clear bits 0 and 2 of r1
2. Comparison Instructions
(1) CMP instruction
CMP instruction introduction: comparison instruction;
-- Syntax format: CMP
-- Syntax analysis: There are three comparison results: op1 > op2 (CPSR N = 0), op1 = op2 (CPSR Z = 1), op1 < op2 (CPSR N = 1), the result is placed in the CPSR register;
Code example:
.text .global _start _start: @cmp directive example mov r1, #2 cmp r1, #1 mov r1, #2 cmp r1, #3 mov r1, #2 cmp r1, #2
(2) TST instruction
TST instruction introduction: comparison instruction;
-- Syntax format: TST
-- Syntax analysis: op1 and op2 are bitwise ANDed, the result affects the CPSR register, if the result is not 0, CPSR's Z = 0, if the result is 0, Z = 1;
Code example:
.text .global _start _start: @cmp directive example mov r1, #0b101 tst r1, #0b001 @The bitwise AND result is 0b1, the result is not 0, CPSR Z = 0 mov r1, #0b101 tst r1, #0b10 @The bitwise AND result is 0, the result is not
3. Branch Instructions
(1) B instruction
B Instruction Introduction: Branch instruction;
-- Syntax format: B{condition} address;
-- Syntax analysis: If the condition is met, jump to the address location, if the condition is not met, execute the following statement, if there is no condition, it is 100% executed;;
Code example:
-- Conditional analysis: gt is a greater than condition, if r1 > r2, go to the conditional branch, otherwise continue to execute the next one;
.text
.global _start
_start:
@b branch instruction example
mov r1, #6
mov r2, #5
cmp r1, r2 @Compare the values in r1 and r2
@b can be followed by a condition. {condition} in {} can be added or not. If there is no condition, it will be executed unconditionally 100%.
@gt is a greater than condition instruction. If the condition is met, it will jump to branch1. If not, it will execute the following instruction.
bgt branch1
add r3, r1, r2
b end @In order not to execute branch1 operation here, jump directly to end execution
branch1:
sub r3, r1, r2
end:
nop
(2) BL instruction
BL instruction introduction: branch instruction with connection;
-- Syntax format: BL{condition} address;
-- Syntax analysis: If the condition is met, jump to the address location, if the condition is not met, execute the following statement, if there is no condition, it is 100% executed;;
Code example:
.text
.global _start
_start:
@bl Branch instruction example with connection
Previous article:Camera driver based on WINCE6.0+S3C2443
Next article:[Embedded Development] Introduction to ARM chips (ARM chip types | ARM processor working modes | ARM registers | ARM addressing)
Recommended ReadingLatest update time:2024-11-14 15:19
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Red Hat announces definitive agreement to acquire Neural Magic
- 5G network speed is faster than 4G, but the perception is poor! Wu Hequan: 6G standard formulation should focus on user needs
- SEMI report: Global silicon wafer shipments increased by 6% in the third quarter of 2024
- OpenAI calls for a "North American Artificial Intelligence Alliance" to compete with China
- OpenAI is rumored to be launching a new intelligent body that can automatically perform tasks for users
- Nidec Intelligent Motion is the first to launch an electric clutch ECU for two-wheeled vehicles
- Nidec Intelligent Motion is the first to launch an electric clutch ECU for two-wheeled vehicles
- ASML provides update on market opportunities at 2024 Investor Day
- Arm: Focusing on efficient computing platforms, we work together to build a sustainable future
- AMD to cut 4% of its workforce to gain a stronger position in artificial intelligence chips
- TMS320C6748 DSP Development Kit (LCDK)
- [RVB2601 Creative Application Development] 6. Online Weather Clock Draft Version
- Altium Designer single-select and checkbox disproportion
- Help: Transformer detection of low frequency or DC residual current
- [Silicon Labs BG22-EK4108A Bluetooth Development Review] Part 3: Bluetooth-Soc Blinky Example Development Experience
- Analysis of the effect of resistance between the be electrodes of PNP transistors
- I want to do some math questions for the postgraduate entrance examination. Which postgraduate entrance examination tutor can explain the real questions in a detailed and easy-to-understand way?
- How to scan keys on this triangular keyboard
- How to use CYCLECOUNTER to quickly measure execution time?
- Circuit principle?