Reputation: 1
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?
Upvotes: -3
Views: 575
Reputation: 43290
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.
Upvotes: 1