Download M16C Assembler Manual

Transcript
Programming Style
4
4.5 A Little Tips...
Saving and Restoring to and from Stack Area
Registers and internal other resources are saved and restored to and from the stack area in the
following cases:
(1) When an interrupt is accepted
When an interrupt is accepted, the registers listed below are saved to the stack area.
Program counter (PC) → 2 low-order bytes
Flag register (FLG) → 2 bytes ... Total 4 bytes
After the interrupt is serviced, the above registers that have been saved to the stack area are
restored from the stack by the REIT instruction.
Stack area
m-4
Program counter, low (PCL)
m-3
Program counter, middle (PCM)
m-2
Flag register, low (FLGL)
Flag register,
high (FLGH)
m-1
Stack pointer
before interrupt
occurs
SP
(Stack pointer after
interrupt is accepted)
Program counter,
high (PCH)
m
Figure 4.5.1 Saving and restoring to/from stack when interrupt is accepted
(2) When subroutine is called (when JSR, JSRI, or JSRS instruction is executed)
When the JSR, JSRI, or JSRS instruction is executed, the following register is saved to the
stack area.
Program counter (PC)→ 3 bytes ... Total 3 bytes
After subroutine execution is completed, the above register that has been saved to the stack
area is restored from the stack by the RTS instruction.
Stack area
Stack pointer before
subroutine is called
m-3
Program counter, low (PCL)
m-2
Program counter, middle ( (PCM)
m-1
Program counter, high (PCH)
SP
(Stack pointer after
subroutine is called)
m
Figure 4.5.2 Saving and restoring to/from stack when subroutine is called
165