How to take number up to 64 and output in a 000 000 binary format

67 Views Asked by At

The goal is to accept a number up to 64 and output it in binary in a 000 000 format, so encoded in two decimal values. I know that LMC wont allow an output number like 010, so a format like 11 100 is also acceptable.

Here is my code so far:

        INP
        STO INPUT
        SUB SUB64
        BRP END
        LDA INPUT
        SUB SUB32
        BRP SET_32
RET_32  LDA INPUT
        SUB SUB16
        BRP SET_16
RET_16  LDA INPUT
        SUB SUB8
        BRP SET_8
RET_8   LDA INPUT
        SUB SUB4
        BRP SET_4
RET_4   LDA INPUT
        SUB SUB2
        BRP SET_2
RET_2   LDA INPUT
        SUB SUB1
        BRP SET_1
RET_1   OUT OUTPUT_2
        OUT OUTPUT_1
END     HLT
SET_1   STO INPUT
        LDA OUTPUT_1
        ADD ADD1
        STO OUTPUT_1
        BRA RET_1
SET_2   STO INPUT
        LDA OUTPUT_1
        BRA RET_2
SET_4   STO INPUT
        LDA OUTPUT_1
        ADD ADD100
        STO OUTPUT_1
        BRA RET_4
SET_8   STO INPUT
        LDA OUTPUT_2
        ADD ADD1
        STO OUTPUT_2
        BRA RET_8
SET_16  STO INPUT
        LDA OUTPUT_2
        ADD ADD10
        STO OUTPUT_2
        BRA RET_16
SET_32  STO INPUT
        LDA OUTPUT_2
        ADD ADD100
        STO OUTPUT_2
        BRA RET_32
OUTPUT_1 DAT 000
OUTPUT_2 DAT 000
INPUT   DAT 000
SUB64   DAT 64
SUB32   DAT 32
SUB16   DAT 16
SUB8    DAT 8
SUB4    DAT 4
SUB2    DAT 2
SUB1    DAT 1
ADD1    DAT 1
ADD10   DAT 10
ADD100  DAT 100

Running this with input 63 will output 101 101, so it's outputting it in the right format, but it is not working consistently: for input 62, this outputs two -1's

What should I do to make this work?

2

There are 2 best solutions below

0
trincot On BEST ANSWER

There are two issues in your code (the updated version at the end of your question):

  1. OUT does not take an argument. OUT will output whatever is in the accumulator. So change:

    OUT OUTPUT_2
    OUT OUTPUT_1
    

    To:

    LDA OUTPUT_2
    OUT
    LDA OUTPUT_1
    OUT
    
  2. You forgot to add 10 in the case of SET_2. The following two instructions need to be added there:

    ADD ADD10
    STO OUTPUT_1
    

Here is the corrected code:

#input:63
        INP
        STO INPUT
        SUB SUB64
        BRP END
        LDA INPUT
        SUB SUB32
        BRP SET_32
RET_32  LDA INPUT
        SUB SUB16
        BRP SET_16
RET_16  LDA INPUT
        SUB SUB8
        BRP SET_8
RET_8   LDA INPUT
        SUB SUB4
        BRP SET_4
RET_4   LDA INPUT
        SUB SUB2
        BRP SET_2
RET_2   LDA INPUT
        SUB SUB1
        BRP SET_1
RET_1   LDA OUTPUT_2
        OUT
        LDA OUTPUT_1
        OUT
END     HLT
SET_1   STO INPUT
        LDA OUTPUT_1
        ADD ADD1
        STO OUTPUT_1
        BRA RET_1
SET_2   STO INPUT
        LDA OUTPUT_1
        ADD ADD10
        STO OUTPUT_1
        BRA RET_2
SET_4   STO INPUT
        LDA OUTPUT_1
        ADD ADD100
        STO OUTPUT_1
        BRA RET_4
SET_8   STO INPUT
        LDA OUTPUT_2
        ADD ADD1
        STO OUTPUT_2
        BRA RET_8
SET_16  STO INPUT
        LDA OUTPUT_2
        ADD ADD10
        STO OUTPUT_2
        BRA RET_16
SET_32  STO INPUT
        LDA OUTPUT_2
        ADD ADD100
        STO OUTPUT_2
        BRA RET_32
OUTPUT_1 DAT 000
OUTPUT_2 DAT 000
INPUT   DAT 000
SUB64   DAT 64
SUB32   DAT 32
SUB16   DAT 16
SUB8    DAT 8
SUB4    DAT 4
SUB2    DAT 2
SUB1    DAT 1
ADD1    DAT 1
ADD10   DAT 10
ADD100  DAT 100


<script src="https://cdn.jsdelivr.net/gh/trincot/[email protected]/lmc.js"></script>

According to your specification, this outputs two decimal numbers, where the digits should be interpreted as binary. As you already noted, this can be confusing. For instance, with input 9, the output is 1 1 and not 001 001.

If you want to have every binary digit visualised, consider outputting 6 values instead of 2, and let each output be either 0 or 1. In that case the output for 9 would be 0 0 1 0 0 1.

See this answer to see how you can achieve that.

8
Serpent27 On

You can print the most significant bit, then multiply by 2 (left-shift 1) until you reach the bit length of the number you're trying to print. For example:

n = 01100100 # 0
SHL(n, 1)
n = 11001000 # 1
SHL(n, 1)
n = 10010000 # 1
SHL(n, 1)
n = 00100000 # 0
SHL(n, 1)
n = 01000000 # 0
SHL(n, 1)
n = 10000000 # 1
SHL(n, 1)
n = 00000000 # 0
SHL(n, 1)
n = 00000000 # 0 (number is 8-bits so we don't stop until we print 8 digits.)
----------------------
Result: '01100100'