5. Memory
SC uses the following memory organization:
5.1. Internal memory
- 0x00 (register R0) is used as operand.
- 0x01 (register R1) is used for repeat loops.
- 0x02-0x07 (registers R2-R7) is used for function arguments and local variables.
- 0x08-0x0D is used for passing arguments to a function.
- 0x0E-0x7F is used for internal data fields, and the remaining space for the stack.
- 0x80-0xFF is used for the stack.
5.2. External memory
SC assumes the code memory and the external memory are the same.
- 0x0000-0x3FFF is used for code.
- 0x4000-0x4FFF is used for external data fields.
5.3. Stack
The stack resides in internal memory and starts at the first available address after 0x20.
The stack can grow up to 0xFF. This does not overlap with the special function registers,
because the 8051 treats the stack address like an indirect address. If the stack grows further, it
wraps to 0x00 and behaviour is undefined. Future versions of the SC compiler can optionally add
code to check for stack exhaustion and halt execution when this occurs.
If no data fields are located in internal memory, the stack can grow up to a size of 242 bytes. If
the maximum number of data fields are located in internal memory, the stack can grow up to a size of
128 bytes.
The stack is used by the following operations:
- Intermediate expression values, including variables and constants. Each expression value uses the
size of the data type. When a part of an expression is evaluated, its arguments are popped from
the stack, and the result is pushed. See the source code for each language element for the precise
order.
- Function calls: 2 bytes (return address) + 1 byte for each local variable and argument. Since
there can be at most 6 local variables and arguments, a function call costs at most 8 bytes.
- Interrupts: Interrupt handlers are functions which also save the PSW, DPTR, A, B and R0
registers. A handled interrupt costs 8 bytes + 1 byte for each local variable + the maximum stack
usage for all expressions.
Since there are two interrupt priority levels, there could be two interrupts being handled at the
same time.