-
Notifications
You must be signed in to change notification settings - Fork 0
Using Memory
A program which only has 32 bytes of register storage available obviously won't be very useful. It is for this reason that Chip16 has 4KB of memory for the programmer to use directly. As detailed in the Chip16 Architecture, memory is accessed through a pointer M. This means that storing data in RAM is slightly slower than keeping the data in a register but is necessary should you need to store more information than will fit in a register.
There are 3 opcodes for working with the memory pointer:
- SMP - ANNN - Sets M to be the 12-bit value NNN.
- RMP - EX1D - Sets register X to the value of M.
- MPAR - EX1E - Increments M by the lower 12-bits of register X.
I shall demonstrate these below:
SMP
code = [
0xA1, 0x04, # smp 0x104 # M = 0x104
]RMP
code = [
0xA1, 0x04, # smp 0x104 # M = 0x104
0xE0, 0x1D, # rmp r0 # r0 = M
]MPAR
code = [
0x60, 0x01, # acr r0, 0x1 # r0 = 1
0xE0, 0x1E, # mpar r0 # M += r0 & 0xFFF
]There are a further 2 instructions for storing and retrieving values from memory:
Mnemonic - Opcode - Description
- SPL - EX55 - Stores the value held in register X in the memory address pointed to by M.
- LD - EX65 - Retrieves the value held in the memory address pointed to by M and stores this value in register X.
Note: values are stored in memory in a big endian format, meaning that the high order byte comes first in memory.
I shall demonstrate the use of these opcodes below:
SPL
code = [
0xA0, 0x00, # smp 0x000 # M = 0
0x60, 0x42, # acr r0, 0x42 # r0 = 0x42
0xE0, 0x55, # spl r0 # *M = r0
]LD
code = [
0xA0, 0x00, # smp 0x000 # M = 0
0xE0, 0x65 # ld r0 # r0 = *M
]Note: The program code is stored in the same address space as your data, so be careful not to overwrite important code with data. Conversely you may find it useful to overwrite code that is guaranteed not to be executed again with data to maximise your use of the available memory.
Next: Random Number Generation