-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Several fixes #3
base: gh-pages
Are you sure you want to change the base?
Changes from 8 commits
5e0f289
a618bb6
3d0155d
6654dad
638651c
a3b544f
6ba8d99
695bc2e
43af2d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -292,23 +292,23 @@ In next sections, we will see stack opcodes that receive parameters from stack. | |
|
||
### `pick` (opcode `0x79`) | ||
|
||
`pick` reads `n` and copies `n`-th element to top stack. Example: | ||
`pick` reads and drops the number at the top of the stack, which we will call `n`, and copies the `n`-th element to the top of the stack. Example: | ||
|
||
push1 push2 push3 push4 push2 pick | ||
push1 push2 push3 push4 push1 pick | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why change the example? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the previous example the stack just remains the same after applying pick, this behaviour confused me a bit when following the tutorial so I decided to change it to an example that modified the stack so other people could appreciate the behaviour of pick better. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still, it may be easier for some people to follow the previous example so if this change is controversial i can revert it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Really? 🤔 I have to check... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
Resulting stack: | ||
|
||
{% include stack.html stack="1 2 3 4 2" %} | ||
{% include stack.html stack="1 2 3 4 3" %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why change the example? This affected the result. Is it a better one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consequence from the change in the example. |
||
|
||
### `roll` (opcode `0x7a`) | ||
|
||
`roll` reads `n` and moves `n`-th element to top stack. Example: | ||
`roll` reads and drops the number at the top of the stack, which we will call `n`, and moves the `n`-th element to the top of the stack. Example: | ||
|
||
push1 push2 push3 push4 push2 roll | ||
push1 push2 push3 push4 push1 roll | ||
|
||
Resulting stack: | ||
|
||
{% include stack.html stack="1 3 4 2" %} | ||
{% include stack.html stack="1 2 4 3" %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why change the example? Is it better? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed it to remain consistent after changing the other example. |
||
|
||
|
||
### Exercises | ||
|
@@ -333,13 +333,13 @@ Second, `swap` top elements. | |
|
||
{% include stack.html stack="1 3 4 5 6 2 7" %} | ||
|
||
**Exercise:** Is there any other way to do it, even if it costs more operations? (_challenge: try to use `tuck` instead of `swap`_) | ||
**Exercise:** Is there any other way to do it, even if it costs more operations? (_challenge: try to use [`tuck`](https://docs.neo.org/developerguide/en/articles/neo_vm.html#tuck) instead of `swap`_) | ||
|
||
## Double-Stack model | ||
|
||
NeoVM includes a _double-stack_ design, so we need operations also for both the Evaluation Stack (which we've seen already) and the Alternative Stack. | ||
|
||
On FORTH language, this stack is called _return stack_, and it is only used for loops and other temporary storages, since it also stores the execution return pointer. This limits the _return stack_ usage, since loops can interfere in stack operations, that's why FORTH also includes a _global storage map_. Although, NeoVM doesn't have a global storage map, it can successfully execute operations only using two stacks and by using arrays as temporary storage (will be explored in later sections). | ||
On FORTH language, this stack is called _return stack_, and it is only used for loops and other temporary storages, since it also stores the execution return pointer. This limits the _return stack_ usage, since loops can interfere in stack operations, that's why FORTH also includes a _global storage map_. Although NeoVM doesn't have a global storage map, it can successfully execute operations only using two stacks and by using arrays as temporary storage (will be explored in later sections). | ||
|
||
Next sections will show important double-stack operations. | ||
|
||
|
@@ -424,7 +424,7 @@ How do we store information on a array? | |
|
||
### `setitem` (opcode `0xc4`) | ||
|
||
Opcode `setitem` consumes an Array pointer `p`, an index `i` and a value `v` from stack, and performs the following attribution: `(*p)[i] = v`. | ||
Opcode `setitem` consumes an Array pointer `p`, an index `i` and a value `v` from stack, and performs the following attribution: `p[i] = v`. | ||
igormcoelho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
So, after that, value `v` is stored at index `i` of `p`, and nothing is put back on stack (note that array is consumed in this operation, remember to `dup` it). | ||
|
||
How do we get information from an Array position? | ||
|
@@ -569,11 +569,11 @@ _______________|________________________________|____________ | |
``` | ||
|
||
To inspect what happened, use the following commands to get array from altstack and pick inside it: | ||
`dupfromaltstack 0 pickitem` (gets first element), `drop`, `dupfromaltstack 1 pickitem` (gets second element). | ||
`dupfromaltstack push0 pickitem` (gets first element), `drop`, `dupfromaltstack push1 pickitem` (gets second element). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good |
||
|
||
{% include editor.html altstack=true neovm=true size="small"%} | ||
|
||
**Exercise:** replace the code above (`PUSH2 NEWARRAY TOALTSTACK DUPFROMALTSTACK PUSH0 PUSH2 ROLL SETITEM DUPFROMALTSTACK PUSH1 PUSH2 ROLL SETITEM`) by a much simpler operation. | ||
**Exercise:** replace the code above (`PUSH2 NEWARRAY TOALTSTACK DUPFROMALTSTACK PUSH0 PUSH2 ROLL SETITEM DUPFROMALTSTACK PUSH1 PUSH2 ROLL SETITEM`) by a much simpler operation. **Tip:** check [the pack opcode](https://docs.neo.org/developerguide/en/articles/neo_vm.html#pack). | ||
|
||
### Managing Execution Parameters | ||
|
||
|
@@ -730,7 +730,7 @@ One important topic is not covered, but an explanation is given below. | |
|
||
### Conditionals, Loops and Function Calls | ||
|
||
Conditionals and loops are implemented on NeoVM via the use o jumps. | ||
Conditionals and loops are implemented on NeoVM via the use of jumps. | ||
Basically, a jump changes the current instruction counter and moves it to another | ||
position in the _NVM script_, affecting which operations will be read after that. | ||
Since this is not directly related to the stack execution model, it will only be covered in future tutorials. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,7 +86,7 @@ function Editor(selectorOrElement) { | |
} | ||
|
||
function selectPreviousLine() { | ||
if (selectedLine === null || selectedLine === 0) { | ||
if (selectedLine === null || selectedLine === 233) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this fix? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you go into one of the interactive prompts in the webpage and press the upper or lower arrow in your keyboard a generic forth script is displayed. This prevents that script from being displayed while still preserving the arrow functionality. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps it's better to create a |
||
selectedLine = lineBuffer.length - 1; | ||
} else { | ||
selectedLine--; | ||
|
@@ -97,7 +97,7 @@ function Editor(selectorOrElement) { | |
|
||
function selectNextLine() { | ||
if (selectedLine === null || selectedLine === lineBuffer.length - 1) { | ||
selectedLine = 0; | ||
selectedLine = 233; | ||
} else { | ||
selectedLine++; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea.. I think that changing to
pick consumes n
could be even simpler. Phrase became too long.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue that I came across when following the tutorial is that
n
is not defined anywhere so you have to end up guessing what it is by looking at the examples. I was trying to fix that with these changes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps:
consumes top-stack value n
...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great idea, I'll change it!