51 macro uses stack protection to prevent PC false jumps and eliminate jump restrictions

Source: Internet
Author: User

MCU Programming
Discover several practical problems
1. If you use the stack SP to protect some variables from passing in the called functions in the program process,
That is, use SP in the function. Note that when the function is called, a PC will be pushed to SP, SP + = 2, and then
If you continue to think that the sequence pop is protected in the function
The problem is that after the first step, the PC content is played. Of course, when the last function is ret, the value of pop-out PC is also changed
The first two bytes of content you press in will jump by mistake.
Solution 1: First pop out the Pc value protection in the register, then pop out the incoming data, and then push into the PC
To protect the PC register content from being modified during interruption. This method must also be completed once by pop.
Enter the PC and then use the data. Not very good
2. The use of macros is clear and easy to replace when learning advanced languages.
Push_in_stact macro; a simple macro Application
MoV A, R0
Push ACC
MoV A, r1
Push ACC
Endm

Pop_out_stact macro; simple out-of-stack Application
Pop ACC
MoV R1,
Pop ACC
MoV r0,
Endm
If push_in_stact macro calls
When a stack macro is used in the same process, the stack Macro will return the content accurately, without worrying about the PC press-in error.
2. Solve Short jump commands such as JB cjne
Three-byte jump commands such as JB cannot be addressed outside of 128 commands
Method 1 use the intermediate jump base station for unified ljmp
Method 2 macro
Macro_jb macro A1, label1
JB A1, $ + 5; Use the JB command 3 + ajmp (2) to jump to ljmp label1 for long JMP
Ajmp $ + 5; If A1 is 0, skip ajmp (2) + ljmp (3) to endm.
; Commands
Ljmp label1; for Long Jump
Endm
It is also applicable to other jumps. You can create a number internally to redirect
Achieve the same simplified effect.

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.