Coding raw WebAssembly Text - Dive into Wat

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ก.ค. 2024
  • Code: github.com/tjpalmer/rio/tree/...
    0:00 - Intro & Demos from Ben
    1:06 - Hello in Wat
    1:49 - Mini Wasm runner
    2:36 - Fibonacci numbers in Zig
    3:30 - Fibonacci numbers in Wat & Wasi
    3:49 - Wasi
    4:33 - Main loop, signedness
    4:57 - Manual stack
    5:28 - Calculating Fibonacci using Wasm stack
    6:05 - Program run, more operand stack, binary size
    7:04 - Print text and numbers using manual stack
    8:04 - String constants
    8:18 - Binary Wasm bytes
    8:41 - Roland demo & Outro
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 25

  • @contextfree
    @contextfree  ปีที่แล้ว +4

    And for an example of coding directly in wasm binary, check out this video from Thomas Ballinger! th-cam.com/video/pkw9USN_Tko/w-d-xo.html

  • @bensmith218
    @bensmith218 ปีที่แล้ว +8

    Thanks for the shout out!
    WebAssembly is a funny format because it looks like a stack machine, but doesn’t have traditional dup/swap/rot operators, and the text format uses s-expressions but otherwise is not at all like a lisp!
    In response to some other comments, WebAssembly is higher level than other machine code formats, primarily in the use of local variables instead of registers, and the requirement to use structured control-flow such as if and loop instead of jumps and branches. Neither of these are needed for safety, however, so it would have been possible to define it a different way.
    By the way, there is a “folded” format for the if instruction. It is written like (if (condition) (then (expr)) (else (expr))).
    Great video as always!

  • @tuffenduffen
    @tuffenduffen ปีที่แล้ว +4

    Having read the wasm spec, this video is very nice because the spec isn't very hands-on to put it lightly. Thank you very much!

    • @BenjaminAster
      @BenjaminAster ปีที่แล้ว +1

      You can look at Dan Gohman's wasm-reference-manual for a more human-readable documentation of WebAssembly.

    • @contextfree
      @contextfree  ปีที่แล้ว

      I missed this document in all my searching for help. Thanks for mentioning it! I think this is the right link? github.com/sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md

    • @BenjaminAster
      @BenjaminAster ปีที่แล้ว +1

      @@contextfree yep

    • @109Rage
      @109Rage ปีที่แล้ว +3

      Yeah, I've gone through the spec before. It's written more for people implementing a WASM VM, and not people trying to write .wat files directly, so it can be rather difficult to parse through imo.

    • @spotandjake1008
      @spotandjake1008 ปีที่แล้ว +1

      @@contextfree Working between this, the wasm spec and mdn seems to be the best option for learning wasm. wat2wasm has a great online demo as well and when you run it with verbose mode on it can be very useful for learning what is what.
      I went through and made a wasm parser and builder in grain which was a fun project the spec is nice in small for wasm though so can be done in about a day or two. but the resources out there are a little lacking given how new wasm is.

  • @MyCodingDiarie
    @MyCodingDiarie ปีที่แล้ว +7

    Your attention to detail and dedication to your craft is truly inspiring. Thank you for sharing your expertise with us!🖤❣

    • @contextfree
      @contextfree  ปีที่แล้ว

      Glad you enjoy the videos!

  • @shilangyu
    @shilangyu ปีที่แล้ว +2

    Very nice showcase. Makes me seriously consider Wat as the target for my toy language. It seems legible enough to do easy debugging on it. It can also compile to standalone executables, which is always desirable.

    • @contextfree
      @contextfree  ปีที่แล้ว +2

      I would actually like to go straight to binary wasm (ideally with some helper library) and use wasm2wat to inspect. Got to generate valid binary at each step for that, though, I suppose.

    • @spotandjake1008
      @spotandjake1008 ปีที่แล้ว +1

      Using binaryen it is rather easy to get something working once you have a front end anyways.

  • @andrewdunbar828
    @andrewdunbar828 ปีที่แล้ว +2

    This was nice to see. I had a tiny bit of a play with WAT a year or so ago to see if it would bring back the nostalgia of doing machine code on the Z80 and assembly on the 68000.
    I didn't play with AssemblyScript though, are you thinking of having a look at that?

  • @109Rage
    @109Rage ปีที่แล้ว +5

    2:40 - I take it you're using 0.10.1 Zig? Current way to iterate through integers is for(0..10) |n| {}

    • @contextfree
      @contextfree  ปีที่แล้ว +3

      Didn't realize they were improving that. Thanks for the info!

    • @VojtaJavora
      @VojtaJavora ปีที่แล้ว +1

      Oh, cool. I did not know that. Some time ago, I tried to Google how to do this but came back only with weird hacks about comptime array building or something.

  • @gamerzero6085
    @gamerzero6085 ปีที่แล้ว +10

    Lol, didn't knew wasm text representation is lisp. That's kinda curious

    • @RodyDavis
      @RodyDavis ปีที่แล้ว

      Same, just now realized this

    • @adamih96
      @adamih96 ปีที่แล้ว

      WASM is different in that it is fully stack based like Forth

    • @gagagero
      @gagagero ปีที่แล้ว

      Most intermediate representation is made up of s-expressions.

  • @enirya
    @enirya ปีที่แล้ว +1

    one thing that bugged me about WAT was that the rules for parentheses arent strict like lisp and it made writing it feel weirdly inconsistent

    • @contextfree
      @contextfree  ปีที่แล้ว +1

      As it's not really lisp anyway, I wish I could add parenthesized args without parenthesizing the whole statement, but then it probably needs to worry about newlines or something. I didn't explore the corners of the grammar, but I suspect it's not aware of newlines.

  • @stevenhe3462
    @stevenhe3462 ปีที่แล้ว +1

    So, it is much higher-level than machine assembly languages, with strong inspiration from Lisp.

    • @contextfree
      @contextfree  ปีที่แล้ว +2

      I wouldn't worry the syntax so much. But yeah it's higher level than common hardware asm. I'm not sure which things are for convenience and which for safety. Haven't studied that enough.