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.