First of all, I would like to thank you for taking the time to write this. You took one question about compilers and articulated why Nand2Tetris is such a useful course.
I see your point that it makes sense not to have a language where you can have an integer whose absolute value is not the integer's actual absolute value.
I have a question about the following:
WBahn wrote
Doing this, we could allow our A-type instructions to allow the full range of 16-bit 2's complement values. The logic would look something like this:
@value
If value is a number (not a symbol -- those are dealt with like normal), the verify that it is in the range -32768 to +32767.
Then, if value is non-negative, implement it like normal as 0(15-bit value).
If it is negative, but not -32768, output two instructions: The first loads the absolute value of the number into the A-register and the second implements A=-A.
If it IS -32768, then we can use load -32767 into the A-register (using the two instructions above) followed by A=A-1, for a total of three machine code instructions.
I thought that negative numbers in a jack program (say, -17) are compiled into a push constant 17, neg. Therefore, the assembler would never see a negative number. This might however just be how my compiler handles it (I am either misunderstanding my compiler, or this difference is because I am doing the second version of the book).
To answer the question that you posed to me about how a VM interperter and compiler could each handle this situation, I would probably do the following:
A VM interpreter would interpert "push constant 32678, neg" the same way that it would interpert "push constant 32677, push constant 1, add."
A compiler could compile -32678 into "push constant 32677, push constant 1, add."
WBahn wrote
Here's a pop quiz for you: We could also load +32767 into the A-register followed by A=A+1, for just two instructions. Why does this trick work?
This would work because of the overflow element of the twos complement, where incrementing 32767 by one would yield -32768 (I think).
Again, thank you so much for taking the time to write your response!