NEWTON

NEWTON


Popular tags

    How to make output like "hello world" in Cairo Language (StarkNet)?

    Asked

    4 months ago

    601

    views


    7

    Can you help how to change the code so that it outputs 400 as well?

    // The following code outputs the numbers 100, 200, 300.
    // 1. Click on "Run" to see the output.
    // 2. Change it to output 400 as well.
    
    // Use the output builtin.
    %builtins output
    
    // Import the serialize_word() function.
    from starkware.cairo.common.serialize import serialize_word
    
    func main{output_ptr: felt*}() {
    // Output 100 by calling serialize_word.
    serialize_word(100);
    // Output 200.
    serialize_word(200);
    // Output 300.
    serialize_word(300);
    // Return.
    return ();
    }
    

    Answers to this question are a part of the ✨ 17 days of Cairo Lang with Playground & Newton. ✨ 

    Vote for your favorite answer - the best answer will win a $10 award. A new day – a new reward! During the next 17 days, our goal is to attract more developers to the Cairo language and to systematize the knowledge of Cairo lang. Read rules

      #17daysOfCairocairo-beginnerscairoplayground

    Newton

    asked

    4 months ago


    20 answers

    5

    Accepted answer

    Solution

    Just add serialize_word(400);

    Explanation

    The function serialize_word(): To write the value x to the output, we can use the library function serialize_word(x). serialize_word gets one argument (the value we want to write) and one implicit argument output_ptr (which means that behind the scenes it also returns one value). In fact it’s quite simple: it writes x to the memory cell pointed by output_ptr (that is, [output_ptr]) and returns output_ptr + 1. Now the implicit argument mechanism kicks in: in the first call to serialize_word() the Cairo compiler passes the value of output_ptr as the implicit argument. In the second call it uses the value returned by the first call.

    Source: https://www.cairo-lang.org/docs/hello_cairo/intro.html

    Code

    // The following code outputs the numbers 100, 200, 300.
    // 1. Click on "Run" to see the output.
    // 2. Change it to output 400 as well.
    
    // Use the output builtin.
    %builtins output
    
    // Import the serialize_word() function.
    from starkware.cairo.common.serialize import serialize_word
    
    func main{output_ptr: felt*}() {
    // Output 100 by calling serialize_word.
    serialize_word(100);
    // Output 200.
    serialize_word(200);
    // Output 300.
    serialize_word(300);
    // Output 400.
    serialize_word(400); // << Solution
    // Return.
    return ();
    }
    

    Program Output

    Program output:
      100
      200
      300
      400
    
    Number of steps: 22
    Program hash: 0x0542e6604d143454ef2a63b5e6762616c9999e2224bc7d6098a1b9617fa9dd72
    Program output:
      100
      200
      300
      400
    
    Number of steps: 22
    Program hash: 0x0542e6604d143454ef2a63b5e6762616c9999e2224bc7d6098a1b9617fa9dd72
    

    juancito.eth

    answered

    4 months ago

    0

    I have created a repository for the cairo challenge https://github.com/chococrypto/17daysOfCairo

    here is my answer

    // Use the output builtin.
    %builtins output
    
    // Import the serialize_word() function.
    from starkware.cairo.common.serialize import serialize_word
    
    func print_hundredths{output_ptr: felt*}(max, init) {
        serialize_word(init);
        if(init == max) {
            return();
        }
        print_hundredths(max, init = init + 100);
        return();
    }
    
    func main{output_ptr: felt*}() {
        print_hundredths(400, 100);
        return ();
    }
    

    4

    Please have a look at this link.

    As you can see, the way to write to the output in Cairo is to actually write your value to the output_ptr, which you have provided as an implicit argument to your main function (read here for implicit arguments). So what is serialize_word doing in the background?

    You find the code for it here, where you can see that the function simply stores your value in the output_ptr and moves the pointer forward by one. So to ouput 400 you could add a serialize_word(400); to your function, or you could do the below also:

    %builtins output
    
    func main{output_ptr: felt*}() {
    // write straight to the output pointer
    // move it by one each time you write in it
    assert [output_ptr] = 100;
    assert [output_ptr + 1] = 200;
    assert [output_ptr + 2] = 300;
    assert [output_ptr + 3] = 400;
    // reassign the output pointer by the amount of 
    // times you wrote in it
    let output_ptr = output_prt + 4;
    return ();
    }
    

    Just compile the above code with cairo-compile main.cairo --out compiled_cairo.json and run it using cairo-run --program compiled_cairo.json --layout=small --print_output, you should be getting:

    Program output:
      100
      200
      300
      400
    
    Number of steps: 10
    

    where you can see that we reduced the numbers of step for the execution by more than 2!

    answered

    4 months ago

    1

    Code

    // The following code outputs the numbers 100, 200, 300.
    // 1. Click on "Run" to see the output.
    // 2. Change it to output 400 as well.
    
    // Use the output builtin.
    %builtins output
    
    // Import the serialize_word() function.
    from starkware.cairo.common.serialize import serialize_word
    
    func main{output_ptr: felt*}() {
        // Output 300.
        serialize_word(400);
        // Return.
        return ();
    }
    
    

    Output

    Program output:
      400
    
    Number of steps: 7
    Program hash: 0x032772c76f12387460a127b421a49bbe955b8fc9dd67b1f3a3dbcbabe76eb34b
    

    0x9aa4...7B759A

    answered

    4 months ago

    0

    // Use the output builtin. %builtins output

    // Import the serialize_word() function. from starkware.cairo.common.serialize import serialize_word

    func main{output_ptr: felt*}() { // Output 100 by calling serialize_word. serialize_word(100); // Output 200. serialize_word(200); // Output 300. serialize_word(300); // Output 400. serialize_word(400); // Return. return (); }

    0x94C4...834517

    answered

    4 months ago

    0

    // The following code outputs the numbers 100, 200, 300, 400.
    // 1. Click on "Run" to see the output.
    // 2. Change it to output 400 as well.
    
    // Use the output builtin.
    %builtins output
    
    // Import the serialize_word() function.
    from starkware.cairo.common.serialize import serialize_word
    
    func main{output_ptr: felt*}() {
        // Output 100 by calling serialize_word.
        serialize_word(100);
        // Output 200.
        serialize_word(200);
        // Output 300.
        serialize_word(300);
        // Output 400.
        serialize_word(400);
        // Return.
        return ();
    }
    

    0

    // The following code outputs the numbers 100, 200, 300. // 1. Click on "Run" to see the output. // 2. Change it to output 400 as well.

    // Use the output builtin. %builtins output

    // Import the serialize_word() function. from starkware.cairo.common.serialize import serialize_word

    func main{output_ptr: felt*}() { // Output 100 by calling serialize_word. serialize_word(100); // Output 200. serialize_word(200); // Output 300. serialize_word(300); // Output 400. serialize_word(400); // Return. return (); }

    Asten

    answered

    4 months ago

    0

    // The following code outputs the numbers 100, 200, 300.
    // 1. Click on "Run" to see the output.
    // 2. Change it to output 400 as well.
    
    // Use the output builtin.
    %builtins output
    
    // Import the serialize_word() function.
    from starkware.cairo.common.serialize import serialize_word
    
    func main{output_ptr: felt*}() {
    // Output 100 by calling serialize_word.
    serialize_word(100);
    // Output 200.
    serialize_word(200);
    // Output 300.
    serialize_word(300);
    
    // Outpit 400
    ****serialize_word(400);****
    
    
    // Return.
    return ();
    }
    

    0xfC2F...EC0d87

    answered

    4 months ago

    1

    Things that can be learned from this exercise

    • serialize_word call takes 5 steps
    • serialize_word adds the passed felt to the output pointer
    • Said command is useful when debugging

    Solution

    // Use the output builtin. %builtins output // Import the serialize_word() function. from starkware.cairo.common.serialize import serialize_word func main{output_ptr: felt*}() { // Output 100 by calling serialize_word. serialize_word(100); // Output 200. serialize_word(200); // Output 300. serialize_word(300); // Output 400. serialize_word(400); // Return. return (); }

    Output

    First run

    Program output:
      100
      200
      300
    
    Number of steps: 17
    Program hash: 0x06dfe1be49c593f8b693c70a6e9ce50ea84272d76360a46dfb26d6b536d94b77
    

    Second run

    Program output:
      100
      200
      300
      400
    
    Number of steps: 22
    Program hash: 0x0542e6604d143454ef2a63b5e6762616c9999e2224bc7d6098a1b9617fa9dd72
    

    0

    // The following code outputs the numbers 100, 200, 300. // 1. Click on "Run" to see the output. // 2. Change it to output 400 as well.

    // Use the output builtin. %builtins output

    // Import the serialize_word() function. from starkware.cairo.common.serialize import serialize_word

    func main{output_ptr: felt*}() { // Output 100 by calling serialize_word. serialize_word(100); // Output 200. serialize_word(200); // Output 300. serialize_word(300); // Solution - Output 300 serialize_word(400); // Return. return (); }

    0xf2c1...A516b0

    answered

    4 months ago

    1

    Solution

    // Use the output builtin.
    %builtins output
    
    // Import the serialize_word() function.
    from starkware.cairo.common.serialize import serialize_word
    
    func main{output_ptr: felt*}() {
    // Output 100 by calling serialize_word.
    serialize_word(100);
    // Output 200.
    serialize_word(200);
    // Output 300.
    serialize_word(300);
    // Output 400.
    serialize_word(400);
    // Return.
    return ();
    }
    

    You only need to pass the value that you want to output, as an argument to the serialize_word() function. In this case 400:

    // Output 400.
    serialize_word(400);
    

    serialize_word() appends a single word to the output pointer, and returns the pointer to the next output cell.

    For more reference: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/common/serialize.cairo

    0x68Db...576a29

    answered

    4 months ago

    0

    func main{output_ptr: felt*}() { // Output 100 by calling serialize_word. serialize_word(100); // Output 200. serialize_word(200); // Output 300. serialize_word(300);

    //Solution
    serialize_word(400);
    
    return ();
    

    }

    0x0129...93AE11

    answered

    4 months ago

    4

    Explanation of components used in code:

    builtins: Cairo supports the implementation of predefined operations called builtin. Our program communicates with the external world by making use of the output builtin,which is specified by the directive %builtins output.

    Implicit arguments: in curly brackets are required for the storage operations. it stores the addresses of the output. This automatically adds an argument and a return value to the function.

    serialize_word: The library function serialize_word can be used to print the value  to the output. serialize_word takes one argument and one implicit argument output ptr 

    Solution

    Add one more serialize_word function which accepts 400 as an argument.

    // Use the output builtin.
    // inscribed in the CPU(preprocessor)
    %builtins output
    
    // Import the serialize_word() function.
    from starkware.cairo.common.serialize import serialize_word
    
    func main{output_ptr: felt*}() {
    // Output 100 by calling serialize_word.
    serialize_word(100);
    // Output 200.
    serialize_word(200);
    // Output 300.
    serialize_word(300);
    // Output 400.
    serialize_word(400);
    // Return.
    return ();
    }
    

    1

    The solution is to add one extra serialize_word call in the end:

    %builtins output
    
    from starkware.cairo.common.serialize import serialize_word
    
    func main{output_ptr: felt*}() {
        // Output 100.
        serialize_word(100);
        // Output 200.
        serialize_word(200);
        // Output 300.
        serialize_word(300);
        // Output 300.
        serialize_word(300);
    
        // Solution: Output 400.
        serialize_word(400);
    
        return ();
    }
    

    0xB883...B7956a

    answered

    4 months ago

    2

    %builtins output
    
    from starkware.cairo.common.serialize import serialize_word
    
    func main{output_ptr: felt*}() {
      serialize_word(100);
      serialize_word(200);
      serialize_word(300);
      // Solution Output 400.
      serialize_word(400);
      return ();
    }
    

    0xB677...2a4AA6

    answered

    4 months ago

    Your answer

    NEWTON

    NEWTON