# Using Zig with WebAssembly

**URL:** <https://ziggit.dev/t/using-zig-with-webassembly/3478>\
**Category:** Media\
**Tags:** wasm\
**Created:** [March 6, 2024, 2:07am UTC](https://ziggit.dev/t/using-zig-with-webassembly/3478 "2024-03-06T02:07:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjg](https://ziggit.dev/user_avatar/ziggit.dev/mjg/32/1897_2.png) [@mjg](https://ziggit.dev/u/mjg)\
**Post date:** [March 6, 2024, 2:07am UTC](https://ziggit.dev/t/using-zig-with-webassembly/3478/1 "2024-03-06T02:07:51Z")

</div>

Hello, I’m quite new to Zig, recently I’ve been playing with using it for WASM and had some (mis)adventures, so I wrote up all the things I learned:

> **[Using Zig with WebAssembly](https://blog.mjgrzymek.com/blog/zigwasm)**
>
> How to send numbers, strings, and arbitrary objects to Zig WASM

If anyone knows

1. What is `__stack_pointer`? (I also asked this on [SO](https://stackoverflow.com/questions/78093023/what-is-stack-pointer-in-zig-wasm))
2. What’s happening to pages 15 and 16 in section Memory?

it would be a help. Thanks!

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [March 6, 2024, 6:00am UTC](https://ziggit.dev/t/using-zig-with-webassembly/3478/2 "2024-03-06T06:00:07Z")

</div>

Hello @mjg, welcome to the forum 🙂  
Thank you for sharing your web assembly journey. ❤

> [@mjg](#):
>
> What is `__stack_pointer`?

I found a reference in `Thread.zig` source in standard library.  
It is used for thread bookkeeping in wasi threads.  
A source code comment in `Thread.zig` is:

```zig
// restore the original stack pointer so we can free the memory
// without having to worry about freeing the stack

```

---

<div class="post-metadata">

**Author:** ![TUSF](https://ziggit.dev/user_avatar/ziggit.dev/tusf/32/456_2.png) [@TUSF](https://ziggit.dev/u/TUSF)\
**Post date:** [March 6, 2024, 6:26am UTC](https://ziggit.dev/t/using-zig-with-webassembly/3478/3 "2024-03-06T06:26:29Z")

</div>

> [@mjg](#):
>
> What is `__stack_pointer`? (I also asked this on [SO](https://stackoverflow.com/questions/78093023/what-is-stack-pointer-in-zig-wasm))

This is part of [Wasm’s tool conventions](https://github.com/WebAssembly/tool-conventions). You can’t just have a bunch of WASM libraries interact with each other, share memory, and so on without any way to coordinate things like where the stack and heap are. This is separate from the stack of the Wasm runtime itself, and is instead part of Wasm’s linear memory.

Looking in the convention on dynamic linking:

> - `env.__stack_pointer` - A mutable `i32` global representing the explicit stack pointer as an offset into the above memory.

This should (?) make it safer (?) for two separate Wasm modules to share memory.

AFAIK, C/++, Rust, and others follow these conventions to some extent, tho I assume most of it comes from having LLVM as a backend.

---

<div class="post-metadata">

**Author:** ![mjg](https://ziggit.dev/user_avatar/ziggit.dev/mjg/32/1897_2.png) [@mjg](https://ziggit.dev/u/mjg)\
**Post date:** [March 6, 2024, 5:46pm UTC](https://ziggit.dev/t/using-zig-with-webassembly/3478/4 "2024-03-06T17:46:57Z")

</div>

Thank you @dimdin and @TUSF

re “2. What’s happening to pages 15 and 16 in section Memory?” I just figured that nothing is necessarily happening to page 16, it’s just that the end of page 15 is written to so I print that as one “block”.

Still surprises me somewhat, the way I was modeling it mentally is that 16 pages (0-15) is the default initial memory, so it writes to page 16 “after the userspace”.

---

<div class="post-metadata">

**Author:** ![zigo](https://ziggit.dev/user_avatar/ziggit.dev/zigo/32/2414_2.png) [@zigo](https://ziggit.dev/u/zigo)\
**Post date:** [September 6, 2024, 5:17pm UTC](https://ziggit.dev/t/using-zig-with-webassembly/3478/5 "2024-09-06T17:17:44Z")

</div>

Great article.

Can anyone answer OP’s second question? I have the same confusion here.
