# 01 - ReturnBool

**Goal:** Return a boolean value (`true` or `false`) from the Yul assembly block.

{% code title="ReturnBool.t.sol" %}

```solidity
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.13;

import {Test, console} from "forge-std/Test.sol";
import {ReturnBool} from "../src/ReturnBool.sol";

contract ReturnBoolTest is Test {
    ReturnBool public c;

    function setUp() public {
        c = new ReturnBool();
    }

    function test_ReturnBool() public {
        bool r = c.main();
        assertEq(r, true);
    }
}
```

{% endcode %}

In Yul, to return data we must explicitly store the value in memory and use the `return` opcode with a pointer and size. Booleans in the EVM are represented as 32-byte values (1 for true, 0 for false). To return `true`, we store 1 in memory and return 32 bytes; for `false`, we store 0.

{% code title="ReturnBool.sol" %}

```solidity
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.13;

contract ReturnBool {
    function main() external pure {
        assembly {
            // Store boolean true (1) at memory position 0
            mstore(0, 1)
            // Return 32 bytes from memory position 0 (bool true)
            return(0, 32)
        }
    }
}
```

{% endcode %}

**Explanation:** We use the `mstore` opcode to write to memory, and then `return(0, 32)` to output 32 bytes starting at memory position 0. The value `1` will be interpreted as `true` by Ethereum tools (and `0` as `false`). Memory is byte-addressable, so writing at offset 0 is convenient for returning data, although not memory safe (it overwrites the "scratch space for hashing methods", read more [here](https://docs.soliditylang.org/en/latest/internals/layout_in_memory.html)).

<figure><img src="/files/YmwGg8x7ervpPoxgo4jG" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/OFpprnlhebqoNywuOPvz" alt=""><figcaption></figcaption></figure>

**Why not use free memory pointer?**

In this scenario we just overwrite the scratch space with a single byte, it will not overwritten important memory regions. In future levels we will use free memory pointer if the data is long.

Run test:

```bash
forge test --mp test/ReturnBool.t.sol -vvvv
```

<figure><img src="/files/Y4W9ELbcDZOt6NALAIr7" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ret2basic.gitbook.io/ctfwriteup/rareskills-puzzles/yul-puzzles/01-returnbool.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
