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
Some comments on your code:
INPdoes not take an operand: it should be used on its own, not likeINP 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 theSTA(which can be removed) and once as the location for the end result.ADD talladds the value intallto 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 oftall, 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:You have both
tallandantalllocations, but as both uses you have forantallshould be removed, that label and mailbox no longer have a purpose. You can just keeptall.You have a
STAand aLDAwithout mention where to store/retrieve the value. Moreover it seems unnecessary code for doing what you ask about.There are two
OUTinstructions 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: