TITLE Demonstrating Procedures (ArraySum.asm) .686 .MODEL flat, stdcall .STACK 4096 INCLUDE Irvine32.inc .data sum DWORD ? .code main PROC push OFFSET sum call SumDigits 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 push ebp ; save old EBP value mov ebp, esp ; new value of EBP sub esp, 20 ; allocate 20 bytes for local string variable pushad ; save general-purpose registers lea edx, prompt ; write prompt string call WriteString lea edx, [ebp-20] ; 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, [ebp+8] ; EBX = parameter = address of sum mov [ebx], eax ; store sum indirectly popad ; pop general-purpose registers mov esp, ebp ; free local string variable pop ebp ; restore EBP register ret 4 ; return and clean the parameter's 4 bytes prompt BYTE "Enter a string of (max 19) digits: ",0 SumDigits ENDP END main