Hinting tutorial/The stack

From DejaVu

Jump to: navigation, search

Hinting tutorial index | Previous: Introduction | Next: Stack manipulation

Contents

How it works

A thing you need to know inside out while hinting is how the stack works. The stack serves as the memory, and it is used for giving parameters, like point numbers, to functions. So when you want to do something like this:

function(3)

you now have to put the 3 on the stack first, and then call function, like:

PUSHB_1
  3
function

We will explain the exact syntax later. PUSHB_1 will just put one number on the stack, namely the number that comes after it. Putting a number on the stack is called "pushing". function pops a number from the stack, which means, it takes a number from it.

If you ever worked with stacks, you know that it's a last in - first out (LIFO) data structure. That means that whatever you put in last, will be retrieved first. So when we do:

PUSHB_2
  5
  3
function

The function will be called with parameter 3, and not with parameter 5 (our hypothetical function takes only one parameter). If you never worked with stacks: see it as a pile of papers: pushing a number means adding a paper with that number on top of the pile. You can only take the top paper from the stack.

It's common to put a large amount of numbers on the stack at once, and then call all functions, like:

PUSHB_2
  5
  3
function_1
function_2

This will first call function_1 with parameter 3, and then function_2 with parameter 5. Syntax like this will make it a lot harder to read through the instructions, since you have to keep an eye on what numbers you already popped from the stack. But it has the advantage that the instructions take much less space than when you push the parameters before each function.

Furthermore, it is perfectly allowed to push more items on the stack when the stack is not empty.


Syntax

PUSHB_1, PUSHB_2, ..., PUSHB_8

(PUSH 1...8 Bytes)

  • Pushes 1, 2, ... or 8 bytes on the stack.
  • Bytes are numbers that can vary between 0 and 255.
  • The numbers that have to be pushed just follow this function.
  • The numbers will be pushed in the same order as they appear after the PUSHB_# function, so the last number will be top of the stack after all pushes.

example:

PUSHB_3
 5
 3
 1

After this, there will be three more items on the stack. Number 1 will be top of the stack (and will thus be popped first).


NPUSHB

(PUSH N Bytes)

  • Pushes an amount of bytes on the stack.
  • Bytes are numbers that can vary between 0 and 255.
  • The number of bytes that have to be pushed is the first number following the NPUSHB.
  • The bytes that have to be pushed follow this number.
  • The numbers will be pushed in the same order as they appear after the NPUSHB function, so the last number will be top of the stack after all pushes.

example:

NPUSHB
 3
 9
 6
 2

This tells that 3 numbers have to be pushed, namely 9, 6 and 2. After this, number 2 will be top of the stack (and will thus be popped first).


PUSHW_1, PUSHW_2, ..., PUSHW_8

(PUSH 1...8 Words)

  • Pushes 1, 2, ... or 8 words on the stack.
  • Words are numbers that can vary between -32768 and 32767.
  • You should only use this when you need large or negative numbers, use PUSHB_# when you can.
  • The numbers that have to be pushed just follow this function.
  • The numbers will be pushed in the same order as they appear after the PUSHB_# function, so the last number will be top of the stack after all pushes.

example:

PUSHW_2
 -64
 1000

After this, there will be two more items on the stack. Number 1000 will be top of the stack (and will thus be popped first).


NPUSHW

(PUSH N Words)

  • Pushes an amount of words on the stack.
  • Words are numbers that can vary between -32768 and 32767.
  • You should only use this when you need large or negative numbers, use NPUSHB when you can.
  • The number of words that have to be pushed is the first number following the NPUSHW.
  • The words that have to be pushed follow this number.
  • The numbers will be pushed in the same order as they appear after the NPUSHW function, so the last number will be top of the stack after all pushes.

example:

NPUSHW
 3
 300
 -64
 2

This tells that 3 numbers have to be pushed, namely 300, -64 and 2. After this, number 2 will be top of the stack (and will thus be popped first).

Personal tools