cpct_setStackLocation

Sets a new location for the program stack in memory.

C Definition

void cpct_setStackLocation (void* memory) __z88dk_fastcall;

Input Parameters (2 Bytes)

(2B HL) memoryPointer to the new location in memory where stack will be placed.

Assembly call (Input parameters on registers)

call cpct_setStackLocation_asm

Parameter Restrictions

  • memory has no restiction.  You may place stack whereever you wanted, but you should be cautious: contents of the stack could erase data, code or essential system information.  If that happens, your program may crash.  Have special care with system rst vector (0x0000-0x003F) and firmware memory (if enabled).

Important notes

  • This function only changes program stack to a new location by modifying the stack pointer (SP).  It does not take care of present contents of the stack.  If you want to preserve present contents of the stack, copy them to the new location (you may use cpct_memcpy).
  • Take into account that local variables and return addresses are usually stored at the stack.  When moving the stack, ensure that you copy all of its contents to the new location if you want to preserve them.  Otherwise, you may experience unexpected behaviour.
  • This function requires FIRMWARE to be DISABLED.  Firmware may restore previous location of the stack, yielding unexpected behaviour (typically, program crashes).

Details

Changes the location of the program stack by moving the stack pointer (SP) to a new memory location.  Changing the program stack location is useful to have greater control of it and to enable the 3rd memory bank (0x8000-0xBFFF) as double buffer zone, data storage or code memory.

Destroyed Register values

DE, HL, SP

Required memory

4 bytes

Time Measures

  Case     | microSecs (us) | CPU Cycles
-----------------------------------------
  Any      |       7        |    28
-----------------------------------------
  • This function is identical called from C or from ASM

Credits

This routine was first sugested and developed by Lachlan Keown (@lachlank / CPCWiki) in this thread of CPCWiki This code is based on Lachlan’s.

Sets a new location for the program stack in memory.
Copies a bunch of bytes from one place in memory to other.
Close