Skip to content
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

Open
wants to merge 9 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions index.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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` consumes top-stack value `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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change the example?

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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

Really? 🤔 I have to check...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot
After applying pick:
Screenshot


Resulting stack:

{% include stack.html stack="1 2 3 4 2" %}
{% include stack.html stack="1 2 3 4 3" %}
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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` consumes top-stack value `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" %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change the example? Is it better?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to remain consistent after changing the other example.



### Exercises
Expand All @@ -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.

Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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).
Copy link
Member

Choose a reason for hiding this comment

The 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

Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions javascripts/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function Editor(selectorOrElement) {
}

function selectPreviousLine() {
if (selectedLine === null || selectedLine === 0) {
if (selectedLine === null || selectedLine === 233) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this fix?

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it's better to create a clearconsole function, so we clear past commands.

selectedLine = lineBuffer.length - 1;
} else {
selectedLine--;
Expand All @@ -97,7 +97,7 @@ function Editor(selectorOrElement) {

function selectNextLine() {
if (selectedLine === null || selectedLine === lineBuffer.length - 1) {
selectedLine = 0;
selectedLine = 233;
} else {
selectedLine++;
}
Expand Down