We're looking for your comments on how to best organize the wiki's content.
Register
CPUs have a fixed number of registers used for quick access to values that need to be updated frequently. Unlike memory, access to registers is fast and efficient in terms of clock cycles and code length.
Contents |
[edit] General Purpose Registers
General purpose registers are registers that are used to hold values that need to be changed often, such as counters and the results of arithmetic expressions. The DCPU-16 features 8 general purpose registers, A, B, C, X, Y, Z, I, J. Any operation can read from or write to these registers.
In addition to being used as storage locations, these registers can also be used for memory access, denoted by square brackets. In the expression SET X, [A], X is saved as the value that is held in the memory location pointed to by A. They can also be used as an offset in memory, SET Y, [0x100+A] saves Y to be the value that is A away from memory location 0x100.
This is useful when looping through arrays or strings, for example.
; Print the characters to the screen. SET I, 0 :loop SET [0x8000+I, string+I] ; Here, we print the Ith value in ADD I, 1 ; the string to the Ith spot in IFN [string+I], 0 ; video memory. SET PC, loop :end SET PC, end :string DAT "Hello, world!", 0
[edit] Special Registers
[edit] Program Counter (PC)
- Main article: Control flow
The Program Counter points to the location in memory of the next operation that will be run. It can be used as the source or the destination of an operation (for example, to jump to another location), but can not be used for memory access or as an offset for memory access.
[edit] Stack Pointer (SP)
- Main article: Stack
The Stack Pointer points to the current location in memory that serves as the top of the stack.
Like the Program Counter, the Stack Pointer register may not be used directly as an offset for memory access. Unlike the Program Counter, special keywords are designated for accessing the memory that the Stack Pointer references: PUSH, POP, PEEK and PICK may be used as sources and destinations. PUSH and POP save or restore a value while adjusting the stack pointer, while PEEK and PICK access members of the stack without affecting SP. PEEK functions like [SP], and PICK arg like [SP+arg] (although strictly speaking, [SP] and [SP+arg] are not valid code, as mentioned above).
[edit] Excess (EX)
The Excess (or Extra) register is used to hold the excess data from arithmetic operations that can not fit in 16 bits. It is set automatically by several opcodes, but can not be used for memory access or as an offset for memory access.
[edit] Interrupt Address (IA)
- Main article: Interrupt
The Interrupt Address holds an address in memory to which PC will be set if an interrupt is triggered. If a piece of software to handle software interrupts is in place, IA usually holds the address of the first instruction in that software. This allows external hardware, such as the keyboard or clock to trigger events in the DCPU, rather than requiring manual polling of the device for changes.
The IA register is read and written using IAG and IAS, respectively.
[edit] References
- DCPU-16 specification v1.7 (Copyright 2012 Mojang)
| ||||||||

