Proposal: 2C! and 2C@
Considered
This page is dedicated to discussing this specific proposal
ContributeContributions
sxjh9935 [299] 2C! and 2C@Proposal2023-06-07 01:09:02
Author:
sxjh9935
Change Log:
2023-06-07 - First draft of proposal.
Problem:
When using Forth for system programming, it is not usual for 16-bit values to be written or read. There are Forth words that handle 64-bit values, 32-bit values, 8-bit values but not 16-bit values.
Solution:
Introduce two new words - 2C! and 2C@.
The signatures for the two words are as follows-
2C! ( x1 x2 c-addr -- )
2C@ ( c-addr -- x1 x2 )
In both instances, x1 is the value at the higher address (c-addr+1) and x2 is the value at the lower address (c-addr).
Proposal:
These words should form part of the CORE dictionary.
The words 2C! and 2C@ should be, respectively, inserted at 6.1.0360 and 6.0365.
The following word definitions should be inserted.
2C!
( x1 x2 c-addr -- )
Store char pair at c-addr with x2 at c-addr and x1 at the next c-addr.
2C@
( c-addr -- x1 x2 )
Fetch char pair at c-addr. x2 is stored at c-addr and x1 at the next c-addr.
Reference implementation:
2C@:
DUP CHAR+ C@ SWAP C@
2C!:
SWAP OVER C! CHAR+ C!
These implementations were written without reference to other publicly available implementations.
After these implementations were written, the author checked whether such words are described in publicly available code. By sheer coincidence, the author found implementations nearly identical to 2C@. The difference being that 1+ was used rather than CHAR+. The author also found implementations of 2C! but such implementations made use of the return stack and such use of the return stack is unnecessary.