Daryl
Daryl

Reputation: 37

Forth and processor flags

Why doesn't Forth use processor flags for conditional execution?

Instead the result of a comparison is placed on the parameter stack. Is it because the inner interpreter loop may alter flags when going to the next instruction? Or is it simply to abstract conditional logic?

E.g. on x86 the flags register holds results of a comparison as most processors if not all will have a flags register.

Upvotes: 3

Views: 276

Answers (2)

Brian Tiffin
Brian Tiffin

Reputation: 4116

It depends on the Forth, and the level of optimization.

: tt 0 if ." true" else ." false" then ;

In SwiftForth (x86_64 GNU/Linux):

see tt
808376F   4 # EBP SUB                   83ED04
8083772   EBX 0 [EBP] MOV               895D00
8083775   0 # EBX MOV                   BB00000000
808377A   EBX EBX OR                    09DB
808377C   0 [EBP] EBX MOV               8B5D00
808377F   4 [EBP] EBP LEA               8D6D04
8083782   808379D JZ                    0F8415000000
8083788   804D06F ( (S") ) CALL         E8E298FCFF
808378D   "true"
8083793   804C5BF ( TYPE ) CALL         E8278EFCFF
8083798   80837AE JMP                   E911000000
808379D   804D06F ( (S") ) CALL         E8CD98FCFF
80837A2   "false"
80837A9   804C5BF ( TYPE ) CALL         E8118EFCFF
80837AE   RET                           C3 ok

In Gforth:

see tt
: tt
  0
  IF     .\" true"
  ELSE   .\" false"
  THEN ; ok

Upvotes: 3

George Kourtis
George Kourtis

Reputation: 2592

As Forth is a stack-based language, in order to define the operations inside the language, you must define the result to alter something that is inside the language. The flags register isn't in the language. Obviously in case of an optimizing compiler, whatever approach that gives the same final result is equally acceptable.

Upvotes: 3

Related Questions