【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-14 15:19

Korean media analysis: Why is Samsung "indifferent" to the acquisition of Arm?
Nvidia recently announced that it will acquire Arm from SoftBank for $40 billion. If this acquisition is realized, it will become the largest deal in the history of semiconductor industry mergers and acquisitions. Nvidia has agreed to purchase all Arm shares held by SoftBank and its subsidiaries, and will seek approva
[Mobile phone portable]
ARM Basic Knowledge
In addition to normal saving, after compiling correctly, choose to save as a .h file with the same name, and change the main function name in the h file to other names, such as xmain, or main1, etc., then the new project can call the function in the original project! 1. Project 1 first write LCD1602, and after compili
[Microcontroller]
ARM · Assembly pseudo instructions
In the ARM assembler, there are several pseudo instructions: *Symbol definition directive *Data definition directives * Assembly control directives *Information reporting directive *Macro instructions and other pseudo instructions   Define local variables LCLA, LCLL, LCLS Define global variables GBLA,
[Microcontroller]
Solve the 2.95.3 arm-linux-gcc compilation error problem
1. There will be an error when decompressing opt. The essence is that the symbolic link inside is wrong and the file cannot be found, but it does not affect the use. 2. When the make compilation error occurs: arm-linux-gcc: installation problem,cannot exec 'cpp0': No such file or directory This 2.95.3 version of arm
[Microcontroller]
Detailed explanation of LPCXpresso54628, a low-power MCU based on ARM Cortex-M4 core
NXP's LPCXpresso54628 is a low-power MCU based on the ARM Cortex-M4 core, with rich peripherals, very low power consumption and enhanced debugging features. The ARM Cortex-M4 CPU adopts a 3-stage pipeline, a Harvard architecture with separate local instruction and data buses and a third bus for peripherals, supports s
[Microcontroller]
Detailed explanation of LPCXpresso54628, a low-power MCU based on ARM Cortex-M4 core
arm assembly pseudo-instructions and position-dependent codes
Position-dependency and position-independence are mainly reflected in whether the address of the program when it is running is the same as the address of the code segment specified during compilation. Compilation environment: ADS1.2 Development board: GEC210 (s5pv210soc) Program running method: Use uboot's tftp
[Microcontroller]
ARM11 and DSP 3G video security helmet design solution
1 Introduction In order to improve the controllability of on-site operations in high-risk workplaces, this paper adopts bionic principles and high-integration design to realize a 3G video helmet with the same viewing angle as the human eye. This design consists of a video helmet and a waist-span data processing termin
[Microcontroller]
ARM11 and DSP 3G video security helmet design solution
Inventory of 11 embedded operating systems based on ARM
  Embedded Operation System (EOS) refers to an operating system used in embedded systems. Embedded systems are divided into four layers: hardware layer, driver layer, operating system layer and application layer. The embedded operating system is responsible for the allocation of all software and hardware resources of
[Microcontroller]
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号