Anda di halaman 1dari 3

data_seg segment

number dw 10 dup(?) ; The array of numbers


sum dw 0 ; The variable sum
n db 10 ; Constant defining the no. of numbers
ten dw 10 ; Defining constant ten for extracting the
digits
counter db ? ; A counter variable
string db 10 dup(?)
msg1 db 10, 13, �Enter a number: �, �$�
msg2 db 10, 13, �Enter the next number: �,�$�
msg3 db 10, 13, 10, 13, �The sum of the numbers is: �, �$�
data_seg ends

code_seg segment
assume cs: code_seg, ds: data_seg

print_string proc ; Procedure to print a string on monitor screen


pop si ; Save Top of Stack(ToS) content (Return Address) in SI register

pop dx
mov ah, 9 ; Put the service code for printing string on monitor screen
int 21h ; Invoke the Software Interrupt 21H

push si ; Push the saved Return Address onto the stack


ret ; Return to Main Program
print_string endp

read_char proc ; Procedure to read a character from key board


pop di ; Save top of stack content (Return Address) in DI
register

mov ah, 1 ; Put service code for reading a character from


keyboard
int 21h ; Invoke the Software Interrupt

mov ah, 0
push ax ; Push read character onto stack

push di ; Push the saved Return Address onto the stack

ret ; Return to the main program


read_char endp

read_number proc ; Procedure to read a number

pop si ; Pop the ToS content (the Return Address) into SI

mov bx,0 ; Initialize bx, dx


mov dx,0

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

done: push bx ; Push the read number onto the stack

push si ; Push the Return Address back on the ToS

ret ; Return to the main program


read_number endp

print_number proc ; Procedure to print a multi-digit number

pop si ; Pop the ToS content (the Return Address) into SI

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

mov ah, 2 ; Place the service code in AH


int 21h ; Invoke the ISR to print the digit character on
screen

dec counter ; Decrement the digit counter


jnz print_digit ; Branch back if some more digits remaining to be
printed

push si ; Push the Return Address back on the ToS

ret ; Return to the main program

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

dec counter ; decrement counter.


je compute_sum ; Reading of the numbers complete, proceed to sum.

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:

mov bp, offset number ; Reset BP to point to the start of the


number array
mov al, n
mov counter, al ; Reset counter value to n
mov ax, 0 ; Initialize AX to hold the computed 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

mov bx, offset sum


mov [bx], ax ; Store the computed sum value in variable sum.

push offset msg3 ; Print Output Message


call print_string

push sum ; Push computed sum onto ToS


call print_number ; Call print_number procedure to
print the sum

mov ah, 4ch ; Enter the service code to AH


register
mov al, 0 ; Set return status of the program
int 21h ; Invoke the ISR to return the control to
the Operating System

code_seg ends
end start

Anda mungkin juga menyukai