I am looking at this Little man computer problem:
Write a Little Monkey Computer program that can convert an n-bit binary number into a number using the base-10 number system. Display the natural number as output before halting the program.
The first input determines the value for n. It is assumed this value will be equal to four, or greater.
For example, if the first input is eight (8) then eight subsequent inputs are requested. If the subsequent inputs number were 1, 0, 0, 1, 0, 0, 0, 0 then the output would be 9.
n input values are provided by the user, one for each bit: The first of these is the least-significant bit.
The nth input is the most-significant bit.
My attempt:
IN
STO NUMBER
IN
STO A
IN
STO B
IN
STO C
IN
STO D
LOOPB: LDA FOUR
BRZ ENDB
SUB ONE
STO FOUR
LDA RESB
ADD B
STO RESB
BRP LOOP
ENDB:LDA RESB
OUT
HLT
ONE: DAT 1
EIGHT: DAT 8
FOUR: DAT 4
TWO: DAT 2
POWERONE: DAT 1
RESA: DAT 000
RESB: DAT 000
RESULT: DAT 000
NUMBER: DAT 0
A: DAT 0
B: DAT 0
C: DAT 0
D: DAT 0
I do not how to solve this question, how to make 00001001 would convert to 9 on LMC? I not sure how to do multiplication on LMC.
The code you have does not use the first input. Instead it uses FOUR
. This is the first thing to change, as you don't want to loop exactly four times, but as many times as your first input is.
Secondly, you don't need to store each next input in a separate mailbox. Instead you can immediately process the 0/1 input as it decides whether you want to add something to the result or not. So you only need to update the result depending on the input's 0/1 value. You don't need to store that 0/1 value itself -- so no need of A
, B
, C
...etc. Instead put the IN
instruction inside the loop.
Then remains what exactly you should add to the result when an input turns out to be 1. As the input is in reversed order (the least significant bits come first), you should keep track of a power of 2, which you double in each iteration of the loop. That will be the number to add to the result whenever you encounter an input of 1. Because that is how the binary system works. For instance if the input of digits is 1 1 0 1, then the calculation is:
input digit | power of 2 | to be added | running sum |
---|---|---|---|
1 | 1 | yes | 1 |
1 | 2 | yes | 3 |
0 | 4 | no | 3 |
1 | 8 | yes | 11 |
So here is the script for that. You can run it here: first run the code snippet (which starts the LMC simulator) and then use the controls in the right panel:
#input:8 1 0 1 1 0 0 0 0
LDA ZERO # Initialise, so program still runs
STO RESULT # correctly when it is reset.
LDA ONE
STO POWER
IN # Get number of binary digits
LOOP BRZ OUTPUT # All digits have been read
STO COUNT
IN # Get a binary digit
BRZ NEXT # Nothing to add when it's zero
LDA RESULT # Add a power of 2 to the result
ADD POWER
STO RESULT
NEXT LDA POWER # Next power of 2
ADD POWER
STO POWER
LDA COUNT # Prepare for next iteration
SUB ONE
BR LOOP
OUTPUT LDA RESULT
OUT
ZERO HLT
ONE DAT 1
POWER DAT 1
COUNT DAT
RESULT DAT
<script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.816/lmc.js"></script>