Deep understanding of SP, LR, and PC

Source: Internet
Author: User

An in-depth understanding of the three registers of ARM is of great benefit to programming and porting the operating system.

1. Stack pointer R13 (SP): Each exception mode has its own R13, which usually points to the stack dedicated to the exception mode, that is to say, the five exception modes, non-exception modes (user mode and system mode) all have their own independent stacks and are indexed using different stack pointers. In this way, when the arm enters the abnormal mode,ProgramThe general register can be pushed into the stack, and the stack is output when the returned result is returned, ensuring the integrity of the State of the program in various modes.

2. Connection register R14 (LR): in each mode, R14 has its own version group, which has two special functions.

(1) Save the return address of the subroutine. When BL or blx is used, the jump command automatically puts the return address into R14. The subroutine copies R14 to the PC to return the result. Generally, one of the following commands is used:
MoV PC, LR
BX lR

Generally, the subprogram is written in this way to ensure that the subprogram can be called in the subprogram.
Stmfd SP !, {LR}
......
Ldmfd SP !, {PC}

(2) When an exception occurs, R14 in the exception mode is used to save the return address of the exception. R14, such as the stack, can handle nested interruptions.

3. program counter R15 (PC): The PC has read and write restrictions. When the read limit is not exceeded, the read value is the instruction address plus 8 bytes. Because the arm command is always aligned with words, bit [1:0] is always 00. When using STR or STM to store PCs, the offset may be other values such as 8 or 12. In V3 and earlier versions, the value of bit [1:0] written is ignored, while the value of bit [1:0] written to R15 in V4 and later versions must be 00, otherwise the consequences cannot be predicted.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.