Skip to content

Instantly share code, notes, and snippets.

@qdwang
Created March 7, 2017 13:10
Show Gist options
  • Save qdwang/7d181696aba97ecd6cfce93472b2868f to your computer and use it in GitHub Desktop.
Save qdwang/7d181696aba97ecd6cfce93472b2868f to your computer and use it in GitHub Desktop.
vm debug
fib(n) = if (x < 2) {
return x
} else {
return fib(n-1) + fib(n-2)
}
// get local variable of index = 0 and push to stack
21
0
// uint8: 2 and push to stack
3
2
// pop 2 elems from stack and if 1st elem < 2nd elem then push 1 to stack else 0
47
2
// pop 1 elem from stack and detect whether it is 0 or 1. If it is 0 then jump to `false branch` else pc += 5 to `true branch`
43
12 // uint32: pc offset to `false branch`
0
0
0
// ============= true branch ===============
// get local variable of index = 0 and push to stack
21
0
// uint32: 22 and push to stack
7
22
0
0
0
// pop 1 elem from stack and jump to `pc += 22`
20
// ============= false branch ===============
// get variable of index = 0 and push to stack
32
0
// get local variable of index = 0 and push to stack
21
0
// uint8: 1 and push to stack
3
1
// pop 2 elems and push the minus of them to stack
37
2
// pop 2 elem [func address, param] from stack and then call the function(it's fib) with the poped param and then push the result back to stack. It's like `fib(n - 1)
34
2
// just like above like `fib(n - 2)`
32
0
21
0
3
2
37
2
34
2
// pop 2 elems and add them and then push back result to stack
36
2
// cleanup and jump to where next to previous applyed
18
19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment