3

I use a word ->list that creates and returns an address. I don't always need to associate this address with a variable name, but I'd like that to be possible. For the moment, if I want to do so, this look like this:

.s <0>  ok
1 2 3 3 ->list .s <1> 140318466418488  ok
VARIABLE mylist  ok    
mylist ! .s <0>  ok  
mylist @ . 140318466418488  ok
mylist @ @ . 1  ok   
mylist @ 1 cells + @ . 2  ok
...

This provides an indirect addressing to access elements in ->list adress. Is it possible to create a variable which address is an already known address?

2
  • There is a bug in your code. The word create does not reserve any units in the data field, you have do it by yourself, e.g.: CREATE mylist 1 cells allot or CREATE mylist 0 , Commented Jul 16, 2023 at 14:06
  • Thx. I just changed for VARIABLE word. Commented Jul 16, 2023 at 16:52

1 Answer 1

5

To associate a number (an address as well as any other number) with a name you can use constant. In your case:

1 2 3 3 ->list constant mylist
mylist @ . \ "1"

Is it possible to create a variable which address is an already known address?

In Forth, a variable is actually a constant that returns an address.

For example, the word variable can be defined as follows:

: variable ( "name" -- )
  align here 0 , constant
;

So if you have the address of an already reserved cell, you can just use constant. For example:

align here 123 ,  constant myvar
myvar @ . \ "123"
456 myvar !
myvar @ . \ "456"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.