-
Notifications
You must be signed in to change notification settings - Fork 0
/
c-02-function-sum-descending-numbers-of-n-to-1.asm
31 lines (26 loc) · 1.32 KB
/
c-02-function-sum-descending-numbers-of-n-to-1.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// This program takes user input and calculates the sum of descending
// numbers from the input down to 1, and then outputs the result.
//
// int sum = 0;
// const int one = 1;
// int input;
// cin >> input;
// do {
// sum += number;
// number = number - one;
// } while (number > 0);
// cout << sum;
lbResetVars STA varSum // Accumulator has 0, and Sum is reset to 0
INP // Take user input and store it in Accumulator
lbAddNumber STA varNumber // Store the input value in memory location varNumber
ADD varSum // Add the sum to the input value
STA varSum // Store the updated sum back in varSum
lbDecrement LDA varNumber // Load the input value back into the Accumulator
SUB valOne // Subtract 1 from the input value
BRP lbAddNumber // If the result is positive, branch back to lbAddNumber
lbOutput LDA varSum // Load the final sum into the Accumulator
OUT // Output the result
HLT // Halt the program
varNumber DAT 0 // Memory location to store the input value
varSum DAT 0 // Memory location to store the sum
valOne DAT 1 // Constant value 1