Read a number n and print 1 + 2 + ... + n. Writing the printing as a procedure (PROC ... RET) keeps the program tidy.
Input
One line with a whole number n, 1 ≤ n ≤ 99.
Output
The sum 1 + 2 + ... + n in decimal.
Examples
Example 1 · Input
10
Expected output
55
Example 2 · Input
1
Expected output
1
Hints
Hint 1
Read one or two digits until Enter: n = n × 10 + digit.
Hint 2
Add n, n − 1, ..., 1 with LOOP (CX = n), keeping the sum in AX; it fits in a word.
Hint 3
Print AX in decimal with a procedure: divide by 10, push the remainders, pop and print them.
Solution
Show one correct solution (try it yourself first)
; Sum from 1 to n
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV BX, 0 ; n
READ:
MOV AH, 1
INT 21H
CMP AL, 0DH
JE ADDUP
SUB AL, '0'
MOV AH, 0
PUSH AX ; the digit
MOV AX, BX
MOV CX, 10
MUL CX ; AX = n x 10
POP CX
ADD AX, CX
MOV BX, AX
JMP READ
ADDUP:
MOV CX, BX
MOV AX, 0
SUM:
ADD AX, CX
LOOP SUM
CALL PRINT_NUM
MOV AH, 4CH
INT 21H
MAIN ENDP
PRINT_NUM PROC ; prints AX in decimal
PUSH AX
PUSH BX
PUSH CX
PUSH DX
MOV BX, 10
MOV CX, 0
DIVIDE:
MOV DX, 0
DIV BX ; AX = AX / 10, DX = the last digit
PUSH DX
INC CX
CMP AX, 0
JNE DIVIDE
SHOW:
POP DX
ADD DL, '0'
MOV AH, 2
INT 21H
LOOP SHOW
POP DX
POP CX
POP BX
POP AX
RET
PRINT_NUM ENDP
END MAIN