0

This is a short "calculator" program I wrote while learning assembly. I am running into an issue when there is a negative value being stored in answer. For example, if I input A 10 S 1 Q into Pep/8, I get a 9 as the answer in the end, as expected. If I subtract or negate the answer and I get a negative, the next command after it branches to the invalid response message. It seems that when there is a negative answer being stored in answer, the next command that goes into the accumulator is wrong. If I put in A 1 S 2 the very next command after it will not be inputted correctly. In this case, if I had A 1 S 2 A 3 the accumulator does not have a value of 65 (for 'A') in it after the CHARO input,s executes, but rather a non-sense negative value. In debugging mode I can see that there is a value of -191 in the accumulator instead of 65. My question is, why is this happening? Why is a negative value in answer causing the value being put into the accumulator come out differently?

         BR      Main
input:   .EQUATE 4
answer:  .EQUATE 2 
number:  .EQUATE 0
msg0:    .ASCII  "Menu: \nA: Add \nS: Subtract \nN: Negate \nQ: Quit \nAnswer: \x00"
msg1:    .ASCII  "Input option: \x00"
msg2:    .ASCII  "Input number: \x00"
msg3:    .ASCII  "Answer: \x00"
msg4:    .ASCII  "The calculator has ended. \x00"
msg5:    .ASCII  "Input was invalid. \x00"

Main:    SUBSP   6,i
         LDA     0,i
         STA     answer,s
         STRO    msg0,d
         DECO    answer,s
         CHARO   '\n',i
         CHARO   '\n',i

Menu:    CHARI   input,s
         LDBYTEA input,s
         CPA     'A',i 
         BREQ    Add
         CPA     'S',i
         BREQ    Subtract
         CPA     'N',i 
         BREQ    Negate
         CPA     'Q',i
         BREQ    Quit
         BR      Invalid

Add:     DECI    number,s
         LDA     answer,s
         ADDA    number,s
         STA     answer,s
         STRO    msg1,d
         CHARO   input,s
         CHARO   '\n',i
         STRO    msg2,d
         DECO    number,s
         CHARO   '\n',i
         STRO    msg3,d
         DECO    answer,s
         CHARO   '\n',i
         CHARO   '\n',i
         BR      Menu

Subtract:DECI    number,s
         LDA     answer,s
         SUBA    number,s
         STA     answer,s
         STRO    msg1,d
         CHARO   input,s
         CHARO   '\n',i
         STRO    msg2,d
         DECO    number,s
         CHARO   '\n',i
         STRO    msg3,d
         DECO    answer,s
         CHARO   '\n',i
         CHARO   '\n',i
         BR      Menu

Negate:  LDA     answer,s
         NEGA
         STA     answer,s
         STRO    msg1,d
         CHARO   input,s
         CHARO   '\n',i
         STRO    msg3,d
         DECO    answer,s
         CHARO   '\n',i
         CHARO   '\n',i
         BR      Menu

Quit:    STRO    msg4,d
         BR      Stop

Invalid: STRO    msg5,d
         BR      Stop

Stop:    ADDSP   6,i
         STOP
         .END

EDIT: I figured a way around the issue by simply clearing what was in the accumulator before doing the input. Here's the change I made, and now the program works as expected:

Menu:    CHARI   input,s
         LDA     0,i
         LDBYTEA input,s
         CHARI   input,s
         STBYTEA input,s
         CPA     'A',i 
         BREQ    Add
         CPA     'S',i
         BREQ    Subtract
         CPA     'N',i 
         BREQ    Negate
         CPA     'Q',i
         BREQ    Quit
         BR      Invalid

Although this works, I still don't completely understand why the value in the accumulator was freaking out after the NEGA instruction. If someone could explain why, I would appreciate it.

0 Answers0