Constant -1

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

Constant -1

Francisco José Tornay Mej
I've got a doubt. In the hack platform constants are introduced by the @constant command. This command is 16 bits wide but its first bit is always set to 0, so constants are 15 bits wide. When I wrote the assembler my binary conversion function emitted this kind of constants. However, the book (p. 130) defines the -1 constant as 0xFFFF (16 bits). This is mentioned as the way to represent "true" but I guess it also applies to computing the 2's complements of numbers.

So, what's the right way to go? Are 2's complements computed from 16 or 15 bits constants?
Reply | Threaded
Open this post in threaded view
|

Re: Constant -1

Francisco José Tornay Mej
I just noticed that the book suggests getting the -1 constant by decrementing 0 in a register, so I guess the answer to my own question is that 16 bits should be used for both the true constant and for computing the 2's complement. Am I right?
Reply | Threaded
Open this post in threaded view
|

Re: Constant -1

Warren Toomey
Yes, you need to perform two Hack instructions to get -1 into the A (or D) register.
Reply | Threaded
Open this post in threaded view
|

Re: Constant -1

Francisco José Tornay Mej
Thanks again, Warren. My VM already passes all arithmetic tests. Now for the segments!
Reply | Threaded
Open this post in threaded view
|

Re: Constant -1

cadet1620
Administrator
There are single C-instructions to do this:
    A=-1
    D=-1
    M=-1
You can also set to 0 or 1 using similar instructions.