当前位置:网站首页>RTOS series (13): assembly LDR instruction, LDR pseudo instruction, [rn] register indirect reference detailed analysis
RTOS series (13): assembly LDR instruction, LDR pseudo instruction, [rn] register indirect reference detailed analysis
2022-07-25 08:08:00 【Brother pig - Embedded】
RTOS series (1): Basic knowledge of —— Break nesting
RTOS Series articles (2):PendSV function , Why PendSV
RTOS Series articles (3): Why will SysTick and PendSV The priority of is set to the lowest
RTOS Series articles (4): MDK Software emulation + Debug-(printf)-Viewer Usage method
RTOS Series articles (5):C Analysis of the running principle of language program : assembly 、 Stack 、 Stack frame 、 Into the stack 、 Out of the stack 、 Save the scene 、 Restore the scene 、 return
RTOS Series articles (6):Cortex-M3/4 And SP,MSP,PSP,Thread Pattern 、Handler Pattern 、 Kernel mode 、 User mode
RTOS Series articles (7):CM3/4 And LR register 、EXC_RETURN In depth analysis
RTOS Series articles (8): In depth analysis of interrupt processing
RTOS Series articles (9): Analyze the stack frame again 、 The difference between function call and interrupt call
RTOS Series articles (10): Simple OS Sample analysis
RTOS Series articles (11):RTOS Starting mode —— Set up directly CONTROL register 、SVC start-up 、PendSV start-up
RTOS series (12): Use SVC or PendSV start-up OS Detailed process analysis
Preface
assembly LDR Instruction in RTOS It is frequently used in , Especially in PendSV When context switching in ,LDR Instructions are indispensable , We looked at uC/OS、FreeRTOS、RT-Thread Source code when , Can see LDR The figure of . because LDR The instructions are 2 A name :LDR Instructions 、LDR Pseudo instruction , This gives us understanding LDR Brought difficulties , It's easy to confuse . This article will analyze the compilation in detail LDR、[Rn] The principle of indirect quotation .
Rn and [Rn] The difference between
[Rn] Indicates indirect addressing , Like a pointer , In assembly instructions , Rn and [Rn] The difference is that , The former Rn The operation is to directly fetch registers Rn The value in ,[Rn] Register Rn The stored value is considered as an address , Then take the value in this address .
LDR Instructions 、LDR Pseudo instruction
LDR Instructions and LDR Although pseudo instructions are used LDR, But it's not a thing , In fact, it is also easy to distinguish ,LDR With ‘=’ is LDR Pseudo instruction , The others are LDR Instructions .
LDR Pseudo instructions are designed to put a 32 Bit immediate or memory address is stored in a register .CPU In case of LDR Pseudo instruction time , Will judge this 32 Bit immediate or address , To decide whether to use MOV Instructions , Or use LDR Instructions .
LDR Instructions
LDR R0, 0x12345678 ; hold 0x12345678 The values in this address are stored in r0 in . and mov I can't do this job ,mov You can only move data between registers ,
; Or move the immediate number to the register .
LDR R0,R1 ; Express the r1 The value in the register is put into r0
LDR R0,[R1] ; [R1] Express R1 The median corresponds to the address of the memory , So here is a R1 The number in is treated as an address , Put the value in memory pointed to by this address into R0.
; therefore [R1] Equivalent to indirect reference , similar C The pointer to language , Fetch through pointer .
LDR Pseudo instruction
LDR R0, =0x12345678 ; hold 0x12345678 This address is written to r0 It's in . therefore ,ldr Pseudo instructions and mov It's quite similar . It's just mov The instruction limits the length of the immediate to 8 position ,
; That is, no more than 512. and ldr Pseudo instructions have no such limitation . If you use ldr Pseudo instruction time , The immediate number that follows does not exceed 8 position ,
; So in actual assembly, it's time to ldr Pseudo instructions are converted to mov The directive
LDR R0, =_start ; Assign the value of the specified label to r0, Here's the label _start The absolute address of , This absolute address ( Link address ) It was determined at the time of linking .
; It's going to take up 2 individual 32bit Space , One is the command , The other is stored in the text pool _start The absolute address of .
LDR Instructions 、 Pseudo instructions are analyzed with examples
#include "led.h"
#include "delay.h"
#include "sys.h"
#include <stdio.h>
typedef struct tcb{
unsigned int *pstk;
u16 a;
u16 b;
} tcb_t;
static unsigned int currStack[32];
static tcb_t currTCB;
volatile tcb_t *pCurrTCB = NULL;
__asm void asm_func(void)
{
extern pCurrTCB
PRESERVE8
ldr r3, =pCurrTCB /* [1] */
ldr r1, [r3] /* [2] */
ldr r0, [r1] /* [3] */
ldmia r0, {
r4-r11} /* */
nop
}
int main(void)
{
currTCB.pstk = &currStack[31];
currTCB.a = 0x0102;
currTCB.b = 0x0304;
currStack[31] = 11;
currStack[30] = 10;
currStack[29] = 9;
currStack[28] = 8;
currStack[27] = 7;
currStack[26] = 6;
currStack[25] = 5;
currStack[24] = 4;
pCurrTCB = &currTCB;
printf("currStack[31] addr:0x%X\n", &currStack[31]);
printf("currTCB's addr:0x%X\n", &currTCB);
printf("pCurrTCB's addr:0x%X\n", &pCurrTCB);
printf("pCurrTCB's addr:0x%X\n", pCurrTCB);
asm_func();
return 0;
}
Print the results :
&currStack[31]:0x200000AC
&currTCB:0x20000000
&pCurrTCB:0x20000008
pCurrTCB:0x20000000
Memory field analysis 
Critical instruction analysis :
ldr r3, =pCurrTCB /* [1] */
ldr r1, [r3] /* [2] */
ldr r0, [r1] /* [3] */
- take pCurrTCB The address of is assigned as an immediate value to r3, After executing the command ,r3 = 0x20000008, and 0x20000008 The content in the address is 0x20000000
- [r3] The meaning is , Indirect reference , take r3 The value in is converted to a memory address , Then assign the value in this memory address to r1, So we can simply deduce :
a. register r3 The value of 0x20000008
b. take 0x20000008 As memory address .
c. Put the memory address 0x20000008 The content in 0x20000000 Assign a value to r1.- take r1 Use the value in as the address , Take the value pointed to by this address and give it to r0
a. register r1 The value of 0x20000000
b. take 0x20000000 As memory address .
c. Put the memory address 0x20000000 The content in 0x200000AC Assign a value to r0
Finish the above 3 After a command ,r0 The value in is the stack currStack At the top of the stack ,uC/OS、FreeRTOS All use this feature to pass TCB label , Calculate the top position of the task stack , Then save the site / Restore the scene .
Summary
- LDR There are two kinds of instructions , Respectively LDR Instructions and LDR Pseudo instruction , The operand has = Is a pseudo instruction , For pseudo instructions, it can be simply considered that label The address of is assigned to the register .
- [Rn] Is an indirect reference method of assembly instructions , Be similar to C The pointer to ,[Rn] The meaning is , take Rn As a memory address , Then take out the value in the memory address , Assign to another register .
- adopt LDR Instructions 、LDR Pseudo instruction 、[Rn] operation , We can easily use assembly instructions to find RTOS Stack top address of a task in , Then put it into the stack to save the site , Or get out of the stack and restore the scene .
边栏推荐
- 用一个栈实现另一个栈的排序
- Dijkstra sequence (summer vacation daily question 5)
- Machine learning theory and case analysis (Part1) -- Fundamentals of machine learning
- C 43. Get UDP available ports
- Technical Analysis | Doris connector combined with Flink CDC to achieve accurate access to MySQL database and table exactly once
- node+js搭建时间服务器
- Eval and assert one sentence Trojan horse analysis
- 第3章业务功能开发(修改线索,数据回显并修改数据)
- Redis最佳实践
- Test the mock data method of knowing and knowing
猜你喜欢

