> For the complete documentation index, see [llms.txt](https://hub.bsvblockchain.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hub.bsvblockchain.org/higher-learning/bsv-academy/chapter-3-the-opcodes/08-stack-data-queries.md).

# Legacy - 08 - Stack Data Queries

Stack data queries are opcodes that return information directly relating to the stack or to the data item at the top of the stack.

### Stack Depth Check

The opcode OP\_DEPTH will leave a value on the stack which indicates the depth of the stack. For example, if the stack had 3 items on it, calling OP\_DEPTH would leave the integer '3' as a new item at the top of the stack.

<table><thead><tr><th width="132">Word</th><th width="95">Input</th><th width="132">Output</th><th>Description</th></tr></thead><tbody><tr><td>OP_DEPTH</td><td>items</td><td>items, qty items</td><td>Counts the number of stack items onto the stack and places the value on the top</td></tr></tbody></table>

<figure><img src="https://3421145409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRTEKu4E7Fvtss3GI9CYU%2Fuploads%2Fgit-blob-6745d11331c2e14e720aa3a6599f7ee29a32f78e%2FBSVA-BitcoinScript_Chapter3-AnimationPack09.gif?alt=media" alt=""><figcaption></figcaption></figure>

#### Example:

`OP_DEPTH OP_1 OP_EQUAL OP_IF`

`<pubkey>`

`OP_ELSE`

`OP_DUP OP_HASH160 <pubkeyhash> OP_EQUALVERIFY`

`OP_ENDIF`

`OP_CHECKSIG`

In this example, the script checks the depth of the stack. If there is just 1 item on the stack, it assumes it is a signature and checks it against a public key stored in the script. If there is more than one item on the stack, it assumes this is a public key and signature, which it checks against a stored public key hash.

### Size Query <a href="#size-query" id="size-query"></a>

OP\_SIZE evaluates the first item on the stack and adds its length to the top of the stack.

<table><thead><tr><th width="118">Word</th><th width="91">Input</th><th width="103">Output</th><th>Description</th></tr></thead><tbody><tr><td>OP_SIZE</td><td>item</td><td>item, item size</td><td>Pushes the string length of the top element of the stack (without popping it).</td></tr></tbody></table>

<figure><img src="https://3421145409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRTEKu4E7Fvtss3GI9CYU%2Fuploads%2Fgit-blob-43e622dfe89b7caeac71690afbe6796cb8425136%2FBSVA-BitcoinScript_Chapter3-AnimationPack10.gif?alt=media" alt=""><figcaption></figcaption></figure>

#### Example:

`OP_SIZE OP_10 OP_LESSTHANOREQUAL OP_VERIFY`

In this example, the script checks that an item input to the stack is less than or equal to 10 bytes long. If the string is longer than 10 bytes long, the script fails. This can be useful when testing input conditions from user entered forms that are directly submitted to script.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hub.bsvblockchain.org/higher-learning/bsv-academy/chapter-3-the-opcodes/08-stack-data-queries.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
