1

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.

trincot
  • 317,000
  • 35
  • 244
  • 286
Tony
  • 13
  • 4
  • 1
    What is the role of *n* in this algorithm? Is that the number of inputs that will follow? Is each next input a single binary digit? What is the purpose of `NUMBER` in your code, since you never use it after it has been input? – trincot Apr 07 '21 at 19:24
  • What is the purpose of `EIGHT`, `TWO`, `POWERONE`, and `RESA` in your code...they are never used? And what does `|A` mean? Is it a comment? – trincot Apr 07 '21 at 19:32
  • the role of n is subsequent inputs are requested, like n is 8, the subsequent input were 1,0,0,1,0,0,0,0 then the output would be 9 and the first of these is least significant and nth input is most significant. The purpose of eight is I do know how to do multiplication in LMC, so I though just output the result directly. A is a comment – Tony Apr 08 '21 at 00:12
  • You can mulitply by two (all you need for this problem) by adding a number to itself. – Chris Dodd Apr 08 '21 at 01:50
  • I tried to do a mulitiple loop but I not sure how to a correct one. – Tony Apr 08 '21 at 02:15
  • So I posted an answer... did you check it out? – trincot Apr 08 '21 at 13:16

1 Answers1

0

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>
trincot
  • 317,000
  • 35
  • 244
  • 286