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?
There are two issues in your code (the updated version at the end of your question):
OUTdoes not take an argument.OUTwill output whatever is in the accumulator. So change:To:
You forgot to add 10 in the case of SET_2. The following two instructions need to be added there:
Here is the corrected code:
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.