Why logical commands return 0xFFFF as true?

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

Why logical commands return 0xFFFF as true?

pete_lin
When i hava not read the chapter-8 ,i just think it is not a good idea let 0xFFFF as true.
now,maybe i have some point to say that.
this is  my if-go command translate result write in python :

def write_if(label):
    """
    if-goto
    if D > 0 ,go
    :param label:
    :return:
    """
    result = '@SP\nAM=M-1\nD=M\n'
    result += "@" + stack[-1] + '$' + label + "\n"
    result += 'D;JGT\n'

    return result

 i pop some num in 'D' ,and 'D;JGT',that work will.

but what if :

push constant 1
push constant 0
gt
if-go

this time ,the num in D is 0xffff , which is not great then 0. this is not right

sorry for my poor english , hopeing anyone can tell me why. thank you a lot !  
Reply | Threaded
Open this post in threaded view
|

Re: Why logical commands return 0xFFFF as true?

cadet1620
Administrator
pete_lin wrote
When i hava not read the chapter-8 ,i just think it is not a good idea let 0xFFFF as true.
now,maybe i have some point to say that.
The specification for if-goto from chapter 8 is:

        if-goto label

        This command effects a conditional goto operation. The stack’s
        topmost value is popped; if the value is not zero, execution
        continues from the location marked by the label; otherwise,
        execution continues from the next command in the program.
        The jump destination must be located in the same function.

Therefore, FALSE must be 0 and all non-zero values must be treated as TRUE.

Because the and, or and not VM commands are bit-wise arithmetic, the binary value for TRUE must have all its bits set. This is the value 0xFFFF.

Consider what happens in this code if TRUE had been assigned the value 1.
    push constant 123
    push constant 123
    eq                   returns 0x0001
    not                  returns 0xFFFE
    if-goto not_equal    jumps to not_equal !!!

If TRUE is 0xFFFF then the result of the not will be 0x0000, and the if-goto will function as desired.


this is  my if-go command translate result write in python :

def write_if(label):
    """
    if-goto
    if D > 0 ,go
    :param label:
    :return:
    """
 ...
You need to change this to test for not equal instead of greater than.

--Mark