Jump to content
IGNORED

Learning Forth: Better way to increment variables


JonnyBritish

Recommended Posts

In my code I am tracking a sprites position as a variable called AY. I wanted to add 1 to the variable so used this TurboForth code which is perfectly valid but is 5 Forth words long

 

AY @ 1+ AY !

 

Here is how it works

 

AY @ - retrieve valiable AY and store on the stack

1+ - retrieve top most cell from stack and add 1 to the value and place back on the stack

AY ! - retrieve the top most value on the stack and place back in variable AY

 

However there is a better way which is only 3 Forth words long and that is

 

1 AY +!

 

From the docs at http://www.turboforth.net/language_reference.html

and specifically

 

+! value address -- S Read the cell at address, adds value to it and stores the result at address.

 

You can see it combines all the operations into a 3 word line.

 

same for subtraction

 

-! value address -- S Reads the cell at address. Subtracts value from the cell value, and stores the result at address.

Link to comment
Share on other sites

Hmmm.... Good idea... :thumbsup:

 

You mean like this:

 

ASM: 1+! ( address -- )
*SP R0 MOV, \ get address in r0
R0 ** INC,  \ increment the value at that address
SP DECT,	\ pop address
;ASM

ASM: 1-! ( address -- )
*SP R0 MOV, \ get address in r0
R0 ** DEC,  \ decrement the value at that address
SP DECT,	\ pop address
;ASM

 

:-D

 

You can paste that right in to classic99 - just load block 9 first (9 LOAD) to load the assembler.

 

Let's convert those into CODE words (so that we don't need to load the assembler every time to use them):

 

29 LOAD
ASM>CODE 1+! CLIP
ASM>CODE 1-! CLIP

 

Here I have loaded ASM>CODE from block 29 which converts assembly words that are already in the dictionary to CODE words, and I used the device CLIP for an output device which is a classic99 only device, so ASM>CLIP will send it's output to the windows clipboard.

 

And hey-presto, the following is in the windows clipboard:

 

CODE: 1+!
C014 0590 0644 ;CODE

CODE: 1-!
C014 0610 0644 ;CODE

 

And there you go. You paste/type those into a block, and you have them forever more. Just load them when you need them! (Don't forget to add the word HEX before the CODE: word and DECIMAL at the end - I'll probably make CODE: set HEX automatically in a future version, and restore the base to the previous base at the end). So, you end up with:

 

HEX
CODE: 1+!
C014 0590 0644 ;CODE

CODE: 1-!
C014 0610 0644 ;CODE
DECIMAL

 

Let's test them:

 

$100 $B000 ! ok:0

$B000 1+! ok:0

$B000 @ $. 101 ok:0

$B000 1-! ok:0

$B000 @ $. 100 ok:0

 

 

Or, with a variable:

 

VARIABLE TEST ok:0

100 TEST ! ok:0

TEST 1+! TEST @ . 101 ok:0

TEST 1-! TEST @ . 100 ok:0

 

Et Voilà :-D

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...