COMMENT !
Calculate an arithmetic mean, (1+2+3+...+n) / n:
1. Read the n.
2. Calculate the arithmetic series, 1+2+3+...+n.
3. Divide the sum by n.
4. Display the quotient, the arithmetic mean.
Require Irvine32.inc and Irvine32.lib (Kip Irvine's library)
!
INCLUDE Irvine32.inc ; Include Irvine32 macros and procedures
.data
prompt1 BYTE "Enter the n: ", 0
prompt2 BYTE "The arithmetic series: ", 0
prompt3 BYTE "The arithmetic mean: ", 0
n SDWORD ?
result SDWORD 0
.code
main PROC
call Crlf ; New line
; Read the n
mov edx, OFFSET prompt1
call WriteString
call ReadInt
mov n, eax ; n = eax
mov ebx, eax ; ebx = eax
; Calculate the aritmetic series
Loop1:
cmp ebx, 0 ; ebx: n
je Divide ; if ( n == 0 ) goto Divide
mov eax, result ; eax = result
add eax, ebx ; eax = result + n
mov result, eax ; result = eax
mov eax, ebx ; eax = n
sub eax, 1 ; n = n - 1
mov ebx, eax ; ebx = n
jmp Loop1
Divide:
; Print the aritmetic series
mov edx, OFFSET prompt2
call WriteString
mov eax, result
call WriteInt
call Crlf
; Calculate the aritmetic mean
mov eax, result ; eax = sum
mov ebx, n ; ebx = n
xor edx, edx ; clear edx
div ebx ; eax = sum / n
mov result, eax ; result = eax
; Print the aritmetic mean
mov edx, OFFSET prompt3
call WriteString
mov eax, result
call WriteInt
call Crlf
call Crlf
exit ; Exit to operating system
main ENDP
END main
|