added INC instruction and changed INC-X macros for ADD-X

This commit is contained in:
sejo 2021-08-18 12:42:53 -05:00
parent 0c75490306
commit 5e5267791f
2 changed files with 56 additions and 36 deletions

View File

@ -60,6 +60,7 @@ this is a summary of the uxn instructions covered in each day of the tutorial.
## day 2
* DEI: read a value into the stack, from the device address given in the stack ( address -- value )
* INC: increment the value at the top of the stack ( a -- a+1 )
* BRK: break the flow of the program, in order to close subroutines
* MUL: take the top two elements from the stack, multiply them, and push down the result ( a b -- a*b )
* DIV: take the top two elements from the stack, divide them, and push down the result ( a b -- a/b )

View File

@ -485,6 +485,24 @@ here's another question for you: how would you write a macro ADD-X that allows y
%ADD-X { } ( increment -- )
```
## INC instruction
adding 1 to the value at the top of the stack is so common that there's an instruction for achieving it using less space, INC:
```
INC ( a -- a+1 )
```
INC takes the value from the top of the stack, increments it by one, and pushes it back.
in the case of the short mode, INC2 does the same but incrementing a short instead of a byte.
our macro for incrementing the x coordinate could be then written as follows:
```
%INC-X { .Screen/x DEI2 INC2 .Screen/x DEO2 } ( -- )
```
## hello pixels using macros
using these macros we defined above, our code could end up looking as following:
@ -498,7 +516,7 @@ using these macros we defined above, our code could end up looking as following:
( macros )
%DRAW-PIXEL { #11 .Screen/pixel DEO } ( -- )
%INC-X { .Screen/x DEI2 #0001 ADD2 .Screen/x DEO2 } ( -- )
%INC-X { .Screen/x DEI2 INC2 .Screen/x DEO2 } ( -- )
( main program )
|0100
@ -736,8 +754,8 @@ the following code will draw our square sprite with all 16 combinations of color
( macros )
%INIT-X { #0008 .Screen/x DEO2 } ( -- )
%INIT-Y { #0008 .Screen/y DEO2 } ( -- )
%INC-X { .Screen/x DEI2 #0008 ADD2 .Screen/x DEO2 } ( -- )
%INC-Y { .Screen/y DEI2 #0008 ADD2 .Screen/y DEO2 } ( -- )
%8ADD-X { .Screen/x DEI2 #0008 ADD2 .Screen/x DEO2 } ( -- )
%8ADD-Y { .Screen/y DEI2 #0008 ADD2 .Screen/y DEO2 } ( -- )
( main program )
|0100
@ -752,27 +770,27 @@ the following code will draw our square sprite with all 16 combinations of color
( set sprite address )
;square .Screen/addr DEO2
#00 .Screen/sprite DEO INC-X
#01 .Screen/sprite DEO INC-X
#02 .Screen/sprite DEO INC-X
#03 .Screen/sprite DEO INC-Y
#00 .Screen/sprite DEO 8ADD-X
#01 .Screen/sprite DEO 8ADD-X
#02 .Screen/sprite DEO 8ADD-X
#03 .Screen/sprite DEO 8ADD-Y
INIT-X
#04 .Screen/sprite DEO INC-X
#05 .Screen/sprite DEO INC-X
#06 .Screen/sprite DEO INC-X
#07 .Screen/sprite DEO INC-Y
#04 .Screen/sprite DEO 8ADD-X
#05 .Screen/sprite DEO 8ADD-X
#06 .Screen/sprite DEO 8ADD-X
#07 .Screen/sprite DEO 8ADD-Y
INIT-X
#08 .Screen/sprite DEO INC-X
#09 .Screen/sprite DEO INC-X
#0a .Screen/sprite DEO INC-X
#0b .Screen/sprite DEO INC-Y
#08 .Screen/sprite DEO 8ADD-X
#09 .Screen/sprite DEO 8ADD-X
#0a .Screen/sprite DEO 8ADD-X
#0b .Screen/sprite DEO 8ADD-Y
INIT-X
#0c .Screen/sprite DEO INC-X
#0d .Screen/sprite DEO INC-X
#0e .Screen/sprite DEO INC-X
#0c .Screen/sprite DEO 8ADD-X
#0d .Screen/sprite DEO 8ADD-X
#0e .Screen/sprite DEO 8ADD-X
#0f .Screen/sprite DEO
BRK
@ -780,7 +798,7 @@ BRK
@square ff81 8181 8181 81ff
```
note that in this case, the INC-X and INC-Y macros increment each coordinate by 0008: that's the size of the tile.
note that in this case, we have a couple of 8ADD-X and 8ADD-Y macros to increment each coordinate by 0008: that's the size of the tile.
## flipping experiments
@ -963,8 +981,8 @@ the following code will show our sprite in the 16 different combinations of colo
( macros )
%INIT-X { #0008 .Screen/x DEO2 } ( -- )
%INIT-Y { #0008 .Screen/y DEO2 } ( -- )
%INC-X { .Screen/x DEI2 #000c ADD2 .Screen/x DEO2 } ( -- )
%INC-Y { .Screen/y DEI2 #000c ADD2 .Screen/y DEO2 } ( -- )
%cADD-X { .Screen/x DEI2 #000c ADD2 .Screen/x DEO2 } ( -- )
%cADD-Y { .Screen/y DEI2 #000c ADD2 .Screen/y DEO2 } ( -- )
( main program )
|0100
@ -978,27 +996,27 @@ the following code will show our sprite in the 16 different combinations of colo
( set sprite address )
;new-square .Screen/addr DEO2
#80 .Screen/sprite DEO INC-X
#81 .Screen/sprite DEO INC-X
#82 .Screen/sprite DEO INC-X
#83 .Screen/sprite DEO INC-Y
#80 .Screen/sprite DEO cADD-X
#81 .Screen/sprite DEO cADD-X
#82 .Screen/sprite DEO cADD-X
#83 .Screen/sprite DEO cADD-Y
INIT-X
#84 .Screen/sprite DEO INC-X
#85 .Screen/sprite DEO INC-X
#86 .Screen/sprite DEO INC-X
#87 .Screen/sprite DEO INC-Y
#84 .Screen/sprite DEO cADD-X
#85 .Screen/sprite DEO cADD-X
#86 .Screen/sprite DEO cADD-X
#87 .Screen/sprite DEO cADD-Y
INIT-X
#88 .Screen/sprite DEO INC-X
#89 .Screen/sprite DEO INC-X
#8a .Screen/sprite DEO INC-X
#8b .Screen/sprite DEO INC-Y
#88 .Screen/sprite DEO cADD-X
#89 .Screen/sprite DEO cADD-X
#8a .Screen/sprite DEO cADD-X
#8b .Screen/sprite DEO cADD-Y
INIT-X
#8c .Screen/sprite DEO INC-X
#8d .Screen/sprite DEO INC-X
#8e .Screen/sprite DEO INC-X
#8c .Screen/sprite DEO cADD-X
#8d .Screen/sprite DEO cADD-X
#8e .Screen/sprite DEO cADD-X
#8f .Screen/sprite DEO
BRK
@ -1159,6 +1177,7 @@ do the same, but using an image composed of multiple tiles (e.g. 2x2 tiles, 1x2
besides covering the basics of the screen device today, we discussed these new instructions:
* DEI: read a value into the stack, from the device address given in the stack ( address -- value )
* INC: increment the value at the top of the stack ( a -- a+1 )
* BRK: break the flow of the program, in order to close subroutines
* MUL: take the top two elements from the stack, multiply them, and push down the result ( a b -- a*b )
* DIV: take the top two elements from the stack, divide them, and push down the result ( a b -- a/b )