mentioned above
When writing bugs on a daily basis, I don’t know if I have experienced such a situation. I just added an extra temporary variable to the function, and as a result, the program execution was abnormal. One of the situations I encountered was that the function program was out of bounds when accessing the array, but it was still working normally. Just because a temporary variable was added, the stack content changed, and because the offset of a variable happened to be set to the LR register when operating the array, the subroutine returned incorrectly and the program execution logic was abnormal.
The above-mentioned problems cannot be discovered by regular debugging, and even the place where the problem occurs cannot be located.
all say that assembly code is the last life-saving straw for programmers. Next, let’s introduce the most commonly used STM/LDM instructions in assembly.
LDM is translated as Load Multiple registers.
STM is translated as Store Multiple registers.
The syntax format is as follows:
LDM{addr_mode}{cond} Rn{!}, reglist{^}STM{addr_mode}{cond} Rn{!}, reglist{^}addr_mode
LDM and STM The instruction provides four different addressing modes. The addressing mode determines the behavior of the base register and is described in the table below.
- I is Increment (increment)
- D is Decrement (decrement)
- B is Before
- A is After
mode determines whether the base address register increases or decreases before the instruction is executed or after the instruction is executed. The
LDM and STM instructions can also be used to push or pop registers from the stack.
- D is Descending (descending order).
- A is the first letter of the word Ascending.
- F is the first letter of the word Full, which means that the current stack pointer points to the last element pushed onto the stack.
- E is the first letter of the word Empty, which means that the current stack pointer points to the next free space.
These four groups describe whether the growth direction of the stack is descending or ascending order, and whether the current stack pointer points to the last element in the stack or to free space.
Cond
conditional instructions are only executed when the condition flag in the program status register matches. For example, BEQ (B instruction with EQ condition) only branches when the Z flag is set. If the {cond} field is empty, the command is always executed.
Rn
Rn is the base address register, and the ARM register holds the initial address of the transmission. Rn cannot be PC.
!
is an optional suffix. if ! When present, writes the final address back to Rn.
reglist
is a list of one or more registers to load, enclosed in curly braces. It can contain register ranges. If it contains multiple registers or register ranges, they must be separated by commas. Any combination of registers R0 to R15 (PC) can be transferred in ARM state, with some restrictions in Thumb state.
^
is an optional suffix, only available in the ARM state. You may not use it in user mode or system mode. Data is transferred to or from user-mode registers, not the current mode registers.
example
Finally, look at an example, as shown below, there is a line assembly instruction in the upper right corner.
STMDB R13!, {R4-R6,R14}Note that when this line of code is executed, the address of the stack pointer (SP) is 0x3FFFC00. The above picture is the result after execution.
step by step analysis, STM is a storage instruction, DB is interpreted as the current stack address is in descending order and the current stack pointer points to the last element in the stack.Therefore, the SP must point to the next free address before the instruction is executed, and then decrement the SP address after execution. Because
has!, the last address needs to be written back to Rn (that is, SP) after the instruction is executed. So the final SP address is 0x3fffbf0
. Note that because the stack address is decreasing, R14 is pushed into the stack first, from right to left. If the stack is incrementing, the opposite is true.
Have you failed in your studies?
END
There are really many things to learn about the embedded Internet of Things. Don’t learn the wrong route and content, which will cause your salary to go up!
will share an information package with you for free, which costs almost 150 gigabytes. The learning content, interviews, and projects are relatively new and comprehensive! It is estimated that buying a certain fish online will cost at least dozens of dollars.
Click here to find an assistant to get 0 yuan:
Reprinted from: typedef
The article comes from ARM assembly STM & LDM How much do you know
Original link: https://mp.weixin.qq.com/s/FZaBy_HAkRlg-TlGicWJuw