作者在 2015-11-29 17:33:49 发布以下内容
TITLE Integer Summation Program
;This program inputs multiple integers from the user
;stores them in an array,caculates the sum of the
;array,and displays the sum.
INCLUDE Irvine32.inc
IntegerCount = 3
.data
prompt1 byte "enter a signed integer:",0
prompt2 byte "the sum of the integers is:",0
array dword IntegerCount dup(?)
.code
main PROC
call clrscr
mov esi,offset array
mov ecx,IntegerCount
call PromptForIntegers
call ArraySum
call DisplaySum
call waitmsg
exit
main ENDP
;-----------------------------------------------
PromptForIntegers PROC
;Prompt the user for an array of integers,and fills
;the array with the user's input
;Receives:ESI points to the array,ECX=array size
;Returns:nothing
;------------------------------------------------
pushad
mov edx,offset prompt1
L1:
call writestring
call readint
call crlf
mov [esi],eax
add esi,4
loop L1
popad
ret
PromptForIntegers ENDP
;------------------------------------------------------
ArraySum PROC
;Calculates the sum of an array of 32-bit integers.
;Receices:ESI points to the array
; ECX=number of elements in the array
;Returns: EAX=sum of the array elements
;------------------------------------------------------
push esi
push ecx
mov eax,0
L1:
add eax,[esi]
add esi,4
loop L1
pop ecx
pop esi
ret
ArraySum ENDP
;------------------------------------------------------
DisplaySum PROC
;Displays the sum on the screen
;Receives:EAX=the sum
;Returns:nothing
;------------------------------------------------------
push edx
mov edx,offset prompt2
call writestring
call writeint
call crlf
pop edx
ret
DisplaySum ENDP
END main