Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
cpu [2022/08/20 22:12] – [Multiplication] simercpu [2022/11/12 14:57] (current) – [Registers] pulkomandy
Line 27: Line 27:
 The BP register has a special addressing mode with a 6-bit immediate offset. It is usually (for example by unsp IDE C compiler) used as a frame pointer, to access a function local variables efficiently. But it can also be used as a normal register if needed. The BP register has a special addressing mode with a 6-bit immediate offset. It is usually (for example by unsp IDE C compiler) used as a frame pointer, to access a function local variables efficiently. But it can also be used as a normal register if needed.
  
 +SP is generally used as a stack pointer but can be used for other things occasionally. When using the PUSH and POP instructions, the stack grows downwards (PUSH decrements SP) and the stack pointer points to the first empty space below the stack.
 +
 +As a result, the typical organization of the stack in VTech code is:
 +
 +^Address^ Contents       ^ Notes                              ^
 +| NNNF  | Parameter 0    | Pushed by caller                   |
 +| NNNE  | Parameter 1    |                                    |
 +| NNND  | PC (from CALL) | Saved by CALL                      |
 +| NNNC  | SR (from CALL) | Saved by CALL                      |
 +| NNNB  | R4             | Saved by PUSH in function prelude  |
 +| NNNA  | R3             | (and more registers as needed)     |
 +| NNN9  | Local variable | Space allocated by decrementing SP |
 +| NNN8  | Local variable | <- BP points here                  |
 +| NNN7  | Empty          | <- SP points here                  |
 +
 +The SP can move when calling other functions, interrupts, ... or when saving registers inside the function code. But BP doesn't move and can be used to access both local variables and function parameters.
 +
 +The parameters are usually removed by the caller (by incrementing SP). It would also be possible to use a POP including SR and PC to return from the function while clearing parameters, but this requires loading them into registers, which is not always desirable since registers are rather used for return values.
 ==== Status Register ==== ==== Status Register ====
  
cpu.txt · Last modified: 2022/11/12 14:57 by pulkomandy
CC Attribution 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0