
M-51 for TUC52, Issue 1.0 Page 10 January 21, 1998
File: m51-i1.doc
4.2 SYSTEM STACK POINTER
At reset the monitor's stack pointer is initialized to 31 bytes below the top of Internal Data Memory (IDM).
If a processor with 256 bytes of IDM is used the monitor will be initialized to address 0E0H. If a processor
with 128 bytes of IDM is used the monitor will be initialized to address 060H. Parts that have more than
256 bytes of internal data memory will be treated like a part with only 256 bytes (i.e., stack initialized to
0E0H). Be aware that following a breakpoint or single step operation that the monitor will not reinitialize
the stack pointer. The monitor only sets the stack pointer following a reset (code starts from 0x0000)!
Following a break-point, NEXT or ZNEXT instruction the monitor will simply continue to use the stack
pointer that was in use when the TRAP instruction was encountered in the user’s program. So, if the user's
program changed the stack pointer then that's what the monitor will be using following the TRAP. Be sure
that your program provides enough stack space for both your program and the monitor. The monitor needs
about 22 (decimal) bytes of stack for its operations.
4.3 CAUTIONS WITH GO BREAKPOINTS, NEXT AND ZNEXT COMMANDS
Proper operation of the GO, NEXT and ZNEXT commands demand that the user stack value be pointing to
a valid location with about 22 (decimal) bytes of free space. Failure to ensure this will cause the monitor to
act flaky or just plain quit. Use the XS command to set the user's stack pointer to somewhere useful
following power-on or a system crash (i.e., anytime the XDM used by the monitor may have been
overwritten).
If the user's program doesn't eventually get to one of the break-point addresses and the reset button is
pressed to stop the user's program the monitor may leave an LCALL instruction at the break-point
address(es). Following a reset the monitor will do its best to check for this problem and, if detected, restore
the original data at the break-point address(es). However, the monitor isn't always successful at this. You
may need to re-download the your code again to ensure that your code is intact.
Recall that the monitor uses a three byte LCALL instruction as its breakpoint trap instruction. It's
unfortunate that the 8051 doesn't include a single byte call instruction (like the RSTn of the 8080 family).
This would be ideal for use by a simple monitor. The three byte trap instruction causes problems only with
short backwards jumps of either 0, 1 or 2 bytes. These are often found in tight DJNZ loops. If you are
debugging code in areas such as this you have two options when using the monitor. You can either avoid
NEXTing through this part of the code or temporarily add some NOPs so that the backwards jumps are of
three or more bytes for debugging purposes. Once you have resolved the programming problem remove the
NOPs. This problem of NEXTing to short backwards jumps is discussed below in more detail.
4.3.1 A detailed example
For this example assume that at location PC there's a SJMP instruction to location PC-1. The problem
occurs as follows: (1) on receiving a NEXT command the monitor knows where it's going to jump, in this
case to PC-1, (2) the monitor then inserts the three byte LCALL instruction at this address with the LCALL
occupying locations PC-1, PC-0 and PC+1, (the second byte of the LCALL instruction is the high order
address of the function in the monitor where the trap instruction will vector to, the vectored from address
will be left on the stack -- that's why a LCALL instruction is used instead of a LJMP), this second byte of
the LCALL instruction will be placed at the current PC location (in this example), (3) note that the second
byte of the LCALL instruction overwrites the op-code we are now going to execute, (4) the monitor
transfers program control to location PC and the high address byte of the LCALL instruction is interpreted
by the 8051 as an instruction, (5) at this point operation of the monitor is indeterminate, however it's safe to
assume that program control has now been transferred to never-never land. This is why it's best to avoid
single stepping through tight loops with small backwards (less than 3 byte) jumps.