【Embedded Development】 ARM Assembly (Instruction Classification | Pseudo-instructions | Coprocessor Access Instructions)

Publisher:温馨生活Latest update time:2024-10-18 Source: cnblogsKeywords:ARM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

[1] [2] [3]
Keywords:ARM Reference address:【Embedded Development】 ARM Assembly (Instruction Classification | Pseudo-instructions | Coprocessor Access Instructions)

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-21 22:44

Arm stated twice: We do not want to lose the opportunity to cooperate with Huawei
Some time ago, Arm, the world's number one IP supplier, became the focus of industry attention due to its forced suspension of cooperation with Huawei HiSilicon. In response, the company made two statements, expressing its desire to find a solution to resume the partnership.   First, on June 5, Arm founder Hermann Hau
[Embedded]
Arm stated twice: We do not want to lose the opportunity to cooperate with Huawei
ARM7 microcontroller (learning) - (three), UART - 03
Today's experiment uses the FIFO interrupt of UART0~~ Receive data sent from the virtual terminal~~ Here the depth is set to 8~~ Then send it back~~ But~~I can't send it back~~Fuck~~ Let's leave this question for now~~ Let’s solve it together later~~ 3. UART Three - (03), send a string to the serial port and then send
[Microcontroller]
ARM7 microcontroller (learning) - (three), UART - 03
ARM assembly pseudo instructions (1)
    The ARM assembler consists of machine instructions, pseudo-instructions and macro instructions. 1. Symbol definition pseudo-instruction The symbol definition pseudo-instruction is used to define variables of the ARM assembler, assign values ​​to variables, and define register names. Global variable de
[Microcontroller]
ARM learning "Three" PLL clock configuration - make your heart beat!
When initializing the system, ARM has one more important task than 51, which is clock configuration. One of the reasons why ARM has to perform tedious clock configuration is that in the ARM architecture, the system fully considers the power consumption of each peripheral. Some peripherals are sometimes not needed in ou
[Microcontroller]
Why do some register addresses in ARM microcontrollers differ by 4?
The picture shows some registers in the system control module (SYSCON) of the LPC1114 user manual. Please look at the red box. The address difference is 0x4. Why is there a difference of 4? Many beginners ask me this question, so experts should avoid it. Computers and microcontrollers store data in bytes. The 4 here
[Microcontroller]
Why do some register addresses in ARM microcontrollers differ by 4?
ARM China's Rashomon has a sequel
Battle for control The two parties in this battle for control are represented by the Arm China Board of Directors, which represents the Arm Group and its parent company SoftBank Group, and the current team represented by Arm China CEO Wu Xiong'ang. On the morning of April 29, ARM Technology (China) Co., Ltd. (
[Semiconductor design/manufacturing]
ARM China's Rashomon has a sequel
Arm China laid off nearly 100 people, and the company’s CEO said he would strive to complete an IPO within this year.
On the morning of February 13, in response to the news of layoffs at the Chinese joint venture Arm Technology, a British chip architecture company owned by Japan’s SoftBank Group, Arm stated in a statement displayed to TMTpost Media App that “Arm Technology (Arm China) It is an independent and operating entity from Ar
[Semiconductor design/manufacturing]
Learn ARM development(21)
First, declare the task pointer because it will be used later. //Task pointer. volatile TASK_TCB* volatile g_pCurrentTask = NULL; volatile TASK_TCB* volatile g_pCurrentTask1 = NULL; volatile TASK_TCB* volatile g_pCurrentTask2 = NULL; Next, we need to initialize these task stacks. Use the fol
[Microcontroller]
Learn ARM development(21)
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号