> For the complete documentation index, see [llms.txt](https://ret2basic.gitbook.io/ctfwriteup/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ret2basic.gitbook.io/ctfwriteup/rareskills-puzzles/yul-puzzles/01-returnbool.md).

# 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="https://223316867-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVtlSxURaW2QQu6RU5%2Fuploads%2FKTAYJvx6OxABy3xhkDA9%2Fimage.png?alt=media&amp;token=57c2761c-fb7c-4235-a391-438669aea99c" alt=""><figcaption></figcaption></figure>

<figure><img src="https://223316867-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVtlSxURaW2QQu6RU5%2Fuploads%2FtcXJXAm8PUfKH54YgMNN%2Fimage.png?alt=media&amp;token=67508110-8864-482c-b218-73a52eb2d0b9" 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="https://223316867-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVtlSxURaW2QQu6RU5%2Fuploads%2Fy0iisy25qMTePIKQvOjO%2Fimage.png?alt=media&amp;token=d851d794-e14a-47dc-9e10-9a8a35c70173" alt=""><figcaption></figcaption></figure>
