TITLE Program Template (template.asm) ; Author: Roger ; Assignment Number: demo Due Date: ; Description: This program gets two integers, and calculate the ; summation from the lower to the upper. ; All variables are global ... using parameter passing ; no data validation INCLUDE Irvine32.inc ; (insert constant definitions here) .data a DWORD ? ;lower limit b DWORD ? ;upper limit sum DWORD ? ;sum of integers from a to b rules1 BYTE "Enter a lower limit and an upper limit, and I'll show",0 rules2 BYTE "the summation of integers from lower to upper.",0 out1 BYTE "The summation of integers from ",0 out2 BYTE " to ",0 out3 BYTE " is ",0 prompt1 BYTE "Lower limit: ",0 prompt2 BYTE "Upper limit: ",0 ; (insert variable definitions here) .code main PROC call intro ;getData(&a, &b); push offset a push offset b call getData ;calculation(a, b, &sum); push a push b push offset sum call calculate ;showResults(a, b, sum); push a push b push sum call showResults exit ; exit to operating system main ENDP ; Procedure to introduce the program. ; receives: none ; returns: none ; preconditions: none ; registers changed : edx intro PROC mov edx, OFFSET rules1 call WriteString call Crlf mov edx, OFFSET rules2 call WriteString call Crlf call Crlf ret intro ENDP ;Procedure to get values for a and b from the user. ;receives: none ;returns: user input values for global variables a and b ;preconditions: none ;registers changed: eax,ebx,edx getData PROC push ebp mov ebp, esp ; esp, ebp --> old ebp ; ebp+4 --> return @ ; ebp+8 --> @b ; ebp+12 --> @a mov ebx, [ebp+12] ;ebx contains @a mov edx, offset prompt1 call WriteString call ReadInt ;store the user input into eax mov [ebx], eax ; a contains lower limit mov ebx, [ebp+8] ;ebx contains @b mov edx, offset prompt2 call WriteString call ReadInt ;store the user input into eax mov [ebx], eax ; b contains upper limit pop ebp ret 8 getData ENDP ; Procedure to calculate the summation of integers from a to b. ; receives: aand b are global variables ; returns: global variable sum = a + (a + 1) + ... + b ; preconditions: a <= b ; registers changed : eax, ebx, ecx calculate PROC push ebp mov ebp, esp ; esp, ebp --> old ebp ; ebp+4 --> return @ ; ebp+8 --> @sum ; ebp+12 --> b // upper ; ebp+16 --> a // lower mov eax, 0 ;initialize the accum ;ecx = b - a + 1 mov ecx, [ebp+12] ; put b into ecx mov ebx, [ebp+16] ; put a into ebx sub ecx, ebx inc ecx top: add eax, ebx inc ebx loop top ;save the result into sum mov ebx, [ebp+8] ; ebx contains @sum mov [ebx], eax pop ebp ret 12 calculate ENDP ; Procedure to display the results(a, b, and sum) ; receives: a, b, and sum are global variables ; returns: none ; preconditions: sum has been calculated ; registers changed : eax, edx showResults PROC push ebp mov ebp, esp ; esp, ebp --> old ebp ; ebp+4 --> return @ ; ebp+8 --> sum ; ebp+12 --> b ; ebp+16 --> a mov edx, offset out1 call WriteString mov eax, [ebp+16] call WriteDec mov edx, offset out2 call WriteString mov eax, [ebp+12] call WriteDec mov edx, offset out3 call WriteString mov eax, [ebp+8] call WriteDec call Crlf pop ebp ret 12 showResults ENDP END main