Start:
INP ; Read number from input
BRZ Output ; If read number is a zero, go to output
ADD Sum ; Add the read number to the current sum value
STA Sum ; Store the current sum value in Sum
BRA Start ; Branch back to start to read a new number
Output:
LDA Sum ; Load the current value of the sum
OUT ; Copy the accumulator value to output
HLT ; Stop the machine
Sum:
0 ; Current value of sum (start value is 0)
-
Start:
- INP: Reads a number from the input and stores it in the accumulator.
-
BRZ Output:
- Checks if the number just read (now in the accumulator) is zero.
- If it is zero, the program branches to the
Output
label; otherwise, it continues to the next instruction.
-
ADD Sum:
- Adds the value stored at the memory location labeled
Sum
to the accumulator. Initially,Sum
is 0, so this effectively starts accumulating the sum of the input numbers.
- Adds the value stored at the memory location labeled
-
STA Sum:
- Stores the current value of the accumulator (which now holds the sum) back into the memory location labeled
Sum
.
- Stores the current value of the accumulator (which now holds the sum) back into the memory location labeled
-
BRA Start:
- Unconditionally branches back to the
Start
label, causing the program to read the next input number.
- Unconditionally branches back to the
-
Output:
- This section is reached only when a zero is input (due to the
BRZ Output
instruction).
- This section is reached only when a zero is input (due to the
-
LDA Sum:
- Loads the current value of
Sum
(the accumulated sum) into the accumulator.
- Loads the current value of
-
OUT:
- Outputs the value in the accumulator (the total sum) to the output.
-
HLT:
- Halts the machine, stopping the program execution.
-
Sum:
- A memory location labeled
Sum
initialized with 0. This is used to store the running total of the input numbers.
- A memory location labeled
- The program continuously reads numbers from the input and adds them to a running total.
- When the user inputs zero, the program outputs the total sum of all previously entered numbers.
- After outputting the sum, the program halts.
- If a user inputs
3
,5
,2
, and then0
, the program will output10
(since3 + 5 + 2 = 10
) and then halt.