Raspberry connects EC20 for PPP dialing

Didi eta (estimate the travel time)

Advanced C language (XIII) - Example Analysis of dynamic memory management

Introduction and principle explanation of 30 common sensor modules in IOT embedded devices

JS pop up picture Lightbox light box plug-in spotlight.js

Numpy learning

app耗电量测试

Raspberrypico analytic PWM

Oracle trigger creation

"Unable to recognize" yarn "item as cmdlet, function, script file
随机推荐
Dijkstra序列(暑假每日一题 5)
A queue of two stacks
Using one stack to sort another stack
刷题《剑指Offer》day02
Introduction to machine learning (I): understanding maximum likelihood estimation in supervised learning
Oracle trigger creation
Machine learning theory and case analysis (Part1) -- Fundamentals of machine learning
Didi - dispatching
Teach you to use cann to convert photos into cartoon style
Can Flink's current capabilities support the synchronization of a table from source (MySQL) to sink (MySQL) to separate databases and tables
2022-07-19 Daily: too many icml2022 papers to read? "One sentence comments on 1234 paper highlights" helps you quickly lock
How should enterprise users choose aiops or APM?
Google AI can't understand the comments of netizens, and the wrong meaning will be as high as 30%. Netizens: you don't understand my stem
The value of integer a after bitwise negation (~) is - (a+1)
Introduction and principle explanation of 30 common sensor modules in IOT embedded devices
Node+js build time server
滴滴 - dispatching
ArcGIS Pro scripting tool (10) -- generate.Stylx style symbols from layers
Raspberry pie 4B parsing PWM
Network file storage system (III) practice of fastdfs distributed file system