The program is a basic little calculator that adds, subtracts, or negates whatever number is in answer. The add, subtract, and quit options work fine, but whenever I use N I am unable to input anything after it. I am assuming that something is wrong in elseif2 but after playing around with it, I can't wrap my head around it. Did I setup the entire problem incorrectly or is it fixable as is?
1 Answers
The problem is caused by the fact that pep8 registers and most operations are 16 bit but LDBYTE
only modifies the low 8 bits. This means, if the top 8 bits of A
get set to something other than zero, none of your letter comparisons will match after you load the next input into the low bits. Negating a positive value will trigger this because it sets the most significant bit to 1 as per the usual two's complement arithmetic.
The solution is to ensure that the top bits are cleared, for example by zeroing A
before the LDBYTE
or masking it off using AND
afterwards.
You could have seen this issue yourself if you used the trace feature of the pep8 simulator:
0072 LDBYTEA 0003,d D10003 FF51 00CE FBCF 1 0 0 0 5100
The FF51
there is the accumulator, holding the ascii code of the Q
I entered in the low 8 bits, but FF
in the top 8 bits from the previous negation.
Also note that CHARI
also returns the newline at the end of the user input in case it hasn't been consumed, so I would recommend you add a check for that too.

- 56,577
- 4
- 81
- 125