We're looking for your comments on how to best organize the wiki's content.
Stack
The stack is a location in memory that is used for temporary storage, particularly when implementing subroutines.
Contents |
[edit] What is a Stack?
A stack is a Last In, First Out (LIFO) data structure that has two main operations:
- PUSH(value) - Adds a value to the top of the stack
- POP() - Reads, then removes the value from the top of the stack
The DCPU-16 also supports the following two operations:
- PEEK() - Reads the value on the top of the stack
- PICK(offset) - Reads a value from inside the stack
[edit] How to Use the Stack in DCPU-16
In the DCPU-16, the stack is located at the end of the memory space, and grows backward. The first item to go on the stack is saved at location 0xffff, the second at 0xfffe, and so on. The Stack Pointer (SP) special purpose register points to the top of the stack.
The stack is manipulated by using the PUSH, POP, and PEEK addressing modes, or by directly manipulating SP. When a value is pushed to the stack, SP is decremented by one, and the value is saved to the new location pointed to by SP. When a value is popped, the value that is pointed to by SP is returned, and SP is incremented by 1.
Address FFF7 FFF8 FFF9 FFFA FFFB FFFC FFFD FFFE FFFF
----------------------------------------------
| ?? | ?? | ?? | ?? | ?? | ?? | 11 | 31 | 22 | ;The initial stack
----------------------------------------------
SP
Address FFF7 FFF8 FFF9 FFFA FFFB FFFC FFFD FFFE FFFF
---------------------------------------------- SET PUSH, 12
| ?? | ?? | ?? | ?? | ?? | 12 | 11 | 31 | 22 | ;Decrement SP, then
---------------------------------------------- ;save 12
SP
Address FFF7 FFF8 FFF9 FFFA FFFB FFFC FFFD FFFE FFFF
---------------------------------------------- SET A, POP
| ?? | ?? | ?? | ?? | ?? | 12 | 11 | 31 | 22 | ;Save 12 in A and
---------------------------------------------- ;increment SP.
SP
When we "remove" a value from the stack, we're actually just moving the stack pointer to the right to allow a future push operation to overwrite the "removed" value.
[edit] Why Use a Stack?
The stack is heavily used when implementing subroutines. A subroutine is a piece of code that is written in one location that can be accessed from anywhere else in the program, often providing useful capabilities such as advanced mathematical functions, I/O, and the like. Here is an example of a program that uses a subroutine:
; This code puts 6 in B, takes the sum of all the ; registers and saves it in memory location 0x100. SET B, 6 JSR add_registers ; <-- pushes PC to stack SET [0x100], A ; -- end of program -- ; This subroutine adds all the registers together, ; and leaves the result in 'A'. :add_registers ADD A,B ADD A,C ADD A,X ADD A,Y ADD A,Z ADD A,I ADD A,J SET PC, POP
The JSR (Jump to SubRoutine) operation pushes the program counter to the stack and sets the program counter to the specified value. Because the program counter is pushed onto the top of the stack, we can pop the program counter back off and return to where we were when we left the main program.
[edit] References
- DCPU-16 specification v1.7 (Copyright 2012 Mojang)
| ||||||||

