Embedded development plan-50—-ARM–Related syntax of ARM assembly language–ARM assembly instructions

One hundred and twenty-five, related syntax of ARM assembly language

125.1 Contents in the assembly file

1. Pseudo operation: It does not occupy storage space in the assembly program, but it can play a guiding and identifying role when the program is compiled.
.text .global .glbal .if .else .endif .data .word....
2. Assembly instructions: Each assembly instruction is used to identify a machine code and allow the computer to perform an instruction operation.
data processing instructions
program flow control instructions
memory access instructions
Status Register Transfer Instruction CPSR
Soft interrupt instruction
3. Pseudo-instruction: It is not an assembly instruction, but it can also allow the processor to do some data processing. Usually a pseudo-instruction is jointly implemented by multiple assembly instructions.
4. Comments
Single line comment: @ ;(some use ;’)
Multi-line comments: /* */
Conditional comments
.if 1/0
Command section 1
.else
Command section 2
.endif

125.2 Basic syntax format of assembly instructions

  • The basic format of the instruction:
    <opcode> {<cond>} {s} <Rd>, <Rn>, <shifter_operand>
    
    <opcode>: The operation code of the instruction
    
    cond: condition code suffix
    
    s: The execution result of the instruction will affect the condition flag bit in CPSR.
    
    <Rd>: Destination register, the operation result of the instruction is stored in the destination register
    <Rn>: The first operation register can only be a register
    <shifter_operand>: The second operand, which can be either a register number or an immediate number
    Meaning: Let the value in the first operation register and the second operand operate according to the instruction opcode, and save the result of the operation in the target register.
    
    Notice:
    1. Generally, one assembly instruction occupies one line of code.
    2. Assembly is not case sensitive
    3. The operand must be preceded by a #
    

One hundred and twenty-six, ARM assembly instructions

126.1 Data Move Instructions

<opcode> {<cond>} {s} <Rd>, <shifter_operand>
explain:
<opcode>: The operation code of the instruction
cond: condition code suffix
s: The execution result of the instruction will affect the condition flag bit in CPSR.
<Rd>: Destination register, the operation result of the instruction is stored in the destination register
<shifter_operand>: The first operand, which can be either a register number or an immediate number

Script code mnemonic:
mov: moves the operand directly to the destination register
mvn: Invert the operand bitwise and move it to the target register.

126.2 The concept of immediate data

  • Definition: Data that can be directly executed as part of an instruction is called an immediate data. The immediate value is obtained by circularly shifting the even digits to the right through a number between 0-255.
     Circular right shift: remove the low bits and fill in the high bits.
    

126.2.1 Judgment of immediate data

  • Looking at the binary representation of this number, if the first and last range of the number 1 can be stored in 8 bits, then it is an immediate number.

126.2.2 How to save a non-immediate value in a register

Use the pseudo-instruction ldr to complete non-immediate data operations
Format:
ldr destination register name, =data

126.3 Shift operation instructions

126.3.1 Shift operation instruction format and instruction code

Format:
<opcode> {<cond>} {s} <Rd>, <Rn>, <shifter_operand>
Explanation: Shift the value of the first operation register by the second operand bit and save the result in the target register.

Script code:
lsl: Left shift operation, the highest bit is shifted out, and the lowest bit is filled with 0
lsr: Right shift operation, the lowest bit is shifted out, and the highest bit is filled with 0
ror: circular right shift: shift out the lowest bit and fill it to the highest bit

126.3.2 Example

.text
.global_start
_start:
mov r0,#0XFF
lsl r1,r0,#4 @0XFF shift left by four bits and save the result to r1 0XFF0
lsr r2,r0,#4 @0XFF shift right by four bits and save the result to r2 0XF
ror r3,r0,#4 @0XFF rotate right by four digits and save the result to r3 0XF000000F
loop:
b loop
.end

126.4-bit operation instructions

126.4.1 Bit operation instruction code and format

Format:
<opcode> {<cond>} {s} <Rd>, <Rn>, <shifter_operand>

Script code:
and: Perform bitwise AND
orr: Perform bitwise OR
eor: bitwise XOR
bic: Clear bit by bit to 0

126.4.2 Example

.text
.global_start
_start:
mov r0,#0XFF
and r1,r0,#(~(0X1<<4)) @The fourth bit is cleared to 0 0xEF
orr r2,r0,#(0X1<<9) @9th position 1 0X2FF
eor r3,r0,#0XF @0xf0
bic r4,r0,#(0X1<<4)@The fourth bit is cleared to 0 0xEF
loop:
b loop
.end

