From 5e0f2898d001ae6bf54d51ed773735281eef67c7 Mon Sep 17 00:00:00 2001 From: Albert Date: Wed, 3 Jul 2019 12:37:34 +0800 Subject: [PATCH 1/9] Improve examples of opcodes that take params --- index.markdown | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/index.markdown b/index.markdown index 8936878..9b5eb53 100644 --- a/index.markdown +++ b/index.markdown @@ -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 Resulting stack: -{% include stack.html stack="1 2 3 4 2" %} +{% include stack.html stack="1 2 3 4 3" %} ### `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" %} ### Exercises From a618bb68c7237eeefe6d4ceed6628f041de96c38 Mon Sep 17 00:00:00 2001 From: Albert Date: Wed, 3 Jul 2019 14:57:48 +0800 Subject: [PATCH 2/9] Add reference to tuck --- index.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.markdown b/index.markdown index 9b5eb53..b3955ee 100644 --- a/index.markdown +++ b/index.markdown @@ -333,7 +333,7 @@ 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 From 3d0155d8bb2685386f52dd4b3ee9d459af0eb447 Mon Sep 17 00:00:00 2001 From: Albert Date: Wed, 3 Jul 2019 15:03:47 +0800 Subject: [PATCH 3/9] Fix comma --- index.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.markdown b/index.markdown index b3955ee..413afa2 100644 --- a/index.markdown +++ b/index.markdown @@ -339,7 +339,7 @@ Second, `swap` top elements. 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. From 6654dade63e0e529f9c3c22f43e59c7b02144d09 Mon Sep 17 00:00:00 2001 From: Albert Date: Wed, 3 Jul 2019 15:08:14 +0800 Subject: [PATCH 4/9] Fix pointer dereference So that it's consistent with C --- index.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.markdown b/index.markdown index 413afa2..d532c0f 100644 --- a/index.markdown +++ b/index.markdown @@ -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`. 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? From 638651cd31a93d925b18417b7d18b5a59a0208ee Mon Sep 17 00:00:00 2001 From: Albert Date: Wed, 3 Jul 2019 16:26:30 +0800 Subject: [PATCH 5/9] Use push instructions in a consistent way --- index.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.markdown b/index.markdown index d532c0f..84cb2f5 100644 --- a/index.markdown +++ b/index.markdown @@ -569,7 +569,7 @@ _______________|________________________________|____________ ``` 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). {% include editor.html altstack=true neovm=true size="small"%} From a3b544fd6f232f44e41408c1cca3677bd68c4303 Mon Sep 17 00:00:00 2001 From: Albert Date: Wed, 3 Jul 2019 16:27:34 +0800 Subject: [PATCH 6/9] Add reference to pack opcode --- index.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.markdown b/index.markdown index 84cb2f5..ea22f7c 100644 --- a/index.markdown +++ b/index.markdown @@ -573,7 +573,7 @@ To inspect what happened, use the following commands to get array from altstack {% 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 From 6ba8d996bdfc5deb9e9b5661c54312c5519e6145 Mon Sep 17 00:00:00 2001 From: Albert Date: Wed, 3 Jul 2019 16:38:08 +0800 Subject: [PATCH 7/9] Fix typo --- index.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.markdown b/index.markdown index ea22f7c..f3618b8 100644 --- a/index.markdown +++ b/index.markdown @@ -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. From 695bc2ed92f2463aa1c4a7840d9ed7df37137d5c Mon Sep 17 00:00:00 2001 From: Albert Date: Wed, 3 Jul 2019 16:38:23 +0800 Subject: [PATCH 8/9] Ignore generic code in prompt Makes it so that generic code is skipped when moving up and down in the interactive command prompt --- javascripts/editor.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/javascripts/editor.js b/javascripts/editor.js index 090faaa..9f814d3 100644 --- a/javascripts/editor.js +++ b/javascripts/editor.js @@ -86,7 +86,7 @@ function Editor(selectorOrElement) { } function selectPreviousLine() { - if (selectedLine === null || selectedLine === 0) { + if (selectedLine === null || selectedLine === 233) { 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++; } From 43af2d997ce0cc12aa09869cf935cfdc5170a848 Mon Sep 17 00:00:00 2001 From: Albert Date: Thu, 4 Jul 2019 10:08:09 +0800 Subject: [PATCH 9/9] Apply @igormcoelho suggestions --- index.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.markdown b/index.markdown index f3618b8..8a7506c 100644 --- a/index.markdown +++ b/index.markdown @@ -292,7 +292,7 @@ In next sections, we will see stack opcodes that receive parameters from stack. ### `pick` (opcode `0x79`) -`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: +`pick` consumes top-stack value `n` and copies the `n`-th element to the top of the stack. Example: push1 push2 push3 push4 push1 pick @@ -302,7 +302,7 @@ Resulting stack: ### `roll` (opcode `0x7a`) -`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: +`roll` consumes top-stack value `n` and moves the `n`-th element to the top of the stack. Example: push1 push2 push3 push4 push1 roll