TITLE Demonstrating Procedures (ArraySum.asm) .686 .MODEL flat, stdcall .STACK 4096 INCLUDE Irvine32.inc SumDigits PROTO sumaddr:PTR DWORD .data sum DWORD ? .code main PROC INVOKE SumDigits, ADDR sum mov eax, sum call WriteDec call Crlf exit main ENDP ; This procedure reads a string of characters and stores it on the stack ; Accumulates the sum of the string digits, non-digit characters are ignored ; Receives: address of sum variable (reference parameter) ; Returns: sum of the string digits (by reference) SumDigits PROC USES eax ebx ecx edx, sumaddr:PTR DWORD LOCAL s[20]:BYTE ; allocate 20 bytes for local string variable lea edx, prompt ; write prompt string call WriteString lea edx, s ; EDX = local string address mov ecx, 20 ; maximum chars to read call ReadString ; string is stored on the stack mov ecx, eax ; save number of chars in ECX mov eax, 0 ; EAX = used to accumulate sum of digits mov ebx, 0 ; BL part of EBX stores one digit L1: mov bl, [edx] ; move one character into BL sub bl, '0' ; convert character to a number cmp bl, 9 ; check if BL is a digit ja L2 ; skip next instruction if BL is a non-digit add eax, ebx ; accumulate sum in eax L2: inc edx ; point to next character loop L1 mov ebx, sumaddr ; EBX = parameter = address of sum mov [ebx], eax ; store sum indirectly ret ; return prompt BYTE "Enter a string of (max 19) digits: ",0 SumDigits ENDP END main