==========================
LMC Negative Flag Trickery
==========================
The lecture notes may seem unclear on how the various Skip instructions
know whether the result in the Calculator is negative, zero, or
positive.
In fact, the result in the calculator is always positive; there is
trickery involved in the setting of the Negative status flag (light).
For the purposes of this course, we define three status flags (or
lights): Negative, Zero, and Positive. (Positive includes zero.)
The Skip instructions work based on the settings of the lights, not on
the value of the number currently in the Calculator. This has to be so,
since the original author of the LMC claims that the LMC doesn't have
negative numbers, so the number in the Calulator is never negative!
The LMC doesn't use two's complement or have a "Sign" flag as most
real computers do.
These three LMC lights are set only after an arithmetic operation
(i.e. only after ADD or SUB) and stay set until the next arithmetic
operation. Moving numbers into or out of the Calculator does not set
or affect the lights.
Negative Numbers vs. Negative Operations
The notes may seem inconsistent in their use of "negative" numbers
in the Little Man Computer. On the one hand, the author states that
negative numbers are not possible; on the other hand, subtraction of
a larger number from a smaller permits a "Skip If Negative" condition
to occur and we may talk about negative results.
For the purposes of this course, we define that the "Negative" light
comes on (and the Positive light goes off) only after we subtract a
larger number from a smaller. The fact that the light is on does not
mean that the number in the Calculator is "negative"; it simply means
that the last SUBTRACT operation subtracted a larger number from a
smaller, and doing that set the Negative light. The operation of
subtracting a larger number from a smaller is the only thing that
turns on the Negative light and turns off the Positive light.
In our LMC, the Negative light is always cleared after an ADD, even if
the ADD causes an overflow. (In a real computer, adding two large two's
complement numbers may cause overflow and the result to be negative.
That doesn't happen with the LMC, since the LMC doesn't use two's
complement or handle negative numbers.)
In short - there are no negative numbers in the LMC, only an operation
(SUBTRACT) that might turn on the Negative light (and turn off the
Positive light) iff a larger number is subtracted from a smaller.
That is the only time when the Negative light will be turned on
and the Positive light will be turned off.
Testing non-Negative Numbers
Since the number in the calculator is always positive, programmers used
to working with signed numbers (e.g. C language, COBOL) may end up
writing code that doesn't work. Here are some observations and problem
examples.
Since numbers are always positive, testing "X >= 0" is always TRUE in
the LMC. Testing "X < 0" is always FALSE in the LMC.
Since numbers are always positive, testing "X <= 0" is the same as
testing "X == 0" (since X will never be less than zero) in the LMC.
Since numbers are always positive, testing "X > 0" is the same as
testing "X != 0" (since X will never be less than zero) in the LMC.
The following C++ Language loops work correctly with signed integers.
They don't work at all for unsigned (never negative) integers:
for ( x=50; x >= 0; x-- ) { // INFINITE LOOP using unsigned integers
cout << x;
}
for ( x=3; x > 0; x -= 2 ) { // INFINITE LOOP using unsigned integers
cout << x;
}
Be careful with unsigned numbers! Your compiler may warn you about
always-true or always-false tests if you turn on warnings.
--
| Ian! D. Allen - idallen@idallen.ca - Ottawa, Ontario, Canada
| Home Page: http://idallen.com/ Contact Improv: http://contactimprov.ca/
| College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/
| Defend digital freedom: http://eff.org/ and have fun: http://fools.ca/