home .. forth .. colorforth mail list archive ..

[ColorForth] More newbie questions



On Thu, 27 Sep 2001, Leslie Snively wrote:

> I've finally been able to start learning/experimenting with colorForth 
> again and it's taken me only a few minutes to run into another beginner 
> question:  How does "if" work?  Yes, I've read the definition that it 
> jumps to then if the zero flag is set.  But how does one set the zero 
> flag?

On the pentium there is a register called eflags which contains the zero
flag. The zero flag is set when the result of some previous operation is
zero. Usually in machine language, you do an operation which modifies the
registers in some way (therefore setting eflags), and then do a jump. The
'if' macro simply compiles to 'jz offset', with offset filled in with the
'then' macro. 

> I would assume by the number zero being the top item on the 
> stack, but that doesn't seem to do it.  For example, using the following 
> word:
> 
> [R]iftest [G] if 1 ; then 0 ;
> 
> I would expect "4 iftest" to leave a 1 on the stack and "0 iftest" to 
> leave a 0 on the stack.  However, it actually returns "4 0" and "0 0".

If you are doing that from the interpreter the result is not predictable,
because you don't know the last operation. However I did some testing of
my own:

: t0 0 iftest ;

and

: t4 4 iftest ;

Do produce what you describe, acording to the pentium manual all flags are
affected by 'call' and 'jmp' instructions. The best thing to do is perform
the operation right before the 'if' macro. For example:

: iftest 4 if 1 ; then 0 ;

will produce '4 1'
  
> It's clear that I'm missing something simple.  It seems that "if" does 
> not consume the value on the stack, which would make sense if it's 
> actually using a zero flag somewhere as the switch. 

That is right.

> All guidance will 
> be appreciated.
> 
> Also, what does the word "?" do?  The documentation isn't clear to me 
> for this word either.
> 

'?' tests (and operation) the top element of the stack. For example you
could write your 'iftest' as:

: iftest -1 ? if 1 ; then 0 ; 

-1 tests all the bits. Also see Kris Johnson's word list which has a lot
of words not documented on Chuck's site:
http://sleepless-night.com/cgi-bin/twiki/view/Main/ColorForthWord . It
doesn't have a lot of the stuff used in the macros though. I'm working on
understanding some of the more complicated macros.

Mark



------------------------

To Unsubscribe from this list, send mail to Mdaemon@xxxxxxxxxxxxxxxxxx with:
unsubscribe ColorForth
as the first and only line within the message body
Problems   -   List-Admin@xxxxxxxxxxxxxxxxxx
Main ColorForth site   -   http://www.colorforth.com