0

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?

trincot
  • 317,000
  • 35
  • 244
  • 286

2 Answers2

0

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/lmc@v0.7/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.

trincot
  • 317,000
  • 35
  • 244
  • 286
-2

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'
Serpent27
  • 332
  • 2
  • 7
  • The LMC does not have an SHL instruction. Moreover, LMC does not use 8-bit numbers, but numbers between 0 and 999, and there is no specification on what the accumulator will have when you overflow it. I cannot see how this answer is useful. – trincot Sep 07 '20 at 19:38
  • Does it have multiply? If you can multiply by 2 you can shift left. And the number of bits doesn't matter, just remove some of the operations. – Serpent27 Sep 07 '20 at 19:42
  • Or, you could even go so far as to add `n+n` if you don't have multiply. There doesn't exist a system where this doesn't work, except maybe in Unlambda or BrainFuck. – Serpent27 Sep 07 '20 at 19:43
  • It doesn't have multiply, and with addition you need to be very careful to avoid overflow of the accumulator, as then the content of the accumulator is undefined. See [Wikipedia](https://en.wikipedia.org/wiki/Little_man_computer) – trincot Sep 07 '20 at 19:44
  • In a range of 0-999 you have room for 9 bits. There's no overflow involved until you hit a 10-bit number. – Serpent27 Sep 07 '20 at 19:46
  • You could manually cut off the extra bits in each operation, too. Just subtract 256 whenever it gets that high. – Serpent27 Sep 07 '20 at 19:47
  • Sure, but you need to get rid of the 9th bit, as it doesn't drop off. I think this answer needs a serious update to take into account the limitations of LMC – trincot Sep 07 '20 at 19:47
  • Anyway, OP seems to think this answer works since they marked it as the accepted answer, so it was obviously not useless. – Serpent27 Sep 07 '20 at 19:49