126.5 Arithmetic operation instructions

126.5.1 Arithmetic operation instruction codes and formats

Format:
<opcode>{<cond>}{s} <Rd>, <Rn>, <shifter_operand>

Script code:
add: addition operation Rd=Rn + shifter_operand
adc: Consider the C bit of CPSR when performing addition operations Rd=Rn + shifter_operand + CPSR[c]
sub : subtraction operation Rd=Rn-shifter_operand
sbc: Consider the c bit of CPSR when performing subtraction operations Rd=Rn-shifter_operand-!CPSR[c]
RSB: Reverse subtraction Rd=shifter_operand-Rn
RSC: Reverse subtraction instruction with borrow bit Rd = shifter_operand – Rn - !CPSR[c]
mul : multiplication operation Rd=Rn*shifter_operand

126.5.2 Example

.text
.global_start
_start:
mov r0,#0XFFFFFFFE
mov r1,#3
adds r2,r0,r1 @0X1, the result of the operation affects the condition bit
adc r3,r1,r2 @r3=r1 + r2 + CPSR[c]
loop:
b loop
.end

126.5.3 Perform 64-bit arithmetic operations

  • Idea: Let the lower 32 bits and the upper 32 bits perform separate operations, and the upper 32 bits and lower 32 bits of each 64-bit number are stored in two registers.
MOV R1,#0xfffffffe @lower 32 bits of the first data
mov r2,#0x00000004 @high 32 bits of the first data

MOV R3,#0x00000005 @lower 32 bits of the second data
mov r4,#0x00000004 @high 32 bits of the second data

addition:
Lower 32 bits:
adds r5,r1,r3
High 32 bits:
adc r6,r2,r4
    
Subtraction:
Lower 32 bits:
subs r5,r3,r1
High 32 bits:
sbc r6,r4,r2

126.6 Data comparison instructions

126.6.1 Data comparison command code and format

Format:
cmp <Rn>, <shifter_operand>
The essence of the comparison instruction:
Perform a subtraction operation on the first operation register and the second operand, and the result of the subtraction operation will affect the condition bit of the CPSR

Different operations can be performed based on the value of the condition bit after the comparison instruction, which is equivalent to the selection statement in c
Here we need to judge the condition bit of CPSR. We rely on the mnemonic {cond} suffix of the condition bit to achieve

126.6.2 Example

.text
.global_start
_start:
MOV R1,#4
MOV R2,#4
CMP R1,R2
addeq r3,r1,r2 @if(r1==r2) r3=r1 + r2
subne r4,r1,r2 @if(r1!==r2) r4=r1-r2
loop:
b loop
.end

126.7 Jump instruction

126.7.1 Jump script and format

  • There are generally two ways to implement program jumps:

    1. Directly modify the value of PC
    2. via jump instructions
  • Jump instruction:

    1. b label
      Explanation: Jump to the code where the label label is located. At this time, the lr register does not save the return address.
      .text
      .global_start
      _start:
      MOV R1,#4
      MOV R2,#4
      CMP R1,R2
      beq addfunc
      bne subfunc
      
      addfunc:
      add r3,r1,r2
      b loop
      subfunc:
      sub r4,r1,r2 @if(r1!==r2) r4=r1-r2
      b loop
      loop:
      b loop
      
      .end
      
    2. bl label
      Explanation: Jump to the code where the label label is located, jump at this time, and the lr register saves the return address
      .text
      .global_start
      _start:
      MOV R1,#4
      MOV R2,#4
      CMP R1,R2
      bleq addfunc
      blne subfunc
      
      addfunc:
      add r3,r1,r2
      mov pc,lr @program returns
      subfunc:
      sub r4,r1,r2 @if(r1!==r2) r4=r1-r2
      mov pc,lr @program returns
      loop:
      b loop
      
      .end
      
    3. bx address
      Explanation: Jump to the instruction location corresponding to the address. At this time, the jump LR does not save the return address
      .text
      .global_start
      _start:
      MOV R1,#4
      MOV R2,#4
      MOV R3,#4
      MOV R4,#4
      MOV R5,#4
      MOV R6,#4
      bx r3 @jump to instruction location at address 4
      
      loop:
      b loop
      .end
      
    4. blx address
      Explanation: Jump to the instruction location corresponding to the address. At this time, jump to LR to save the return address
      .text
      .global_start
      _start:
      MOV R1,#4
      MOV R2,#4
      MOV R3,#4
      MOV R4,#4
      MOV R5,#4
      blx r3 @jump to instruction location at address 4
      MOV R6,#4
      
      loop:
      b loop
      .end