实验目标
1)从键盘接收多个有符号整数
2)对输入的多个整数进行排序
3)再次接收用户输入的一个整数,并在排序结果中查找;
4)以二进制编码输出下标。若未找到,则输出提示。
实验代码
TITLE Integer Summation Program (Sum2.asm)
INCLUDE Irvine32.inc
INTEGER_COUNT = 5
.data
str1 BYTE "Enter a signed integer: ",0
str2 BYTE "The sum of the integers is ",0
str3 BYTE "The BubbleSort result is:",0
str4 BYTE "Please input the finding number:",0
str5 BYTE "找到了,二进制下标为:",0
str6 BYTE "没有找到该数字!!",0
array DWORD INTEGER_COUNT DUP(?)
ARRAY_SIZE = 20
Array1 DWORD ARRAY_SIZE DUP(?)
.code
main PROC
call Clrscr
mov esi,OFFSET array
mov ecx,INTEGER_COUNT
call PromptForIntegers
call BubbleSort
call DisplayResult
call InputFindNum
call FindNum
exit
main ENDP
;冒泡降序
BubbleSort proc uses eax ecx esi ; esi指向要排序的双字数组,ecx为元素个数
Dec ecx
L1:push ecx
push esi
L2:mov eax , [esi]
cmp eax , [esi + 4]
jg L3
xchg eax , [esi+4]
mov [esi] ,eax
L3:add esi ,4
loop L2
pop esi
pop ecx
Loop L1
ret
BubbleSort endp
;输入数字
PromptForIntegers PROC USES ecx edx esi
mov edx,OFFSET str1
L1: call WriteString
call ReadInt
call Crlf
mov [esi],eax
add esi,TYPE DWORD
loop L1
ret
PromptForIntegers ENDP
;输入要查找的数字
InputFindNum PROC USES edx
mov edx,OFFSET str4
call WriteString
call ReadInt
call Crlf
;输入之后,新的数字保存在EAX里面
;call DumpRegs
ret
InputFindNum ENDP
;查找数字
FindNum PROC USES esi ecx edx ebx
L1:cmp eax,[esi]
je Feng1
add esi,TYPE DWORD
loop L1
mov edx,OFFSET str6
call WriteString
ret
Feng1:mov edx,OFFSET str5
mov ebx,INTEGER_COUNT
sub ebx,ecx
mov eax,ebx
call WriteString
call WriteBin
call Crlf
ret
FindNum ENDP
;打印排序结果
DisplayResult PROC USES edx ecx esi
mov edx,OFFSET str3
call WriteString
call Crlf
mov eax,0
L1:mov eax,[esi]
add esi,TYPE DWORD
call WriteInt
call Crlf
Loop L1
ret
DisplayResult ENDP
END main