APPAPA
APPAPA

Reputation: 25

Pep8 - Base 2 to Base 10 converter

I am developing a so called simple pep8 program, that converts a base 2 number into base 10.

Below are the requirements:

  1. Ask for user input in base 2
  2. Convert the base 2 value into decimal and output that.
  3. Finally loop and ask user if they want to enter another value. If so ask question again, else display message 'done' (or something to that effect)

So far I am trying to read a character and store this as a string.

Could anyone help!

Thank you.

             BR      main        
letter:  .BLOCK  1           ;global variable #1c
;
main:    CHARI   letter,d    ;cin >> letter
         LDA     0x0000,i    
while:   LDBYTEA letter,d    ;while (letter != '*')
         CPA     '*',i       
         BREQ    endWh       
         CHARO   letter,d    ;   cout << letter
         CHARI   letter,d    ;   cin >> letter
         BR      while       
endWh:   STOP                
         .END

Upvotes: 0

Views: 748

Answers (1)

CrazyMLC
CrazyMLC

Reputation: 13

Storing it as a string would work, but that over complicates the problem.

You might have found this out during the month between question and answer, but you can build the number up as you read each character, using the arithmetic shift left operation.

        BR  main            ;#include <iostream>
letter: .BYTE   0           ;char letter = 0;
number: .WORD   0           ;int number = 0;
                            ;int main() {
main:   CHARI   letter,d    ;   std::cin >> letter;
        LDA     0,i
        LDBYTEA letter,d
        CPA     '0',i       ;   if (letter == '0') {
        BRNE    notzer
        LDA     number,d
        ASLA                ;       number *= 2;
        STA     number,d
        BR      main        ;       main();
notzer: CPA     '1',i       ;   } else if (letter == '1') {
        BRNE    end
        LDA     number,d
        ASLA                ;       number *= 2;
        ADDA    1,i         ;       number++;       
        STA     number,d
        BR      main        ;       main();
                            ;   } else {
end:    DECO    number,d    ;       std::cout << number;
        STOP                ;       return 0;
        .END                ;   }
                            ;}

This isn't necessarily the simplest or best way to solve the problem though, I was just trying to make it easy to understand.

For example, you could keep the number in the index register until you were ready to print it, saving you lines/time from loading and storing it with the accumulator. (you could just use ASLX and ADDX 1,i)

Upvotes: 1

Related Questions