You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+84-4
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ The recommended approach is to just install the [NPM module](https://www.npmjs.c
19
19
npm i ax-x86
20
20
```
21
21
22
-
To actually emulate programs, you have to make sure the WASM binary has been downloaded using the default `init` function:
22
+
Before using any functions, you have to make sure the WASM binary has been downloaded using the default `init` function:
23
23
24
24
```js
25
25
import { defaultasinit, version } from'ax-x86';
@@ -29,7 +29,16 @@ await init();
29
29
console.log("ax version:", version());
30
30
```
31
31
32
-
Here is a simple example that executes a few instructions and prints the result:
32
+
Two warnings/pitfalls when using this emulator:
33
+
* Make sure that all numbers are passed as `bigint`, which can be done using an `n` suffix. `0x1000n` is a `bigint` literal (which is what we want), `0x1000` is a `number` (which will *not* work)
34
+
* When using frontend frameworks, it is recommended to await the `init` function before your components are mounted, e.g. in a `setup` function. This will make sure the WASM binary is downloaded before the component is rendered. You can look at the [this Vue component](examples/web/src/components/Initial.vue) for an example.
35
+
36
+
37
+
### Simple emulation of instruction
38
+
The following is a simple example that executes a few instructions and prints the calculated result.
39
+
40
+
<details>
41
+
<summary>Open for more info on the example</summary>
33
42
34
43
```js
35
44
import { defaultasinit, Axecutor, Mnemonic, Register, version } from'ax-x86';
Warning: Make sure that all numbers are passed as `bigint`, hence the `n` suffix!
99
+
The emulator will just stop when reaching the end of the code.
100
+
101
+
</details>
102
+
103
+
### Emulate ELF binaries
104
+
The emulator also has some convenience functions for handling Linux/ELF binaries. The [test site](https://ax.010.one) uses these convenience functions to emulate programs.
105
+
106
+
<details>
107
+
<summary>Open for more info on how to emulate ELF files</summary>
108
+
One thing to note is that binaries usually exit via the `exit` syscall, which is not implemented by default (same as any other syscall). You must handle it yourself, like in the following example:
109
+
110
+
```js
111
+
let ax =Axecutor.from_binary(/* elf file content as Uint8Array */);
112
+
113
+
// Set up the stack according to the System V ABI.
114
+
// This sets up memory locations for command-line arguments and environment variables
115
+
// and writes the stack pointer to RSP
116
+
ax.init_stack_program_start(
117
+
8n*1024n, // Stack size
118
+
["/bin/my_binary", "arg1", "arg2"],
119
+
[ /* environment variables */ ]
120
+
);
121
+
122
+
123
+
letsyscallHandler=asyncfunction (ax:Axecutor) {
124
+
let syscall_num =ax.reg_read_64(Register.RAX);
125
+
let rdi =ax.reg_read_64(Register.RDI);
126
+
let rsi =ax.reg_read_64(Register.RSI);
127
+
let rdx =ax.reg_read_64(Register.RDX);
128
+
129
+
console.log(`Syscall ${syscall_num} with args ${rdi}, ${rsi}, ${rdx}`);
130
+
131
+
switch (syscall_num) {
132
+
case1n: {
133
+
// WRITE syscall MUST write to stdout or stderr (stdin supported for compatibility)
If your binaries need more syscalls, you can look at the [example site implementation](examples/web/src/components/Initial.vue) or get more details [here](https://syscalls.w3challs.com/?arch=x86_64).
91
171
92
-
When using frontend frameworks, it is recommended to await the `init` function before your components are mounted, e.g. in a `setup` function. This will make sure the WASM binary is downloaded before the component is rendered. You can look at the [this Vue component](examples/web/src/components/Initial.vue) for an example.
172
+
</details>
93
173
94
174
## Contributing
95
175
If you want to contribute to this project, that's great! A good way to involved is using the emulator and finding things that could be improved :)
0 commit comments