current position:Home>[GNU assembly syntax] assembly instructions involved in the startup of embedded chips

[GNU assembly syntax] assembly instructions involved in the startup of embedded chips

2022-11-24 23:09:02Evenurs

1. Introduction to GNU assembly syntax

GNU assembly syntax is currently available for all architectures, not just ARM.GNU assembly instructions are composed of a set of well-defined rules, each of which can be decomposed into the following three parts:

Label:Command @Comment
  1. Label: label, used to represent the location (address, etc.).Some assembly instructions will be preceded by a label, in which case it is allowed to use the label to represent the address of the assembly instruction.Note: Any field ending with a colon ":" will be recognized as a label.
  2. Command: Instructions, which can be assembly instructions or pseudo-instructions.
  3. @: comment symbol, the following content is used as a comment, and does not participate in compilation, and has the same effect as "//", "/*", "*/" and other comment symbols in c language.Note: In GNU assembly files, "/*" and "*/" can also be used as comment symbols to replace "@".
  4. Comment: comment content.

The sample code is as follows:

funckey:MOV R0,#0x01

Note: In the assembly language of ARM, any assembly instructions, pseudo-instructions, pseudo-operations, registers and other descriptions can use all uppercase or all lowercase, but mixed uppercase and lowercase are not allowed.

1.1 Pseudo-operation

​Pseudo-operations, assembly instructions, and macro instructions together constitute an assembly language program. Pseudo-operations are not executed by the computer during program execution, but are processed by the assembler when the assembler compiles the source program.Pseudo-operations can complete a variety of defined functions, such as data definition, memory allocation, and program end indication.

  1. .section: Define section name
  2. .global: define global tags
  3. .byte: Define single-byte data
  4. .short: define double-byte data
  5. .long: Define four-byte data
  6. .equ: assignment, for example: .equ var,0x01
  7. .align: data byte alignment
  8. .end: Indicates the end of the source program file
  9. .type: the class that defines the label

1.2 Define function

​Using .type to define, the structure is as follows:

.type func_name,@func_startfunc_name:$operationret
  1. Line 1:
    1. .type: define a label, which is a function class
    2. func_name: function name
    3. @func_start: Indicates that the function starts
  2. Line 2:
    1. func_name: Function label declaration
  3. Line 3:
    1. $operation: the specific operation of the function
  4. Line 4:
    1. ret: the function returns, the same as the "return" at the end of the function in C language

A sample function is as follows:

.type move_reg,@functionmove_reg:mov R0,R1 @Assign the data stored in register R1 to R0, same as "R0=R1;" in c languageret

Second, assembly instructions

Introduce several commonly used assembly instructions.


For register data writing.


MOV R0,R1 @Write the data in R1 into R0MOV R0,#0x01 @Write 0x01 (immediate value) into R0


Read data in special registers (CPSR, SPSR) and transfer to general registers.


MRS R0, SPSR_xxx @Write the data in SPSR_xxx into the R0 register, xxx is the working mode of the chip


Write the data in the general register to the special register.


MSR CPSR, R0 @Write the data in R0 into the special register CPSR


Write a 32-bit data into the target register. The data generally uses the register address. In this way, the data in the register address is accessed, so the use of this instruction is very flexible.


LDR R0,[R1] @Consider the 32-bit data in R1 as a register address, write the data in this address into R0LDR R0,[R1,R2] @Add the data in R1 and R2, the 32-bit data in it is regarded as the register address, and write the data in this address into R0LDR R0,[R1,#0x01] @After adding 0x01 to the data in R1, the 32-bit data in it is regarded as the register address, and the data in this address is written into R0LDR R0,=0x20000c00 @Write the data in register address 0x20000c00 into R0

Note: If the last address data of LDR is an immediate value and does not exceed 8 bits, then the LDR instruction will be converted into a MOV instruction during assembly. The example is as follows:

LDR R0,=0xff

The instructions after assembly are as follows:


At this time, it is not the immediate number 0xff, but the indirect addressing to write the data in the address 0xff into R0.


Write data into the register address.


STR R0,[R1] @Take the 32-bit data in the R1 register as the register address, and write the data in R0 to this address


The unconditional jump instruction, if followed by the actual value, is regarded as the relative offset of the PC pointer, not the absolute pointer.Generally, it is used to follow the label of the jump address.


B label1:


Jump with data protection.Before the jump, the current PC pointer will be saved in the register R14, and the original instruction can be realized by reloading the PC pointer, that is, the operation of function call and return is realized.


BL label2:


On the basis of BL, after adding a jump, switch the processor working state from ARM to Thumb.


BLX label3:


Jump to this address, the instruction stored in this address can be either ARM or Thumb instruction.


BX R0 @PC pointer jumps to the address represented by the data in the R0 register, and uses the lowest bit of the data in the R0 register to set the processor working state

When the lowest bit data of R0 is 0, the processor works in ARM state.

When the lowest bit data of R0 is 1, the processor works in Thumb state.

3. Summary

Assembly instructions are generally used for booting after power-on during the chip start-up phase, just point the PC address to the main function entry of the program.

copyright notice
author[Evenurs],Please bring the original link to reprint, thank you.

Random recommended