How do you set the stack pointer to the last address in the RAM?
Question:
Answer:
In the 38000/740 Series, the area that can be used as the stack is assigned to RAM within addresses 0040h to 01FFh. Even if RAM is assigned after address 0200h, it cannot be used as the stack.
Addresses 0040h to 00FFh are in the zero page and addresses 0100h to 01FFh are in the 1st page. Please select either zero page or the 1st page for the stack. Stacks spanning both pages cannot be used. This page is selected by the stack page selection bit of the CPU mode register. The low-order 8 bits of the stack address are determined by the contents of the stack pointer.
The following explanation uses an MCU with an internal RAM capacity of 512 bytes assigned to addresses 0040h to 023Fh as an example. The stack pointer cannot be set to address 023Fh, which is the last address in the RAM. Set the stack pointer to the last address of the area that can be used as stack (address 01FFh).
An example of stack pointer setting is shown below.
Set the stack pointer before a subroutine call, acceptance of an interrupt or execution of an instruction that uses the stack. We recommend setting the stack pointer in the initialize routine of the program.
SEB | 2,$3B | ;Specify 1st page using the stack page selection bit of the CPU mode register |
LDX | #$FF | |
TXS | ;Set the stack pointer to address 01FFh |
*Instructions which use the stack: PHA, PHP, PLA, PLP, MUL, DIV, JSR, RTS, RTI and BRK
(#104738)
3803H |
3803L |
3823 |
3850A |
3858 |
38D2 |
38D5 |
7544 |
7545 |
7546 |
7547 |
7548 |
7549 |