LMC Negative FlagThe text and lecture notes are 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!) These lights are set only after an arithmetic operation (i.e. ADD or SUB) and stay set until the next arithmetic operation. Moving numbers into or out of the Calculator does not affect the lights. Negative Numbers vs. Negative OperationsThe text is inconsistent in its 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 the author talks about negative results. For the purposes of this course, we define that the "Negative" light comes on 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. 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 handle negative numbers.) In short - there are no negative numbers in the LMC, only an operation (SUBTRACT) that might turn on the Negative light if a larger number is subtracted from a smaller. That is the only time when the Negative light will be on. Testing non-Negative NumbersSince 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. Testing "X < 0" is always FALSE. Since numbers are always positive, testing "X <= 0" is the same as testing "X == 0" (since X will never be less than zero). Since numbers are always positive, testing "X > 0" is the same as testing "X != 0" (since X will never be less than zero). The following 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! |
Web Author: Ian! D. Allen idallen@idallen.ca Updated: 2003-09-23 11:44 Support free and non-commercial Internet. This site works best in Any Browser, a campaign for non-specific WWW. This work is licensed under a Creative Commons License. |