汇编语言系列文章仅作为实验报告和汇编学习参考,不作为任何技术文章,还望大佬们勿喷。
1. 实验名称
有符号整数排序
2. 实验目的
熟悉逻辑指令,移位指令,比较指令与条件判断指令,并灵活运用这些指令。
3. 实验要求
从键盘输入多个有符号整数并进行排序,然后查找用户再次输入的任何一个整数,并以二进制编码输出其在排序结果中的下标。
4. 实验内容
- 从键盘接收多个有符号整数
- 对输入的多个整数进行排序
- 再次接收用户输入的一个整数,并在排序结果中查找;
- 以二进制编码输出下标。若未找到,则输出提示。
- (可选)更高要求:(涉及到乘法指令) 在第3步输入一个整数时,不使用ReadInt等别人写的过程,而是自己写一个读入有符号整数的过程(此时可以使用readkey读取输入的字符。注意从键盘读入的均是ASCII代码字符)。
提示:
可以先做1、3、4步,最后再加入第2步。
5. 实验步骤或源代码、结果
1. 实验步骤
查找数据成功
查找数据不存在时
2. 实验源代码、结果
INCLUDE Irvine32.inc.datastr1 BYTE "How many integers you want input? ", 0str2 BYTE "Please input a integer: ", 0str3 BYTE "The index of the integer is: ", 0str4 BYTE "The integer you want to seek is: ", 0str5 BYTE "Cannot find the integer.", 0IntegerSize DWORD 0Array DWORD 0FFFFH DUP(?).codemain PROCcall InputSizecall InputArraycall SortIntegersmov edx, offset str4call WriteStringcall ReadIntcall BinSeekcmp eax, -1je NotFoundmov edx,offset str3call WriteStringadd eax, 1call WriteBinBcall Crlfjmp ProcessExitNotFound:mov edx, offset str5call WriteStringcall CrlfProcessExit:exitmain ENDPInputSize PROC USES edx eaxmov edx, offset str1call WriteStringcall ReadDecmov IntegerSize, eaxretInputSize ENDPInputArray PROC USES eax ecx esimov esi, 0mov eax, 1mov ecx, IntegerSizeL1:mov edx, offset str2call WriteStringpush eaxcall ReadIntmov Array[esi], eaxadd esi, TYPE Arraypop eaxloop L1InputArrayRet:retInputArray ENDPSortIntegers PROC USES ecx eax esi edimov ecx, IntegerSizedec ecxL1:mov esi, ecxmov eax, esicall MulTypeArraymov esi, eaxmov edi, esiL2:mov eax, Array[esi]sub edi, TYPE Arraycmp eax, Array[edi]jge L2Retxchg eax, Array[edi]mov Array[esi], eaxL2Ret:cmp edi, 0jg L2loop L1retSortIntegers ENDPMulTypeArray PROC USES edxsal eax, 2retMulTypeArray ENDPDivTypeArray PROC USES edxsar eax, 2retDivTypeArray ENDPBinSeek PROC USES edx esi edi ecxmov ecx, eaxmov esi, 0mov edi, IntegerSizedec ediL1:cmp edi, esijb NotFoundmov edx, esiadd edx, edisar edx, 1mov eax, edxcall MulTypeArraymov edx, eaxcmp ecx, Array[edx]jl LessDealje Foundmov eax, edxcall DivTypeArrayinc eaxmov esi, eaxjmp L1LessDeal:mov eax, edxdec eaxcall DivTypeArraymov edi, eaxjmp L1Found:mov eax, edxcall DivTypeArrayjmp BinSeekRetNotFound:mov eax, -1BinSeekRet:retBinSeek ENDPend main
6. 实验结论和心得体会
- 通过输入想要输入的数字个数来确定数组的大小,不能像高级语言那种一次性输完;
- 熟悉了逻辑指令,移位指令,比较指令与条件判断指令,并灵活运用这些指令;
- 数组的索引以4字节增加;
- 冒泡排序中要注意保存和恢复外循环计数器;
- 确定了索引值后以二进制值方式输出,但是作者提供的WriteBinB是输出eax的值,不能从第一个1开始输出,这里下来还需要考虑进行重写。
