I need to create a simple LMC program for the following task:
Input a series of numbers and output:
- the largest among them, and
- 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?
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:
Code to check if current is end of list:
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
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):
Iterating over the list for the count.
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.