Reputation: 13
I'm struggling a bit on this question. Essentially, I have to loop through a code with inputs, until the input is 0. Doing that by BRZ. And then, I have to output how many numbers that have been typed in to input. If I have typed in 1, 4, 6, 2, then the input should be 4, since there's four numbers there.
This is the code I'm currently using
start INP antall
BRZ jump
ADD tall
STA tall
BRA start
jump LDA tall
OUT
antall STA
LDA
OUT
HLT
tall DAT 0
antall DAT
antall = total tall = numbers if anyone's wondering what the labels mean
Upvotes: 0
Views: 539
Reputation: 350310
Some comments on your code:
INP
does not take an operand: it should be used on its own, not like INP antall
. The user's input is loaded into the accumulator, so it doesn't need any parameter.
Labels should be unique. You have defined two lines with the label antall
, once for the STA
(which can be removed) and once as the location for the end result.
ADD tall
adds the value in tall
to the accumulator. But at that moment the accumulator holds the last input value, which is not something you want to include in the addition. Instead, you want to load the current value of tall
, and then add 1 to it. So you need a mailbox with the value 1 in it. You could define it near the end of your code block:
one DAT 1
You have both tall
and antall
locations, but as both uses you have for antall
should be removed, that label and mailbox no longer have a purpose. You can just keep tall
.
You have a STA
and a LDA
without mention where to store/retrieve the value. Moreover it seems unnecessary code for doing what you ask about.
There are two OUT
instructions in your code, but reading the question you only need to output one thing: the number of inputs.
Here is the corrected code -- you can run it here:
#input: 1 4 6 2 0
start INP
BRZ jump
LDA tall
ADD one
STA tall
BRA start
jump LDA tall
OUT
HLT
one DAT 1
tall DAT 0
<script src="https://cdn.jsdelivr.net/gh/trincot/[email protected]/lmc.js"></script>
Upvotes: 0