51 Macro User Stack Protection Prevent PCs from misrepresenting and eliminating jump restrictions

xiaoxiao2021-03-06  39

In the beginning, the microcontroller program is found to find a few practical problems 1. If you try to use the stack sp in the program to protect some variables to the function that is called below, that is, use sp as SP, pay attention to the next PC will be called when the function is called. Pressed in SP, SP = 2, and then if you continue to follow the original idea to think that the order POP is protected, the problem is generated, the advanced, the pop is the PC content, of course, when the final function ret is POP The value of the PC also becomes the 2-byte content you first pressed, and it will be misbere. Solve 1: Pre-POP PC value is protected in the register, and then the PUSH will enter the value of the PC after the data is completed, carefully protect the register content of the PC to be modified when interrupt. This method must also be loaded into the PC once a POP and then uses data. Not very good 2. Use macro to learn the high-level language for the macro machine, simply replace the push_in_stact macro; simple in the stack macro application MOV A, R0 PUSH ACC MOV A, R1 PUSH ACC ENDM POP_OUT_STACT MACRO; Simple outlet application POP ACC MOV R1, A POP ACC MOV R0, AENDM At this time, if the PUSH_IN_STACRO call stack, use the stack macro in the same process, it will return the content accurately, but do not worry because PC is transdermous. 2. Solve JB CJNE The three-byte jump instructions such as short jump instructions JB cannot be addressed outside the 128 instructions, using intermediate jump base station, unified LJMP method, two use macro Macro_JB Macro A1, Label1 JB A1, $ 5; use JB Directive 3 AJMP (2) Jump LJMP Label1 to the long JMP AJMP $ 5; if A1 is 0, the jump AJMP (2) LJMP (3) to the endm end; strip instruction ljmp label1; Long jump ENDM also applies to other jumps, which can be built internally to jump to the same simplified effect.

转载请注明原文地址:https://www.9cbs.com/read-65832.html

New Post(0)