33,317
社区成员
发帖
与我相关
我的任务
分享
.程序通过调用函数求出数组的最大值,最小值,平均值
.386
.MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
include io.h ; header file for input/output
cr equ 0dh ; carriage return character
Lf equ 0ah ; line feed
maxlen equ 100 ;数组最大长度
.STACK 4096 ; reserve 4096-byte stack
.DATA ; reserve storage for data
prompt1 BYTE 'Now Init the Array ',cr,Lf,Lf,0
prompt2 BYTE 'please input a number :',0
prompt3 BYTE 'please input the length of Array :',0
MaxNum BYTE cr,Lf,'the max number is : ',0
MinNum BYTE cr,Lf, 'the min number is : ',0
Ave BYTE cr,Lf,'the average number is : ',0
number BYTE 16 DUP (?)
Array DWORD maxlen DUP(?)
Result DWORD 3 DUP(?)
Len DWORD ?
Sum DWORD ?
Value BYTE 11 DUP(?) ,0
.CODE
; start of main program code
MaxMinAvg PROC NEAR32 ;结果存放在Result中
push ebp ; establish stack frame
mov ebp,esp
pushad
pushf ; save flags
mov ebx,[ebp+16] ;数组首地址
mov ecx,[ebp+12] ;数组长度
mov edx,[ebp+8] ;结果数组地址
mov eax,[ebx] ;累加和
mov [edx],eax ;初始化数据
mov [edx+4],eax
mov [edx+8],eax
forcount:
mov eax,[ebx]
cmp eax,[edx]
jg Big;
cmp eax,[edx+4]
jb Small
jmp Com
Small :
mov [edx+4],eax
jmp Com
Big :
mov [edx],eax
jmp Com
Com :
add eax,Sum
mov Sum,eax
add ebx,4
loop forcount
endfor :
mov eax,Sum
mov ecx,[ebp+12] ;数组长度
idiv ecx
mov [edx+8],eax ;平均数
popf
popad
pop ebp
ret
MaxMinAvg ENDP
_start:
output prompt1
Safe: output prompt3
input number,16
atod number
cmp eax,maxlen
jg Safe ; 超过范围再次输入
mov len,eax ;输入数组长度
lea ebx,Array ;取数组首地址
mov ecx,len
forArray:
output prompt2
input number,16
atod number
mov [ebx],eax
add ebx,4
loop forArray ;循环
lea ebx,Array ;取数组首地址
push ebx ;压入数组首地址
mov ecx,len
push ecx ;压入数组长度
lea edx,Result
push edx ;压入结果数组
call MaxMinAvg
add esp,12
;输出结果
output MaxNum
dtoa Value,[edx]
output Value
output MinNum
dtoa Value,[edx+4]
output Value
output Ave
dtoa Value,[edx+8]
output Value
INVOKE ExitProcess, 0 ; exit with return code 0
PUBLIC _start ; make entry point public
END ; end of source code