Embedded Linux+ARMARM architecture and programming (ARM assembly instructions)

Publisher:sunyouz1Latest update time:2016-06-24 Source: eefocusKeywords:Linux Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
These are my own simple summaries of the most commonly used ARM assembly instructions, which will be continuously supplemented and improved later.

1. Segment names predefined by the assembly system

.text @code segment.data
@initialized data segment.bss
@uninitialized data segment
It should be noted that the .bss segment should be before .text in the source program.

2. Define the entry point
The default entry point of the assembler is the start label, and the user can also use the ENTRY flag in the linker script file to specify other entry points.

.text

.global _start

_start:

 

3. Word usage

word expression is to put a word value at the current position. This value is expression 
. For example, 

_rWTCON: 

     .word 0x15300000 
means putting a value of 0x15300000 at the current address, _rWTCON 

 

4. Equ assignment operation, equivalent to the macro definition of C language

.equ MEM_CTRL_BASE, 0x48000000 //Note that the , sign must be added

 

5. Logical instructions

AND---Logical "AND" operation instruction

Instruction format:
AND{cond}{S} Rd,Rn,operand2
The AND instruction performs a bitwise logical AND on the operand operand2 and the value of Rn, and stores the result in the destination register Rd. If S is set, the N and Z bits are affected according to the operation result. When calculating the second operand, the C bit is updated and the V bit is not affected (the influence of the instructions ORR, EOR, and BIC on the flag bit is the same as that of the AND instruction).
Instruction example:
ANDS R1,R1,R2; R1=R1&R2, and update the flag bit according to the result of the operation
AND R0,R0,#0x0F; R0=R0&0x0F, take out the lowest 4 bits of data in R0.
 
ORR --- logical "OR" operation instruction
Instruction format: ORR {cond} {S} Rd, Rn, operand2 ORR instruction performs bitwise logical "OR" on operand2 and the value of Rn, and stores the result in the destination register Rd. Instruction example: 
ORRS R1, R1, R2; R1 = R1 | R2, and updates the flag bit according to the result of the operation
ORR R0,R0,#0x0F; R0=R0|0x0F, set the lowest 4 bits of R0 to 1, and keep the rest of the bits unchanged.
 
BIC---bit clear instruction
Instruction format:
BIC{cond}{S} Rd,Rn,operand2 
The BIC instruction performs a bitwise logical AND operation on the value in Rn and the complement of operand2, and stores the result in the destination register Rd.
Instruction example: BIC R0,R0,#0x0F; clear the lowest 4 bits of R0 and keep the other bits unchanged.
 
CMP---Comparison instruction
Instruction format:
CMP{cond} Rn,operand2
The CMP instruction subtracts operand2 from the value of Rn and reflects the status of the result (Rn is greater, lesser, or equal to operand2) in CPSR so that the following instructions can determine the direction of the program based on the conditional flags. The CMP instruction performs the same operation as the SUBS instruction, except that the CMP instruction only subtracts and does not store the result.
Instruction example: 
cmp R0,R1; compare R0,R1 
beq stop; R0=R1 jump to stop
blt less; R0
Less:...
Stop:...

reference:

http://blog.csdn.net/denlee/article/details/2501182

 

In embedded development, assembler programs are often used in critical places, such as initialization at system startup, environment preservation and recovery when entering and exiting interrupts, and functions with very demanding performance requirements.

1. Relative jump instructions: b and bl. 
The difference is that in addition to jumping, the bl instruction also saves the return address (the address of the next instruction of bl) in the lr register.
Jump range: 32M before and after the current instruction.
They are position-independent instructions.
Example:
        b fun1
......
fun1:
        bl fun2
......
fun2:
......


2. Data transfer instructions: mov, address read pseudo-instruction: ldr
The mov instruction can assign the value of a register to another register, or assign a constant to a register.
Example:
mov r1, r2
mov r1, #4096
The constant transferred by the mov instruction must be represented by an immediate number.
When you don't know whether a number can be represented by an immediate number, you can use the ldr command to assign a value. ldr is a pseudo instruction, it is not a real instruction, the compiler will expand it into a real instruction: if the constant can be represented by an immediate value, use the mov instruction; otherwise, save the constant in a certain location during compilation, and use the memory read instruction to read it out.
Example:
ldr r1, =4097
The original meaning of ldr is "large-range address read pseudo instruction". The following is to obtain the absolute address of the code:
Example:
        ldr r1, =label
