-3

I need to create a simple LMC program for the following task:

Input a series of numbers and output:

  1. the largest among them, and
  2. how many numbers were entered.

The number 000 will be used to mark the end of the series of input numbers (000 is not considered as part of the series but it should be output if it is the only number entered).
Your program must work for ANY number of input values.

I think I need to use branching (BRZ, BRP, BR) but I'm not sure which type I'm supposed to use. Also, I've tried using an online LMC simulator but I'm not really sure where to start as LMC is very confusing.

How can I solve this?

trincot
  • 317,000
  • 35
  • 244
  • 286
Asher
  • 1
  • 2
  • What have you tried so far? – Dario Petrillo Nov 07 '21 at 01:37
  • I've done some research and I think I need to use branching (BRZ, BRP, BR) but I'm not sure which type I'm supposed to use. Also, I've tried using an online LMC simulator but I'm not really sure where to start as lmc is very confusing. – Asher Nov 07 '21 at 01:41
  • LMC is so minimal that it's annoying to get anything done with it. But at least this problem is something LMC can do in a fairly straightforward way, without self-modifying code, since you don't need to store the input numbers in an array or anything, just count them and check against a current-max. – Peter Cordes Nov 07 '21 at 01:44
  • 1
    Welcome to Stack Overflow. Please read the [About](http://stackoverflow.com/tour) page soon and also visit the links describing [How to Ask a Question](http://stackoverflow.com/questions/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) (MCVE). Providing the necessary details, including your MCVE, compiler warnings and associated errors, if any, will allow everyone here to help you with your question. – David C. Rankin Nov 07 '21 at 02:41
  • Well, I *almost* was able to post an answer, then @DavidC.Rankin closed it. Still, he's right. OP should at least post an attempt at having solved it. Can you write a loop that inputs "any" amount of input? Can you count the inputs? Write as much as you can, take a shot at the parts you are unsure of, then ask for help with the rest. – David Wohlferd Nov 07 '21 at 02:57
  • 1
    @DavidWohlferd - sorry about that, had no indication that was coming. If you still want to post, I'll vote to reopen -- but no guarantees that will happen. Let me know. If it won't reopen, you can always post a solution on patebin or similar and add a link in the comment. – David C. Rankin Nov 07 '21 at 03:06

1 Answers1

1

Answer given to question as updated by OP:

The question has been edited to be about branching. Branching is the core of learning assembly language. We can talk about that.

Fragment of data segment:

counter   DAT 0
max       DAT 0
current   DAT 0

Code to check if current is end of list:

         LDA current
         BRZ end
         ;... more code here

Simple. We check if current is currently zero and end the loop if it is. BRZ branches if the current value is zero.

Checking if current is the new maximum

         LDA current
         SUB max
         BRP positive
         BRA next
positive LDA current
         STA max
         BRA next     ;next is almost certainly back at top
end      HLT          ;the zero check ends up here
                      ;you probably need to insert OUT instructions

And here's a different branch. BRP branches if the value in current is not zero and the negative flag is not set. New concept flags. LMC has one flag, the negative flag. It is set if the previous operation underflowed/overflowed. If such an event happened we want set the value to the new maximum. Otherwise, we move on to the next value.

Answer given to question as original:

Apologies, I may have missread. I have interpreted it as given an array in memory terminated by zero, find the size and the largest. This problem is hard, and I understand why OP hasn't been able to post an attempt. This has to do with indirection. The only way to do indirection in LMC is self-modifying code.

First off, we have only two digits of indirection in the LOAD/STORE instructions, the total memory size is only 100 bytes, and we're going to need to make our program much smaller than that.

Starting with the data segment (which must go at the end):

one      DAT 1
ldazero  LDA 0
largest  DAT 0
count    DAT 0
first    DAT ?

Iterating over the list for the count.

ctr       LDA first
          BRZ  endcount
          LDA ctr
          ADD  one
          STO  ctr
          JMP  ctr
endcount  LDA  ctr
          SUB  ldazero
          STO  count
          HLT

Given the method ofindirection, it should be possible to write down how to do max. In order to avoid two counters, copy the value being compared into a working slot.

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • Really? The sample in the lower left hand corner of [this](https://www.101computing.net/LMC/) shows using an `@`. Besides, you don't actually need to store the values, just count. – David Wohlferd Nov 07 '21 at 02:34
  • @DavidWohlferd: Wikipedia shows no possible way of encoding an indirect: https://en.wikipedia.org/wiki/Little_man_computer – Joshua Nov 07 '21 at 02:35
  • The link I gave is an online LMC simulator. It has a (working) example of using indirection with LMC. Maybe it uses LMC v2.0? I don't know enough about LMC to say. – David Wohlferd Nov 07 '21 at 03:01
  • @DavidWohlferd: lol they cheated. "3@12" is not a number. – Joshua Nov 07 '21 at 04:16
  • LMC doesn't have indirect memory addressing; to do that you need self-modifying code. (Which is apparently a standard LMC technique for looping over arrays for example, which makes it a weird teaching language but helpful for driving home the von Neumann architecture concept.) But the question talks about taking them as input (i.e. from the user), so no need for an array. `STA @address` isn't a normal LMC instruction, and yeah, as Joshua says, the opcode it assembles to (3@12) is not a number from 0..999. – Peter Cordes Nov 07 '21 at 22:14