Im currently writing a program in wasm text format and I was wondering if there was a way to write an 8 bit integer to memory. Does anybody know if this even possible?
I have looked everywhere but I can't find an answer.
Currently WebAssembly does not have i8
type for values on the stack. The closest type you can use is i32
.
If you are working with i32
or i64
(for short inn
), then as the comments point out, inn.load8_sx
and inn.store8
can be used to load/store 8bit values from/to a WebAssembly.Memory, where sx = u | s
, and from the specs:
annotation sx distinguishes whether the operands are to be interpreted as unsigned or signed integers.
; address
i32.const 0
; value
i32.const 258
; write the lowest 8 bits, 00000010b, from the value
; into the memory address 0, ignoring the higher bits
i32.store8
; address
i32.const 0
; load 8 bits from address 0 that are 00000010b
; zero extend the 8 bits to 32 (0..00000010b)
; and push i32 into the stack
i32.load8_u
The inn.load8_s
works similarly, but by sign extending the loaded 8 bit integer, which means that if the highest bit of the loaded bits is 1
(negative signed integer), then 10000010b
becomes 1..10000010b
.