MARC4 Programmer’s Guide
Programming in qFORTH
03.01 49
3.9.1 The CCR and the Control
Operations
The Carry flag is set by ALU instructions such
as the +, +C, -or -C whenever an arithmetic
under/overflow occurs. The Carry flag is also
used during shift/rotate instruction such as ROR
and ROL.
The Branch flag is set under CPU control,
depending upon the current ALU instruction,
and is a result of the logical combination of the
carry flag and the TOS = 0 condition.
The Branch flag is responsible for generating
conditional branches. The conditional branch is
performed when the Branch flag has been set by
one of the previous qFORTH operations (e.g.
comparison operations).
The TOG_BF instruction will toggle the state of
the Branch flag in the CCR. If the Branch flag is
set before the TOG_BF instruction, it will be
reset following the execution.
The SET_BCF instruction will set the Branch
and Carry on execution, while the CLR_BCF
operation will reset both flags.
3.10 Arithmetic Operations
The arithmetic operators presented here are
similar to those described in most FORTH
literature. The underlying difference, however,
is that the qFORTH arithmetic operations are
based on the 4-bit CPU architecture of the
MARC4.
3.10.1 Number Systems
When coding in qFORTH, standard numeric
representations are decimal values. For other
representations, it i s necessary to append a single
character for that representation.
Example:
Bh ––> hexadecimal ( base 16 )
bH ––> hexadecimal ( base 16 )
11 ––> decimal ( base 10 )
1011b––> binary ( base 2 )
1011B––> binary ( base 2 )
Single- and Double-Length Operators
Examples have already been presented which
perform operations on the TOS as a 4-bit
(single-length) value or on both the TOS and
TOS-1 values. By combining the TOS and
TOS-1 locations, it is possible to handle the data
as an 8-bit value.
Note: In qFORTH, all operators which start
with a 2 (e.g: 2SWAP or 2@) use
double-length (8-bit) data. Other
operators such as D+ and D= are also
double-length operators.
The qFORTH language also permits
triple-length opera t o r s , w h i c h a r e d e f i n e d w i t h a
3 prefix (e.g: 3DROP). Examples for all
qFORTH dictionary words are included in the
qFORTH Language Reference Dictionary.
3.10.2 Addition and Subtraction
The algebraic expression 4 + 2 is spoken in the
English language as: 4 plus 2, and results in a
value of 6 . I n qFORTH, this expression as 4 2 +.
The 4 is deposited onto the Data Stack, followed
by the 2. The operator gives a command to take
the top two values from the Data Stack and add
them together. The result is then placed back
onto the Data Stack. Both the 4 and the 2 are
dropped from the stack by the operation.
The stack notation for the addition operator is:
+ EXP ( n1 n2 –– n1+n2 )
qFORTH performs the subtraction in a similar
way to the addition operator. The operator is the
common algebraic symbol with the stack
notation:
– EXP ( n1 n2 –– n1–n2 )
Examples:
: TNEGATE ( 12-bit 2’s complement
on the TOS )
0 SWAP –( th tm tl –– th tm –tl )
0 ROT –c ( th tm –tl –– th –tl –tm )
ROT 0 SWAP–c ( th –tl –tm –– tl –tm –th)
SWAP ROT ( –tl –tm –th –– –t )
;