余数
rem 指令(remainder 的缩写)用于计算一个整数除以另一个整数后剩余的余数,类似于其他语言中的% 运算符。rem 指令仅适用于整数类型,不适用于浮点类型。
试一试
(module
(import "console" "log" (func $log (param i32)))
(func $main
;; load `10` and `3` onto the stack
i32.const 10
i32.const 3
i32.rem_u ;; calculate the remainder
call $log ;; log the result
)
(start $main)
)
const url = "{%wasm-url%}";
await WebAssembly.instantiateStreaming(fetch(url), { console });
语法
wat
;; load two numbers onto the stack
i32.const 10
i32.const 3
;; calculate the remainder of dividing one number by the other
i32.rem
;; the top item on the stack will now be 1 (10 % 3 = 1)
| 指令 | 二进制操作码 |
|---|---|
i32.rem_s |
0x6f |
i32.rem_u |
0x70 |
i64.rem_s |
0x81 |
i64.rem_u |
0x82 |