1

I'm trying to write a Little Man Computer program that outputs how many positive numbers I have and how many negative numbers I have, but it considers the negative numbers as positive too.

This is what I have so far:

LOOP IN 
     STO NUM
     BRZ END
     BRP POS
POS  LDA ZERO 
     ADD UN 
     STO ZERO 
END  LDA ZERO
     OUT 
     BR LOOP
     HLT
NUM  DAT 000
UN   DAT 001
ZERO DAT 000
trincot
  • 317,000
  • 35
  • 244
  • 286
  • What is the output of the above program, basically you will have to write a parser that will parse your commands and figure out the output – zenwraight Nov 19 '19 at 20:19
  • 1
    @zenwraight https://en.wikipedia.org/wiki/Little_man_computer – JohanC Nov 19 '19 at 22:24
  • Is this an acceptable simulator? http://www.peterhigginson.co.uk/lmc/ – JohanC Nov 19 '19 at 22:28
  • As I understand it, there is no way with a Little Man Computer to input negative numbers, only the values in the range 0...999 are defined. – trincot Nov 20 '19 at 05:54

1 Answers1

1

Providing an LMC with negative input is not supported by the specification. LMC memory cells can only hold values between 0 and 999 (source: Wikipedia). The only notion of "negative" is represented by the LMC's negative flag. And that flag is only set by the execution of ADD or SUB, so not by INP/IN, LDA or STA/STO (spellings differ). See also these notes of Ian! D. Allen where he discusses this confusing concept.

This practically means that your BRP instruction will always branch to the provided label.

You may find simulators that actually allow negative input, but that would be an extension to the original specifications.

Now, your code also has an issue with this BRP POS instruction, because it just jumps to the next line (labeled POS), so it actually would not make any difference whether BRP would branch or not: it is a no-operation.

A similar exercise

As negative values do not really exist in LMC, let's do the exercise a bit differently: consider the input as presented in 10's-complement, so that 500..999 are to be considered negative values (i.e. -500...-1). Or, that signed input values will actually get stored in 10's-complement when you do INP. Then count the number of inputs that are less than 500 (positive) and those that are not (as these are negative when interpreted as 10's-complement).

LOOP    INP
        BRZ FINISH
        SUB COMPARE
        BRP ELSE
        LDA LESS
        ADD ONE
        STA LESS
        BRA LOOP
   ELSE LDA NOTLESS
        ADD ONE
        STA NOTLESS
        BRA LOOP
 FINISH LDA LESS
        OUT
        LDA NOTLESS
        OUT
        HLT
   LESS DAT
NOTLESS DAT
COMPARE DAT 500
    ONE DAT 1
    
<script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.6/lmc.js"></script>

Be sure to supply a final 0 value for the program to output the result.

trincot
  • 317,000
  • 35
  • 244
  • 286