label:
......


3. Memory access instructions: ldr, str, ldm, stm
The ldr instruction can be a large-range address read pseudo instruction or a memory access instruction. When there is "=" in front of its second parameter, it indicates a pseudo instruction, otherwise it indicates a memory access instruction. The
ldr instruction reads data from the memory to the register, and the str instruction stores the value of the register to the memory. The data they operate on are all 32 bits.
Example:
ldr r1, [r2, #4] // Read the data of the memory cell at address r2+4 into r1ldr
r1, [r2] // Read the data of the memory cell at address r2 into r1ldr
r1, [r2], #4 // Read the data of the memory cell at address r2 into r1, then r2=r2+4str
r1, [r2, #4] // Save the data of r1 into the memory cell at address r2+4str
r1, [r2] // Save the data of r1 into the memory cell at address r2str
r1, [r2], #4 // Save the data of r1 into the memory cell at address r2, then r2=r2+4ldm
and stm are batch memory access instructions, which can read and write multiple data with only one instruction. The format is:
ldm {cond} { ! } { ^ }
stm {cond} { ! } { ^ }
, where {cond} indicates the execution conditions of the instruction:

Condition code (cond)

Mnemonics

meaning

Conditional flags in cpsr

0000

eq

equal

From = 1

0001

it is

not equal

Z = 0

0010

cs/hs

Unsigned greater than/equal to

C = 1

0011

cc/lo

Unsigned number is less than

C = 0

0100

me

negative number

N = 1

0101

pl

Non-negative numbers

N = 0

0110

vs

Overflow

V = 1

0111

vc

No overflow

V = 0

1000

hi

Unsigned number greater than

C = 1 or Z = 0

1001

ls

Unsigned number less than or equal to

C = 0 or Z = 1

1010

ge

Signed number greater than or equal to

N = 1, V = 1 or N = 0, V = 0

1011

lt

Signed number is less than

N = 1, V = 0 or N = 0, V = 1

1100

gt

Signed number greater than

Z = 0 and N = V

1101

the

Signed less than/equal to

Z = 1 or N! = V

1110

al

Unconditional execution

-

1111

nv

Never execute

-

Most ARM instructions can be executed conditionally, that is, whether to execute the instruction is determined by the conditional flag in the cpsr register: if the condition is not met, the instruction is equivalent to a nop instruction.
Each ARM instruction contains a 4-bit conditional code field, which means that 16 execution conditions can be defined. The
cpsr conditional flags N, Z, C, and V represent Negative, Zero, Carry, and oVerflow, respectively.

indicates the address change mode, which has 4 modes:
ia (Increment After): post-increment mode.
ib (Increment Before): pre-increment mode.
da (Decrement After): post-decrement mode.
db (Decrement Before): pre-decrement mode.
stores the memory address. If an exclamation mark is added after it, the value of rn will be updated after the instruction is executed, which is equal to the address of the next memory unit.
indicates a register list. For the ldm instruction, data is taken from the memory block corresponding to and written to these registers; for the stm instruction, the values ​​of these registers are written to the memory block corresponding to .
{^} has two meanings:
if there is a pc register in , it means that after the instruction is executed, the value of the spsr register will automatically be put into the cpsr register - this is often used to return from an interrupt handler;
if there is no pc register in , {^} means that the register in user mode is operated, not the register in the current privileged mode.
Example:
HandleIRQ: @Interrupt entry function
        sub lr, lr, #4 @Calculate the return address
        stmdb sp!, { r0 - r12, lr } @Save the used registers
                                                                @r0 - r12, lr are saved in the memory represented by sp
                                                                @“!” makes the instruction execute sp = sp - 14 * 4

        ldr lr, =int_return @Set the return address after calling the IRQ_Handle function
        ldr pc, =IRQ_Handle @Call interrupt distribution function
int_return:
        ldmia sp!, { r0 - r12, pc }^ @Interrupt return, “^” means to transfer the value of spsr to cpsr
                                                                 @So return from irq mode to the interrupted working mode
                                                                 @“!” makes the instruction execute sp = sp + 14 * 4


4. Addition and subtraction instructions: add , sub
Example:
add r1, r2, #1 // r1 = r2 + 1
sub r1, r2, #1 // r1 = r2 - 1


5. Program status register access instructions: msr, mrs
ARM processor has a program status register (cpsr), which is used to control the processor's working mode and set the interrupt switch.
Example:
msr cpsr, r0 // r0 to cpsr
mrs r0, cpsr // cpsr to r0

6. Other pseudo instructions.extern
: define an external symbol (can be a variable or a function)
.text: indicates that the current statements belong to the code segment.global
: define a program label in this file as global


ARM-THUMB subroutine call rules: ATPCS
In order to enable C language programs and assembly programs to call each other, rules must be formulated for subroutine calls. In ARM processors, this rule is called ATPCS: rules for subroutine calls in ARM programs and THUMB programs. The basic ATPCS rules include register usage rules, data stack usage rules, and parameter passing rules.

1. Register usage rules Subroutines
pass parameters through registers r0 ~ r3, and their aliases a1 ~ a4 can be used at this time. The contents of r0 ~ r3 do not need to be restored before the called subroutine returns. 
In a subroutine, r4 ~ r11 are used to save local variables, and their aliases v1 ~ v8 can be used at this time. If some of their registers are used in the subroutine, the values ​​of these registers must be saved when the subroutine enters and restored before returning; for registers not used in the subroutine, these operations are not necessary. In THUMB programs, usually only registers r4 ~ r7 can be used to save local variables.
Register r12 is used as a scratch register between subroutines, and its alias is ip.
Register r13 is used as a data stack pointer, and its alias is sp. Register r13 cannot be used for other purposes in a subroutine. Its value must be the same when entering and exiting a subroutine.
Register r14 is called a link register, and its alias is lr. It is used to save the return address of the subroutine. If the return address is saved in the subroutine (for example, the lr value is saved in the data stack), r14 can be used for other purposes.
Register r15 is the program counter, aliased as pc . It cannot be used for any other purpose.

register

Aliases

Rules of Use

r15

pc

Program Counter

r14

lr

Connection Register

r13

sp

Data stack pointer

r12

ip

The scratch register for the subroutine call

r11

v8

ARM state local variable register 8

r10

v7、s1

ARM state local variable register 7. In ATPCS supporting data stack checking, it is the data stack limit pointer

r9

v6、sb

ARM state local variable register 6, static base address register in ATPCS supporting RWPI

r8

v5

ARM state local variable register 5

r7

v4、wr

ARM state local variable register 4, THUMB state working register

r6

v3

ARM state local variable register 3

r5

v2

ARM state local variable register 2

r4

v1

ARM state local variable register 1

r3

a4

Parameters/results/scratch register 4

r2

a3

Parameters/results/scratch register 3

r1

a2

Parameters/results/scratch register 2

r0

a1

Parameters/results/scratch register 1


2. Data stack usage rules
The data stack has two growth directions: when it grows in the direction of decreasing memory address, it is called DESCENDING stack; when it grows in the direction of increasing memory address, it is called ASCENDING stack.
The so-called growth of the data stack is to move the stack pointer. When the stack pointer points to the top element of the stack (the last data pushed into the stack), it is called FULL stack; when the stack pointer points to an empty data unit adjacent to the top element of the stack (the last data pushed into the stack), it is called EMPTY stack.
The data stack can be divided into 4 types:
FD: Full Descending
ED: Empty Descending
FA: Full Ascending
EA: Empty Ascending
ATPCS stipulates that the data stack is of FD type, and the operation on the data stack is 8-byte aligned. Use stmdb / ldmia batch memory access instructions to operate the FD data stack.
When using the stmdb command to save content to the data stack, first decrement the sp pointer and then save the data. When using the ldmia command to restore data from the data stack, first obtain the data and then increment the sp pointer. The sp pointer always points to the top element of the stack, which is exactly the definition of the FD stack.

3. Parameter passing rules
Generally, when the number of parameters does not exceed 4, the four registers r0 ~ r3 are used to pass parameters; if the number of parameters exceeds 4, the remaining parameters are passed through the data stack.
For general return results, r0 ~ r3 are usually used to pass.
Example:
Assume that the CopyCode2SDRAM function is implemented in C language, and its data prototype is as follows:
int CopyCode2SDRAM( unsigned char *buf, unsigned long start_addr, int size )
In the assembly code, use the following code to call it and judge the return value:
ldr r0, =0x30000000 @1. Target address = 0x30000000, which is the starting address of SDRAM
mov r1, #0 @2. Source address = 0
mov r2, #16*1024 @3. Length = 16K
bl CopyCode2SDRAM @Call C function CopyCode2SDRAM
cmp a0, #0 @Judge the return value of the function

 
Keywords:Linux Reference address:Embedded Linux+ARMARM architecture and programming (ARM assembly instructions)

Previous article:ARM assembly instructions detailed explanation 1
Next article:ARM assembly instruction learning to implement data block copy

Recommended ReadingLatest update time:2024-11-16 13:50

I2C touch screen based on ARM processor S3C2440 and Linux system
0 Introduction With the development of computer-related technologies, ARM embedded systems are increasingly widely used and are increasingly integrated into people’s lives. Touch screen devices are widely used in this embedded field because of their friendly human-computer interaction, convenient and flexible operati
[Microcontroller]
I2C touch screen based on ARM processor S3C2440 and Linux system
Introduction to the ticket anti-counterfeiting detection system based on digital watermark and ARM
Tickets are tickets for watching the Olympics, concerts, and attending high-level conferences. With the development of high-quality image input and output devices, especially the emergence of color inkjet, laser printers and high-precision color copiers with an accuracy of more than 1,200 dpi, ticket counterfeiting
[Microcontroller]
Introduction to the ticket anti-counterfeiting detection system based on digital watermark and ARM
Research on the Design of GPRS/CQT Test System Based on ARM Platform
     GPRS, as the bearer network for China Mobile's data services, supports a variety of data services involving different network elements. Any failure of a network element will directly affect the normal use of data services. It is very important to ensure end-to-end use for users.   GPRSDT/CQT test simulates the us
[Microcontroller]
Research on the Design of GPRS/CQT Test System Based on ARM Platform
Regarding the acquisition of Arm, has Nvidia made the worst-case scenario plan?
According to a telegraph report a few days ago, Nvidia warned for the first time that if regulators insist on blocking the deal, it may lose the $1.25 billion (950 million pounds) down payment paid to realize the acquisition of British chip company Arm. The US graphics chip company confirmed to investors that if regul
[Semiconductor design/manufacturing]
【ARM】2410 bare metal series - key query control LED
Development Environment   Hardware platform: FS2410 Host: Ubuntu 12.04 LTS LED lamp schematic diagram Button schematic diagram Wiring resources for buttons   KSCAN0 - GPE11 KSCAN1 - GPG6 KSCAN2 - GPE13 KSCAN3 - GPG2 EINT0 - GPF0 EINT2 - GPF2 EINT11 - GPG3 EINT19 - GPG11 Main principles of the program   Here we
[Microcontroller]
【ARM】2410 bare metal series - key query control LED
Technical differences between ARM (RISC) and x86 (CISC)
RISC and CISC, this pair of enemies, have been in constant entanglement since the day they were born. Until today, after years of development, both have opened up a world in their respective fields and penetrated each other. RISC focuses on high performance, high performance power consumption, small size and mobile de
[Microcontroller]
Technical differences between ARM (RISC) and x86 (CISC)
Introduction to the ticket anti-counterfeiting detection system based on ARM
Tickets are admission tickets to watch the Olympics, concerts, and attend high-level conferences. With the development of high-quality image input and output equipment, especially the emergence of color inkjet and laser printers and high-precision color copiers with an accuracy of more than 1,200 dpi, ticket counterfe
[Microcontroller]
Introduction to the ticket anti-counterfeiting detection system based on ARM
ARM embedded application debugging: self-made system call
1. Principle To make your own system call, of course, all you need to do is understand the system call process: Let's take the open function as an example: when the user space executes the open function, it will eventually call the sys_open function through the glibc function library, and the sys_open function will ev
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • 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* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号