What is true ? And what is false ?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

What is true ? And what is false ?

Albert G
In the chapter 7 implementation of the comparators (eq,gt,lt), it says that we should be using 0 to indicate false and ffff to indicate true.

In chapter 8, the textbook says that if-goto jumps if the top of the stack is >0. I first implemented the if-goto using "D;JGT" which works fine. But then what about negative stack values ? I then used "D;JNE" and that works fine too.

This looks like a fine point but I think it needs clarifying. As I understand, here is what happens:

- 0 means true
- anything else means false
- we use ffff (-1) as a convention to mean false, but we could just as easily use 1, or in fact any number but 0.

Is this correct ?

Albert
Reply | Threaded
Open this post in threaded view
|

Re: What is true ? And what is false ?

cadet1620
Administrator
I quickly scanned chapter 8 but didn't see where it says ">0".

Most modern languages work as you described. The result of a logical expression is either 0 or -1. When testing logical expressions 0 is false, any other value is true.

There is a subtle bug in the supplied compiler that causes if() and while() commands to interpret their logical expression slightly differently.  To get my game to work correctly with both my compiler and the supplied compiler I ended coding if(x) as if(~(x=0)).

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: What is true ? And what is false ?

Albert G
You are right. The reference to ">0" is actually only in the source of example BasicLoop.vm

push argument 0
if-goto LOOP_START <b>// If counter > 0, goto LOOP_START</b>

That comment confused me. Factually it is not correct, since the if-goto will jump to LOOP_START if counter != 0, so also if counter happens to become negative - which would happen in this example if the input was given as 0.

Both the book and the lecture correctly indicate that if-goto jumps if the value popped is anything else than zero.

Like I said, the test produces an incorrect result if the input is passed as 0: the program goes into an infinite loop (until the test stops it after the set number of ticktocks).

The proper way should be to compare the result of the decremented counter with 0 and use that result to stop when the counter becomes 0 or negative, like this:

push constant 0    
pop local 0        		// initialize sum = 0
push argument 0    
label LOOP_START
	push local 0
	add
	pop local 0	   		// sum = sum + counter
	push argument 0
	push constant 1
	sub
	pop argument 0      // counter--
	push argument 0
	push constant 0
	gt
if-goto LOOP_START 		// If counter > 0, goto LOOP_START
push local 0

The code is slightly longer but now works correctly also when the input is zero.

Still the trick used in this example may deserve a little elaboration, since we are actually using the result of a calculation to feed the if-goto instead of a proper boolean value. I used that trick for the loop that initializes the local variables.
Reply | Threaded
Open this post in threaded view
|

Re: What is true ? And what is false ?

SonOfLilit
In reply to this post by Albert G
Lecture 7 says (twice) that "true and false are 0 and -1, respectively", but lecture 4 says "Hack convention: True is represented by -1, false is represented by 0".

The important piece of text is this: "To get acquainted with the intended behavior of the supplied test program Xxx.vm, run it on the supplied VM Emulator using the supplied XxxVME.tst test script."

The VM Emulator (and the supplied tests) use -1=true, 0=false logic.

Shimon, the lecture 7 slides should probably be fixed in both places. Sorry, I don't have Powerpoint, so I can't submit a pull request.