ARM assembly language (2)

ARM assembly language

I. Introduction

ARM assembly language is a low-level computer instruction set architecture (ISA) language, which is an instruction set architecture on ARM processors, used to write low-level system software, such as operating systems, drivers, and embedded system applications program.

ARM assembly language uses a register-based instruction set, where the data that the instructions operate on is usually stored in the processor's registers rather than in memory. ARM assembly language uses assembly instructions to control the operation of the computer, such as data transfer, arithmetic and logic operations, branch and jump instructions, and so on.

The syntax of ARM assembly language is usually simple and intuitive, and is similar to other assembly languages, but the specific syntax rules will vary with different ARM processor models. Writing ARM assembly language code requires a solid understanding of how the underlying computer hardware and instruction set architecture work.

Second, the addressing mode of the ARM processor

1. Register addressing

An operation that transfers the value of one register to another register.

MOV R1,R2      ;R2→R1,将寄存器R2的值传送到寄存器R1

When the instruction is executed, the value in the register is directly fetched for operation.

2. Immediate addressing

The purpose of the immediate addressing mode is to put the operand immediately after the opcode, and put it in the instruction code segment together with the opcode. When the program is running, the program directly calls the operand without going to other address units to fetch it. Corresponding operands, the above-mentioned operands written in the instruction are also called immediate values.
MOV R0, #0x1234
MOV is the opcode, R0 is the first operand, and 0x1234 is the immediate value

Immediate (immediate) refers to the data value directly specified in the instruction, which must correspond to an 8-bit constant obtained by circularly shifting even bits to the right . Immediate data is usually used for constants, addresses, and offsets in instructions.

How to judge and quickly judge that the immediate value is legal:

  • (1) First of all, express this number in binary, follow along and cycle to see if the maximum interval of "1" in the number is greater than 8 (including the two 1s at the beginning and the end), if it is greater than 8, then this number must be illegal . If it is less than or equal to 8 once, it may be legal.
    For example:
    the binary of 0x1101 is: 0000 0000 0000 0000 000 1 0001 0000 0001 , no matter whether you look at it along the way or in a loop, the maximum interval between two 1s is greater than 8, so it is not an immediate number.

  • (2) Method 1:
    If the maximum interval between two 1s is greater than 8, it must not be an immediate number.
    If the maximum interval of 1 is equal to 8 when viewed in sequence, you can check at this time whether there is an even number of 0s in front of the highest digit 1 or behind the lowest digit 1 of this number. As long as there is one case, this number is legal.
    If the maximum interval of 1 is less than or equal to 8 when viewing in a loop, you can check at this time. When viewing in a loop, whether one of the intervals obtained at both ends is an even number. If one is an even number, this number is legal.

  • (3) Method 2:
    1. Convert the data into binary form, and write it as a group of 4 digits from low to high. If the high-order group is not enough for four digits, add 0 in front of the high-order.
    2. The number of 1, if it is greater than 8, it is definitely not an immediate number, if it is less than or equal to 8, proceed to the following steps.
    3. If there are continuous 0s greater than or equal to 24 in the middle of the data, the cycle is shifted to the left by a multiple of 4, so that the high bits are all 0s.
    4. Find the high-order 1, and remove the previous even-numbered 0.
    5. Find the lower 1, and remove the big even number of 0s behind.
    6. Count the remaining digits, if it is less than or equal to 8 digits, then this number is an immediate number, otherwise it is not an immediate number.

Summary: When judging an immediate number, convert it into binary first, and then take the eight-digit number to see if it can be restored by moving the even-numbered bits left and right.
For example:
1. To determine whether 0X20000018 is an immediate number:
convert the data into binary 0010 0000 0000 0000 0000 0000 000 1 1000 and rotate right 32-3=29 times to get the original number, so it is not an immediate number.
2. Determine whether 0x104 is an immediate number:
convert the data into binary 0000 0000 0000 0000 0000 000 1 0000 01 00. If you take 01000001 and cycle 32-2=30 times to get the original number, if you take 10000010 , cycle 32-1=31 times to get original number.

In ARM assembly language, the immediate value can be represented by the prefix "#", and "0x" is used to represent the hexadecimal value. Note that "#" must be followed by a legal immediate value. For example, in the following command:

ADD R0, R0, #10   ;R0+10→R0 ,将立即数10加到寄存器R0中
MOV R0,#0x1234   ;0x1234→R0

3. Register shift addressing

3.1 LSL: logical shift left

MOV R0,#0xff0000f0    ;#0xff0000f0 →R0
MOV R0,R0,LSL #4     ;R0+R*2^4→R0

Example: 0xff0000f0 is 0xf0000f00 after logical left shift of 4 bits , and the carry output is 0xf.
The vacated bits at the low end of the word in the register are filled with zeros.

3.2 LSR: logical shift right

MOV R0,R0,LSR #4     ;R0+R/2^4→R0

Example: 0xff0000f0 is 0x0ff0000f after 4 bits of logical right shift , and the carry output is 0x0.
The vacated bits at the high end of the word in the register are filled with 0s.

3.3 ASR: Arithmetic shift right

MOV R0,R0,ASR #4     ;R0+R/2^4→R0

Example: 0xff0000f0 is 0xfff0000f after 4 bits arithmetic right shift , and the carry output is 0x0.
Keep the sign bit (the highest bit) unchanged during the shifting process, that is, the source operand is a positive number, and the high-end vacancy of the word is filled with 0, otherwise, it is filled with 1.

3.4 ROR: rotate right

MOV R0,R0,ROR #4     ;R0+R/2^4→R0

Example: 0xff0000f0 is 0x0ff0000f after 4-bit circular shift right , and the carry output is 0x0.
The bits shifted from the low end of the word fill the vacated bits in the high end.

4. Register indirect addressing

The register is the address pointer of the operand, and the operand is stored in the memory.

LDR   R0,[R1]   ;[R1]→R0    取
STR   R0,[R1]    ;R0→[R1]    存

The first instruction transfers the data of the memory whose value is R1 to R0, and the second instruction transfers the value of R0 to the memory whose address is R1.
LDR can also be used as a pseudo-instruction, for example:

LDR r0, =0xFFF0 @伪指令
LDR r0, 0xFFFF @指令

That is, you can directly pass in an arbitrary number, and you don't have to care about the immediate value, because it is parsed at compile time as a load instruction.

5. Base address plus offset addressing

Add the contents of the register to the address offset given in the instruction to obtain the effective address of an operand, usually used to access a unit at an address near the base address.
(1) Front indexing mode
LDR R0,[R1,#4] ;[R1+4]→R0
(2) Automatic indexing mode
LDR R0,[R1,#4]! ;[R1+4]→R0, R1+4→R1,"!" is
the index mode LDR after update address (3)
R0,[R1],#4 ;[R1]→R0,R1+4→R1

Guess you like

Origin blog.csdn.net/weixin_57038791/article/details/130454384