code_seg segment
assume cs: code_seg, ds: data_seg
pop dx
mov ah, 9 ; Put the service code for printing string on monitor screen
int 21h ; Invoke the Software Interrupt 21H
mov ah, 0
push ax ; Push read character onto stack
next_digit:
call read_char
pop ax ; Get the read character in AX from ToS
cmp al, 0Dh ; Check if the input character is CR indicating end
of the number
je done
sub al, 30h ; Convert ASCII to binary
mov cl, al ; Save the digit value in CX as a word
mov ch, 0 ;
mov ax, bx ; Get back the saved partial MS digits value to AX
mul ten ; Shift left the partial MS digits value by 1 digits
add ax, cx ; Add the last digit to the shifted MS digits value
mov bx, ax ; Save the new partial MS digits value in BX
jmp next_digit ; loop back read the next digit
pop ax
mov bx, 0 ; Initialize bx, dx
mov dx, 0
repeat1:
mov cx, 0 ; Extract the digits in the number
mov dx, 0 ; for printing
div ten ; Extract the next digit
push dx ; Use stack to have the extracted digits in the reverse order
inc counter ; Increment count of the number of digits extracted.
cmp ax, 0 ; Check if all the digits extracted
jne repeat1 ; Continue to extract the digits
print_digit:
pop dx ; Pop out the digits from the stack
add dl, 30h ; Convert the digit value to ASCII
print_number endp
start:
mov ax, data_seg ; Initialize DS
mov ds, ax
mov al, n
mov counter, al ; Initialize counter
mov bp, offset number ; Initialize the pointer number array
push offset msg1 ; Print the Input Prompt Message on
Screen
call print_string
rd_next:
call read_number ; Call the number read procedure.
pop dx ; Take the number into dx from ToS.
mov [bp], dx ; Place the input number in the number array.
add bp, 2 ; Increment BP to point to the next array location
push offset msg2 ; Print Prompt Message for the next number
call print_string
jmp rd_next ; Loop back to read the next number.
compute_sum:
add_next:
add ax, [bp] ; add next number
add bp, 2 ; increment BP to point to the next number
dec counter ; decrement counter
jne add_next ; Loop back to add the next number
code_seg ends
end start