实验4排序和检索程序设计一.实验目的:(1)掌握常用的DOS输入输出系统功能调用。(2)学习逻辑运算指令的用法。(3)掌握排序和检索程序的设计使用方法。二.实验内容:编写程序1:将内存中10个无符号数(长度为2字节)由小到大排序。编写程序2:在上述已排好序的数据区里查找某一个数。若找到,显示其在数据区中的位置,否则显示‘N’字符。编写主程序,通过调用程序1和程序2,实现排序和检索。1、编译:c:\masm.asm2、链接:c:\link.obj3、调试:c:\debug.exe4、运行:c:\.exe(1)1、编译:C:\masm4-1.asmC:\masmmasm4-1Microsoft(R)MacroAssemblerVersion5.00Copyright(C)MicrosoftCorp1981-1985,1987.Allrightsreserved.Objectfilename[4-1.OBJ]:Sourcelisting[NUL.LST]:Cross-reference[NUL.CRF]:4-1.ASM(25):warningA4031:Operandtypesmustmatch4-1.ASM(80):warningA4031:Operandtypesmustmatch4-1.ASM(84):warningA4031:Operandtypesmustmatch50158+415346Bytessymbolspacefree3WarningErrors0SevereErrors2、链接:c:\link4-1.objC:\masmlink4-1Microsoft(R)OverlayLinkerVersion3.65Copyright(C)MicrosoftCorp1983-1988.Allrightsreserved.RunFile[4-1.EXE]:ListFile[NUL.MAP]:Libraries[.LIB]:3、调试:c:\debug4-1.exe1)、先用命令U反汇编整个程序,查看每条指令的物理地址,其显示内容的含义为:内存地址指令代码反汇编的程序代码C:\masmdebug4-1.exe-u1434:0000B83114MOVAX,1431数据段的首地址是:1431代码段的首地址是:14341434:00038ED8MOVDS,AX1434:00058D1E0000LEABX,[0000]1434:0009B90A00MOVCX,000A1434:000CE82800CALL00371434:000FBA1400MOVDX,00141434:0012B409MOVAH,091434:0014CD21INT211434:0016B401MOVAH,011434:0018CD21INT211434:001A240FANDAL,0F1434:001CA22300MOV[0023],AL1434:001FB220MOVDL,202)、从上一步结果中找到数据段的首地址142E,查看里面存储的数据,数据段从首地址1431中存储了:2,4,1,8,3,6,10,7,13,11inputthekey:$-d1431:00001431:00000200040001000800-030006000A000700................1431:00100D000B00696E7075-7420746865206B65....inputtheke1431:0020793A240500000000-0000000000000000y:$.............1431:0030B831148ED88D1E00-00B90A00E82800BA.1...........(..1431:00401400B409CD21B401-CD21240FA22300B2.....!...!$..#..1431:005020B402CD218B1E00-00B90A008B162300...!.........#.1431:0060E82800B44CCD2150-33C04951BB00008B.(..L.!P3.IQ....1431:00708700003B87020076-0887870200898700...;...v........-.-3)、从反汇编地址中找到把数据段的首地址142E传给寄存器AX的指令地址:0005,用G命令执行到地址0005,然后用T命令单步执行,看寄存器AX,DS,ES的内容的变化-g0005AX=1431BX=0000CX=00F1DX=0000SP=0028BP=0000SI=0000DI=0000DS=1431ES=141ESS=142ECS=1434IP=0005NVUPEIPLNZNAPONC1434:00058D1E0000LEABX,[0000]DS:0000=0002-tAX=1431BX=0000CX=00F1DX=0000SP=0028BP=0000SI=0000DI=0000DS=1431ES=141ESS=142ECS=1434IP=0009NVUPEIPLNZNAPONC1434:0009B90A00MOVCX,000A-tAX=1431BX=0000CX=000ADX=0000SP=0028BP=0000SI=0000DI=0000DS=1431ES=141ESS=142ECS=1434IP=000CNVUPEIPLNZNAPONC1434:000CE82800CALL0037-tAX=1431BX=0000CX=000ADX=0000SP=0026BP=0000SI=0000DI=0000DS=1431ES=141ESS=142ECS=1434IP=0037NVUPEIPLNZNAPONC1434:003750PUSHAX-tAX=1431BX=0000CX=000ADX=0000SP=0024BP=0000SI=0000DI=0000DS=1431ES=141ESS=142ECS=1434IP=0038NVUPEIPLNZNAPONC1434:003833C0XORAX,AX-tAX=0000BX=0000CX=000ADX=0000SP=0024BP=0000SI=0000DI=0000DS=1431ES=141ESS=142ECS=1434IP=003ANVUPEIPLZRNAPENC1434:003A49DECCX-tAX=0000BX=0000CX=0009DX=0000SP=0024BP=0000SI=0000DI=0000DS=1431ES=141ESS=142ECS=1434IP=003BNVUPEIPLNZNAPENC1434:003B51PUSHCX-tAX=0000BX=0000CX=0009DX=0000SP=0022BP=0000SI=0000DI=0000DS=1431ES=141ESS=142ECS=1434IP=003CNVUPEIPLNZNAPENC1434:003CBB0000MOVBX,0000-tAX=0000BX=0000CX=0009DX=0000SP=0022BP=0000SI=0000DI=0000DS=1431ES=141ESS=142ECS=1434IP=003FNVUPEIPLNZNAPENC1434:003F8B870000MOVAX,[BX+0000]DS:0000=0002-tAX=0002BX=0000CX=0009DX=0000SP=0022BP=0000SI=0000DI=0000DS=1431ES=141ESS=142ECS=1434IP=0043NVUPEIPLNZNAPENC1434:00433B870200CMPAX,[BX+0002]DS:0002=0004-tAX=0002BX=0000CX=0009DX=0000SP=0022BP=0000SI=0000DI=0000DS=1431ES=141ESS=142ECS=1434IP=0047NVUPEINGNZACPOCY1434:00477608JBE0051-4)然后直接G命令运行完毕。-ginputthekey:34输入关键字KEY:3,输出关键字的位置:4Programterminatednormally4、运行4-1.exe,完全执行程序,最后再查看数据段最后的结果:-d1431:00001431:00000100020003000400-0600070008000A00................1431:00100B000D00696E7075-7420746865206B65....inputtheke1431:0020793A240300040000-0000000000000000y:$.............1431:0030B831148ED88D1E00-00B90A00E82800BA.1...........(..1431:00401400B409CD21B401-CD21240FA22300B2.....!...!$..#..1431:005020B402CD218B1E00-00B90A008B162300...!.........#.1431:0060E82800B44CCD2150-33C04951BB00008B.(..L.!P3.IQ....1431:00708700003B87020076-0887870200898700...;...v........-Q三.实验代码:1)2)STACKSEGMENTSTACK'S'DB40DUP(0)STACKENDSDATASEGMENTARYDW2,4,1,8,3,6,10,7,13,11NEQU($-ARY)/2inputDB'inputthekey:$'KEYDW5RESULTDW?DATAENDSCODESEGMENTASSUMECS:CODE,DS:DATA,SS:STACKSTART:MOVAX,DATAMOVDS,AXLEABX,ARY;取数组首地址MOVCX,NCALLARYSORT;调用排序子程序MOVDX,OFFSETINPUTMOVAH,9INT21HMOVAH,1;使用1号功能键,输入关键字INT21HANDAL,0FHMOVKEY,AL;(第25行)MOVDL,20H;输出一个换行符MOVAH,2INT21HMOVBX,ARYMOVCX,NMOVDX,KEYCALLARYFIND;调用查找子程序MOVAH,4CHINT21H;子程序名:ARYSORT;功能:从小到大排序字型10个元素的数组;入口参数:BX=数组地址;CX=数组元素个数;DX=要查找的关键字;出口参数:无ARYSORTPROCNEARPUSHAXXORAX,AXDECCXLOOP1:PUSHCXMOVBX,0LOOP2:MOVAX,ARY[BX]CMPAX,ARY[BX+2]JBEOKXCHGAX,ARY[BX+2]MOVARY[BX],AXOK:ADDBX,2LOOPLOOP2POPCXLOOPLOOP1POPAXRETARYSORTENDP;子程序名:ARYFOUND;功能:从排好序的数组中查找某一个数。若找到,显示其在数据区中的位置,否则显示‘N’字符;入口参数:BX=数组地址;CX=数组元素个数;DX=要查找的关键字;出口参数:无ARYFINDPROCNEARPUSHAXXORAX,AXMOVBX,0AGAIN:MOVAX,ARY[BX]CMPAX,KEYJENEXT1CMPCX,1JENEXT2ADDBX,2LOOPAGAINNEXT1:MOVRESULT,BXADDBX,30HMOVDL,BXJMPEXITNEXT2:MOVRESULT,4EHMOVDL,RESULTEXIT:MOVAH,2INT21HPOPAXRETARYFINDENDPCODEENDSENDSTART四.实验总结:通过这次试验掌握了掌握常用的DOS输入输出系统功能调