-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d1997a
commit d7452bd
Showing
8 changed files
with
142 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule Examples.HexFormatter do | ||
use Orb | ||
|
||
Memory.pages(1) | ||
|
||
wasm_mode(U32) | ||
|
||
defw u32_to_hex_lower(value: I32, write_ptr: I32.U8.UnsafePointer), | ||
i: I32, | ||
digit: I32 do | ||
i = 8 | ||
|
||
# for i <- 8..1//-1 do | ||
loop Digits do | ||
i = i - 1 | ||
|
||
digit = I32.rem_u(value, 16) | ||
value = value / 16 | ||
|
||
if digit > 9 do | ||
write_ptr[at!: i] = ?a + digit - 10 | ||
else | ||
write_ptr[at!: i] = ?0 + digit | ||
end | ||
|
||
Digits.continue(if: i > 0) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
defmodule Examples.HexFormatterTest do | ||
use ExUnit.Case, async: true | ||
|
||
Code.require_file("hex_formatter.exs", __DIR__) | ||
alias Examples.HexFormatter | ||
|
||
alias OrbWasmtime.Instance | ||
|
||
test "u32_to_hex_lower" do | ||
# IO.puts(HexFormatter.to_wat()) | ||
|
||
inst = Instance.run(HexFormatter) | ||
u32_to_hex_lower = Instance.capture(inst, :u32_to_hex_lower, 2) | ||
read = &Instance.read_memory(inst, &1, 8) | ||
|
||
u32_to_hex_lower.(0x00000000, 0x100) | ||
assert read.(0x100) == "00000000" | ||
|
||
u32_to_hex_lower.(255, 0x100) | ||
assert read.(0x100) == "000000ff" | ||
|
||
u32_to_hex_lower.(0x00ABCDEF, 0x100) | ||
assert read.(0x100) == "00abcdef" | ||
|
||
u32_to_hex_lower.(0x44ABCDEF, 0x100) | ||
assert read.(0x100) == "44abcdef" | ||
|
||
u32_to_hex_lower.(0xFFFF_FFFF, 0x100) | ||
assert read.(0x100) == "ffffffff" | ||
|
||
u32_to_hex_lower.(1, 0x100) | ||
assert read.(0x100) == "00000001" | ||
|
||
# Does NOT write outside its bounds. | ||
assert Instance.read_memory(inst, 0x100 - 1, 1) == <<0>> | ||
assert Instance.read_memory(inst, 0x100 + 9, 1) == <<0>> | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters