常见汇编程序源代码示例.doc_第1页
常见汇编程序源代码示例.doc_第2页
常见汇编程序源代码示例.doc_第3页
常见汇编程序源代码示例.doc_第4页
常见汇编程序源代码示例.doc_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

包含14个常见的汇编程序源代码,所有代码在VC6.0中调试通过;汇编程序采用微机原理接口与技术(钱晓婕)一书中所用的框架。目录1.编写程序,计算下面函数的值并输出。22.输入一个年份(调用readuid子程序),判断是否是闰年.33.输入三个无符号整数(调用readuid子程序),判断并输出这三个数是否能构成一个三角形的三条边。若这三个数能构成一个三角形的三条边,输出三角形的形状:斜三角形、等腰三角形、等边三角形。44.采用无条件和条件转移指令构造while和do while循环结构,完成下面的求和任务并输出sum(sum 为双字)。65.编写程序,求0到100间所有偶数和并输出。要求采用 loop、 while和do while 三种不同的循环结构完成。76.Fibonacci numbers的定义:8f1=1,f2=1, fn= fn-1 + fn-2 n=38编程输出Fibonacci numbers的前30项。87.有一个首地址为array的20个有符号的双字数组,编程分别求出正数的和与负数的和并输出。118.有一个首地址为string的字符串 ,剔除string中所有的空格字符。请从字符串最后一个字符开始逐个向前判断、并进行处理。139.有一个首地址为string的字符串 ,分别统计string中空格、英文字母、数字和其它字符的个数并输出。1410.palindrome(回文)是指正读和反读都一样的数或文本。例如:11、121、12321等,编写程序,求10到10000之间所有回文数并输出。要求每行输出10个数。1511.编写程序,求出所有满足勾股定理且边长 不大于500的直角三角形。1712.编写一个求n!的子程序,利用它求1!+2! +3! +4! +5! +6! +7! +8! 的和并输出。2313.编写一个判断闰年的子程序,利用它求出2010年到2060年之间所有的闰年并输出。2514.编写一个求解双字型有符号数数组元素的平均值子程序,并验证它的正确性。271. 编写程序,计算下面函数的值并输出。include io32.inc.codestart:call readsidcmp eax,0jl smallcmp eax,10jle midjmp largesmall:imul eax,2jmp donemid:imul eax,3jmp donelarge:imul eax,4jmp donedone:call dispsidexit 0end start2. 输入一个年份(调用readuid子程序),判断是否是闰年.include io32.inc.datayes_msg byte is leap,13,10,0no_msg byte no leap,13,10,0.codestart:call readuidmov edx,0mov ecx,4div ecxcmp edx,0je firstjmp secondfirst:mov edx,0mov ecx,100div ecxjne leapjmp secondsecond:mov edx,0mov ecx,400div ecxje leapjmp noleapleap:mov eax,offset yes_msgcall dispmsgjmp donenoleap:mov eax,offset no_msgcall dispmsgjmp donedone:exit 0end start3. 输入三个无符号整数(调用readuid子程序),判断并输出这三个数是否能构成一个三角形的三条边。若这三个数能构成一个三角形的三条边,输出三角形的形状:斜三角形、等腰三角形、等边三角形。include io32.inc.datamsg_dengyao byte dengyao,13,10,0;等腰三角形msg_dengbian byte dengbian,13,10,0;等边三角形msg_zhijiao byte zhijiao,13,10,0;直角三角形msg_xiesanjiao byte xiesanjiaoxing,13,10,0;斜三角形msg_wrong byte wrong,13,10,0;无法构成三角形sqr dword 0.code;在ebx,ecx,edx分别保存三条边的长度start:call readuid;读取第一个数和第二个数到ebx、ecxmov ebx,eaxcall readuidmov ecx,eaxcmp ebx,ecx;确保ebx=3编程输出Fibonacci numbers的前30项。include io32.inc.dataspace byte ,0;输出空格f1 dword 1;fn-1f2 dword 1;fn-2.codestart:mov ecx,30mov eax,f1;输出f1call dispdec ecxmov eax,f2;输出f2call dispdec ecxagain:mov eax,f1add eax,f2;eax = fn-1 + fn-2call dispmov ebx,f2mov f1,ebx;fn-1 = fn-2mov f2,eax;fn-2 = eaxloop againjmp donedisp procpush eax;保护寄存器eaxcall dispuidmov eax,offset spacecall dispmsgpop eax;恢复寄存器eaxretdisp endpdone:exit 0end start;结果:;1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 2;8657 46368 75025 121393 196418 317811 514229 832040 Press any key to continue思考题:在不产生溢出的情况下n的最大值是多少?include io32.inc.dataspace byte ,0;输出空格maxn byte The Maximum n is ,0f1 dword 1;fn-1f2 dword 1;fn-2.codestart:xor ecx,ecxmov eax,f1;输出f1call dispinc ecxmov eax,f2;输出f2call dispinc ecxagain:mov eax,f1add eax,f2;eax = fn-1 + fn-2jc done;有进位时跳出call dispmov ebx,f2mov f1,ebx;fn-1 = fn-2mov f2,eax;fn-2 = eaxinc ecxjmp againdisp proc;输出过程push eaxcall dispuidmov eax,offset spacecall dispmsgpop eaxretdisp endpdone:call dispcrlfmov eax,offset maxncall dispmsgmov eax,ecxcall dispuidcall dispcrlfexit 0end start;运行结果:;1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 2;8657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702;887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 43;3494437 701408733 1134903170 1836311903 2971215073;The Maximum n is 47;Press any key to continue7. 有一个首地址为array的20个有符号的双字数组,编程分别求出正数的和与负数的和并输出。include io32.inc.dataarray dword -1, 2, 4, -8, 6,-4,-9, 8, 4, 0,-49, 4,31,-68, 1,-4, 6, 5, 7, -51sumofpos dword ?;保存正数的和sumofneg dword ?;保存负数的和.codestart:mov ecx,lengthof array;数组元素个数xor edx,edx;数组下标again:mov eax,arrayedx*4;取出下标为edx的数组元素inc edxcmp eax,0jg positivejl negativecontinue:loop againjmp done;所有求和结束positive:add sumofpos,eax;正数求和jmp continuenegative:add sumofneg,eax;负数求和jmp continuedone:mov eax,sumofposcall dispsidcall dispcrlfmov eax,sumofnegcall dispsidcall dispcrlfexit 0end start运行结果:78-194思考题:将求和改为求最大值与最小值。include io32.inc.dataarray dword -1,2, 4, -8, 6,-4,-9, 8, 4, 0,-49, 4,31,-68, 1,-4, 6, 5, 7, -51max dword ?;最大值min dword ?;最小值.codestart:mov eax,array0;为max和min赋初值mov max,eaxmov min,eaxmov ecx,lengthof array;数组元素个数xor edx,edx;数组下标again:mov eax,arrayedx*4;取出下标为edx的数组元素inc edxcmp eax,maxjg _maxcmp eax,minjl _mincontinue:loop againjmp done;数组遍历结束_max:mov max,eax;保存可能的最大值jmp continue_min:mov min,eax;保存可能的最小值jmp continuedone:mov eax,maxcall dispsidcall dispcrlfmov eax,mincall dispsidcall dispcrlfexit 0end start运行结果:31-688. 有一个首地址为string的字符串 ,剔除string中所有的空格字符。请从字符串最后一个字符开始逐个向前判断、并进行处理。include io32.inc.datastring byte Let us have a try !,0;测试字符串.codestart:mov eax,offset string;输出字符串call dispmsgcall dispcrlfmov esi,lengthof string;esi为数组长度dec esi;esi指向最后一个元素again:mov al,stringesicmp al, jz reject;剔除空格dec esijl done;循环结束jmp againreject:mov ecx,esi;ecx指向当前找到的空格reject1:;将后续字符依次往前移mov al,stringecx+1mov stringecx,altest al,al;遇到0结尾jz again;完成一个空格的剔除inc ecxjmp reject1;继续前移后一个字符done:mov eax,offset stringcall dispmsgcall dispcrlfexit 0end start;运行结果:; Let us have a try !;Letushaveatry!9. 有一个首地址为string的字符串 ,分别统计string中空格、英文字母、数字和其它字符的个数并输出。include io32.inc.datastring byte Let us hava a try. 1,2,3,Go!,0;测试字符串countofspace dword ?;空格countofletter dword ?;英文字母countofdigit dword ?;数字countofother dword ?;其他.codestart:xor edx,edx;字符数组下标again:mov al,stringedxcmp al,0;遇到0结束jz donecmp al, ;判断空格jz spacecmp al,a;初步判断为小写字母jge letter_lowercontinue_upper:cmp al,A;初步判断为大写字母jge letter_uppercontinue_digit:cmp al,0;初步判断为数字jge digit0continue_other:;其他字符inc countofothercontinue:;增加下标号,开始下一次循环inc edxjmp againspace:inc countofspacejmp continueletter_lower:cmp al,z;进一步判断是否为小写字母jle lowerjmp continue_upper;继续判断是否大写字母lower:;确定为小写字母inc countofletterjmp continueletter_upper:cmp al,Z;进一步判断是否为大写字母jle upperjmp continue_digit;继续判断是否为数字upper:;确定为大写字母inc countofletterjmp continuedigit0:cmp al,9;进一步判断是否数字jle digit9jmp continue_other;只能是其他字符digit9:inc countofdigitjmp continue;依次输出空格数、英文字母数、数字、其他、done:mov eax,countofspacecall dispuidcall dispcrlfmov eax,countoflettercall dispuidcall dispcrlfmov eax,countofdigitcall dispuidcall dispcrlfmov eax,countofothercall dispuidcall dispcrlfexit 0end start;运行结果:;5;15;3;510. palindrome(回文)是指正读和反读都一样的数或文本。例如:11、121、12321等,编写程序,求10到10000之间所有回文数并输出。要求每行输出10个数。提示:采用div指令把整数分解为单个的数字,并将它们组合成一个新的整数。include io32.inc.datastring byte 0,0,0,0,0;保存转换后的字符串(逆序方式)chushu dword 10;除数10,用于提取个位数字back dword ?;备份eax寄存器的值count dword ?;计算以输出的总数,用于每10个换行.codestart:mov eax,10;eax从10到10000outlp:mov ecx,0;ecx清零,用于记录字符串实际长度mov back,eax;备份eax寄存器的值DigitToString:;将数字转换成字符串xor edx,edx;被除数高32位清零div chushu;edx保存余数,eax保存商add dl,30hmov stringecx,dl;将数字的各位逆序放到string字符串inc ecxtest eax,eax;当商不为零时继续转换更高位的数字到字符串jnz DigitToStringinlp:dec ecx;ecx为字符串最后一个字符的下标mov ebx,0;ebx为字符串首个字符的下标palindrome:cmp ebx,ecx;当ebx=ecx时说明是回文数jge dispcontinue:mov dl,stringebxcmp dl,stringecx;比较首尾字符是否相等jz inlp_next;相等的话比较下一对字符jmp outlp_next;不是回文数inlp_next:inc ebxdec ecxjmp palindromeoutlp_next:mov eax,back;恢复寄存器inc eaxcmp eax,10000jg donejmp outlpdisp:;输出回文数mov eax,backcall dispuidmov al, call dispcinc count;判断是否需要换行mov eax,countcmp eax,10jz crlfjmp outlp_nextcrlf:;换行mov count,0call dispcrlfjmp outlp_nextdone:exit 0end start;运行结果:;11 22 33 44 55 66 77 88 99 101;111 121 131 141 151 161 171 181 191 202;212 222 232 242 252 262 272 282 292 303;313 323 333 343 353 363 373 383 393 404;414 424 434 444 454 464 474 484 494 505;515 525 535 545 555 565 575 585 595 606;616 626 636 646 656 666 676 686 696 707;717 727 737 747 757 767 777 787 797 808;818 828 838 848 858 868 878 888 898 909;919 929 939 949 959 969 979 989 999 1001;1111 1221 1331 1441 1551 1661 1771 1881 1991 2002;2112 2222 2332 2442 2552 2662 2772 2882 2992 3003;3113 3223 3333 3443 3553 3663 3773 3883 3993 4004;4114 4224 4334 4444 4554 4664 4774 4884 4994 5005;5115 5225 5335 5445 5555 5665 5775 5885 5995 6006;6116 6226 6336 6446 6556 6666 6776 6886 6996 7007;7117 7227 7337 7447 7557 7667 7777 7887 7997 8008;8118 8228 8338 8448 8558 8668 8778 8888 8998 9009;9119 9229 9339 9449 9559 9669 9779 9889 9999 Press any key to continue11. 编写程序,求出所有满足勾股定理且边长 不大于500的直角三角形。 提示:采用三重循环,用穷举法求解。include io32.inc.datacircum dword ?ebx2 dword ?;第一个数的平方ecx2 dword ?;第二个数的平方esi2 dword ?;第三个数的平方count dword ?;满足的三角形的总数sep byte - ,0;分隔符.codestart:first:;第一层循环初始化mov ebx,1first2:second:;第二层循环初始化mov ecx,1second2:third:;第三层循环初始化mov esi,1third2:mov eax,ebx;边长是否大于500add eax,ecxadd eax,esicmp eax,500ja second_nextcmp ebx,ecx;确保三边从小到大排序jg third_nextcmp ecx,esijg third_nextmov eax,ebx;计算平方和mul ebxmov ebx2,eaxmov eax,ecxmul ecxmov ecx2,eaxmov eax,esimul esimov esi2,eaxmov eax,ebx2add eax,ecx2cmp eax,esi2;满足勾股定理?jz outputthird_next:inc esicmp esi,250;任意一条边都不超过250jge second_nextjmp third2second_next:inc ecxcmp ecx,250;任意一条边都不超过250jge first_nextjmp second2first_next:inc ebxcmp ebx,250;任意一条边都不超过250jge done;穷举结束jmp first2output:inc count;输出编号mov eax,countcall dispuidmov eax,offset sep;输出分隔符call dispmsgmov eax,ebx;输出满足勾股定理的3个数call dispuidmov eax, call dispcmov eax,ecxcall dispuidmov eax, call dispcmov eax,esicall dispuidmov eax, call dispccall dispcrlfjmp third_nextdone:exit 0end start;运行结果:;1 - 3 4 5;2 - 5 12 13;3 - 6 8 10;4 - 7 24 25;5 - 8 15 17;6 - 9 12 15;7 - 9 40 41;8 - 10 24 26;9 - 11 60 61;10 - 12 16 20;11 - 12 35 37;12 - 13 84 85;13 - 14 48 50;14 - 15 20 25;15 - 15 36 39;16 - 15 112 113;17 - 16 30 34;18 - 16 63 65;19 - 17 144 145;20 - 18 24 30;21 - 18 80 82;22 - 19 180 181;23 - 20 21 29;24 - 20 48 52;25 - 20 99 101;26 - 21 28 35;27 - 21 72 75;28 - 21 220 221;29 - 22 120 122;30 - 24 32 40;31 - 24 45 51;32 - 24 70 74;33 - 24 143 145;34 - 25 60 65;35 - 26 168 170;36 - 27 36 45;37 - 27 120 123;38 - 28 45 53;39 - 28 96 100;40 - 28 195 197;41 - 30 40 50;42 - 30 72 78;43 - 30 224 226;44 - 32 60 68;45 - 32 126 130;46 - 33 44 55;47 - 33 56 65;48 - 33 180 183;49 - 35 84 91;50 - 35 120 125;51 - 36 48 60;52 - 36 77 85;53 - 36 105 111;54 - 36 160 164;55 - 39 52 65;56 - 39 80 89;57 - 40 42 58;58 - 40 75 85;59 - 40 96 104;60 - 40 198 202;61 - 42 56 70;62 - 42 144 150;63 - 44 117 125;64 - 45 60 75;65 - 45 108 117;66 - 45 200 205;67 - 48 55 73;68 - 48 64 80;69 - 48 90 102;70 - 48 140 148;71 - 48 189 195;72 - 49 168 175;73 - 50 120 130;74 - 51 68 85;75 - 51 140 149;76 - 52 165 173;77 - 54 72 90;78 - 55 132 143;79 - 56 90 106;80 - 56 105 119;81 - 56 192 200;82 - 57 76 95;83 - 57 176 185;84 - 60 63 87;85 - 60 80 100;86 - 60 91 109;87 - 60 144 156;88 - 60 175 185;89 - 63 84 105;90 - 64 120 136;91 - 65 72 97;92 - 65 156 169;93 - 66 88 110;94 - 66 112 130;95 - 69 92 115;96 - 70 168 182;97 - 72 96 120;98 - 72 135 153;99 - 72 154 170;100 - 75 100 125;101 - 75 180 195;102 - 78 104 130;103 - 78 160 178;104 - 80 84 116;105 - 80 150 170;106 - 80 192 208;107 - 81 108 135;108 - 84 112 140;109 - 84 135 159;110 - 84 187 205;111 - 85 132 157;112 - 87 116 145;113 - 88 105 137;114 - 88 165 187;115 - 90 120 150;116 - 93 124 155;117 - 95 168 193;118 - 96 110 146;119 - 96 128 160;120 - 96 180 204;121 - 99 132 165;122 - 99 168 195;123 - 100 105 145;124 - 102 136 170;125 - 104 153 185;126 - 105 140 175;127 - 108 144 180;128 - 111 148 185;129 - 114 152 190;130 - 117 156 195;131 - 119 120 169;132 - 120 126 174;133 - 120 160 200;134 - 123 164 205;135 - 130 144 194;136 - 133 156 205;137 - 140 147 20312. 编写一个求n!的子程序,利用它求1!+2! +3! +4! +5! +6! +7! +8! 的和并输出。l 参数的传递采用:寄存器传递include io32.inc.datasum dword 0;求和结果.codestart:mov ecx,1again:mov eax,ecx;寄存器传递call factadd sum,eaxinc ecxcmp ecx,8;ecx8时终止循环jbe againmov eax,sumcall dispuidexit 0fact proc;阶乘计算子程序push edxpush ecx;保护寄存器mov ecx,eaxfact_again:;计算阶乘,结果在eax寄存器dec ecxcmp ecx,0jz overmul ecxjmp fact_againover:pop ecx;恢复寄存器并返回pop edxretfact endpend start;运算结果:46233l 参数的传递采用:全局变量传递include io32.inc.datasum dword 0;求和结果temp dword 0;全局变量传递:阶乘结果(从1开始).codestart:mov ecx,1again:mov temp,ecxcall factmov eax,tempadd sum,eaxinc ecxcmp ecx,8;ecx8时终止循环jbe againmov eax,sumcall dispuidexit 0fact proc;阶乘计算子程序push eaxpush edxpush ecx;保护寄存器mov eax,tempmov ecx,eaxfact_again:;计算阶乘,结果在eax寄存器dec ecxcmp ecx,0jz overmul ecxjmp fact_againover:mov temp,eaxpop ecx;恢复寄存器并返回pop edxpop eaxretfact endpend start;运算结果:46233l 参数的传递采用:堆栈传递include io32.inc.datasum dword ?;求和结果.codestart:mov ecx,1again:push ecx;堆栈传递call factadd esp,4;平衡堆栈add sum,eaxinc ecxcmp ecx,8;ecx8是终止循环jbe againmov eax,sumcall dispuidexit 0fact proc;阶乘计算子程序push edxpush ecx;保护寄存器mov eax,esp+12mov ecx,eaxfact_again:;计算阶乘,结果在eax寄存器dec ecxcmp ecx,0jz overmul ecxjmp fact_agai

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论