【VC++开源代码栏目提醒】:网学会员在VC++开源代码频道为大家收集整理了“常见汇编程序源代码示例 - 其它资料“提供大家参考,希望对大家有所帮助!
包含 14 个常见的汇编程序源
代码,所有
代码在
VC6.0 中调试通过;汇编程序采用 (钱晓婕) 《微机原理接口与技术》 一书中所用的框架。
目录1. 编写程序,计算下面函数的值并输出。
............................................................... 22. 输入一个年份调用 readuid 子程序判断是否是闰年. ....................................... 23. 输入三个无符号整数调用 readuid 子程序判断并输出这三个数是否能构成一个三角形的三条边。
若这三个数能构成一个三角形的三条边, 输出三角形的形状:斜三角形、等腰三角形、等边三角形。
.............................................................. 34. 采用无条件和条件转移指令构造 while 和 do while 循环结构,完成下面的求和任务并输出 sumsum 为双字。
................................................................................ 55. 编写程序, 0 到 100 间所有偶数和并输出。
求 要求采用 loop、while 和 do while三种不同的循环结构完成。
........................................................................................... 66. Fibonacci numbers 的定义:..................................................................................... 8f11f21 fn fn-1 fn-2 ngt3 .................................................................................. 8编程输出 Fibonacci numbers 的前 30 项。
.................................................................... 87. 有一个首地址为 array 的 20 个有符号的双字数组, 编程分别求出正数的和与负数的和并输出。
..........................................................................................................108. 有一个首地址为 string 的字符串,剔除 string 中所有的空格字符。
请从字符串最后一个字符开始逐个向前判断、并进行处理。
................................................129. 有一个首地址为 string 的字符串,分别统计 string 中空格、英文字母、数字和其它字符的个数并输出。
.........................................................................................1310. palindrome回文是指正读和反读都一样的数或文本。
11、 例如: 121、 12321等,编写程序,求 10 到 10000 之间所有回文数并输出。
要求每行输出 10 个数。
1511. 编写程序,求出所有满足勾股定理且边长不大于 500 的直角三角形。
...1712. 编写一个求 n的子程序, 利用它求 12 3 4 5 6 7 8 的和并输出。
2213. 编写一个判断闰年的子程序,利用它求出 2010 年到 2060 年之间所有的闰年并输出。
......................................................................................................................2514. 编写一个求解双字型有符号数数组元素的平均值子程序,并验证它的正确性。
26 1. 编写程序,计算下面函数的值并输出。
include io32.inc .codestart: call readsid cmp eax0 jl small cmp eax10 jle mid jmp largesmall: imul eax2 jmp donemid: imul eax3 jmp donelarge: imul eax4 jmp donedone: call dispsid exit 0 end start 2. 输入一个年份调用 readuid 子程序判断是否是闰年. include io32.inc .data yes_msg byte is leap13100 no_msg byte no leap13100 .codestart: call readuid mov edx0 mov ecx4 div ecx cmp edx0 je first jmp secondfirst: mov edx0 mov ecx100 div ecx jne leap jmp secondsecond: mov edx0 mov ecx400 div ecx je leap jmp noleapleap: mov eaxoffset yes_msg call dispmsg jmp donenoleap: mov eaxoffset no_msg call dispmsg jmp donedone: exit 0 end start 3. 输入三个无符号整数调用 readuid 子程序判断并输出这三个数是否能 构成一个三角形的三条边。
若这三个数能构成一个三角形的三条边,输出三 角形的形状:斜三角形、等腰三角形、等边三角形。
include io32.inc .data msg_dengyao byte dengyao13100 等腰三角形 msg_dengbian byte dengbian13100 等边三角形 msg_zhijiao byte zhijiao13100 直角三角形 msg_xiesanjiao byte xiesanjiaoxing13100斜三角形 msg_wrong byte wrong13100 无法构成三角形 sqr dword 0 .code在 ebxecxedx 分别保存三条边的长度start: call readuid 读取第一个数和第二个数到 ebx、ecx mov ebxeax call readuid mov ecxeax cmp ebxecx 确保 ebxltecx jg great jmp nextgreat: xchg ebxecxnext: 读取第三个数到 edx call readuid mov edxeax cmp ebxedx jg thirdsmall 如果第三个数最小 cmp ecxedx jg thirdmid 如果第三个数在最中间 jmp order如果第三个数最小thirdsmall: mov eaxedx mov edxecx mov ecxebx mov ebxeax jmp order如果第三个数在最中间thirdmid: xchg ecxedx jmp order执行至此三个数ebxecxedx已经从小到大排序order: mov eaxebx add eaxecx cmp eaxedx 最小的两边之和大于第三边 jle wrong cmp ebxecx je b_equal_c 第一条边等于第二条边 cmp ecxedx je c_equal_d 第二条边等于第三条边 jmp general 非等腰等边三角形b_equal_c: cmp ecxedx 继续比较确认是否为等边三角形 je dengbian jmp dengyaoc_equal_d: cmp ebxecx 继续比较确认是否为等边三角形 je dengbian jmp dengyao执行至此能否构成三角形以及三角形是否等边等腰已经确定输出判断是否为直角三角形general: mov eaxebx imul eaxeax mov sqreax mov eaxecx imul eaxeax add sqreax mov eaxedx imul eaxeax cmp sqreax je zhijiao jmp xiesanjiaoxiesanjiao: mov eaxoffset msg_xiesanjiao call dispmsg jmp lastzhijiao: mov eaxoffset msg_zhijiao call dispmsg jmp lastdengbian: mov eaxoffset msg_dengbian call dispmsg jmp lastdengyao: mov eaxoffset msg_dengyao call dispmsg jmp lastwrong: mov eaxoffset msg_wrong call dispmsg jmp last输出完毕运行结束last: exit 0 end start 4. 采用无条件和条件转移指令构造 while 和 do while 循环结构,完成下面 的求和任务并输出 sumsum 为双字。
include io32.inc .data sum dword 0 .codestart: call readuid mov ecxeaxgetsum: cmp ecx0 je next add sumecx dec ecx jmp getsumnext: mov eaxsum call dispuid call dispcrlf exit 0 end start思考题:假设 sum 为双字无符号整数,在和不溢出的情况下求出 n 的最大值;输出 sum 和n 的值。
include io32.inc .data sum dword 0 .codestart: mov ecx1getsum: mov eaxsum 备份 sum 的值 add sumecx jc next 进位的话跳出循环,此时的 eax 中保存最大 sum而 ecxn1 inc ecx jmp getsumnext: call dispuid 输出 sum call dispcrlf dec ecx mov eaxecx 输出 n call dispuid call dispcrlf exit 0 end start 5. 编写程序,求 0 到 100 间所有偶数和并输出。
要求采用 loop、 while 和 do while 三种不同的循环结构完成。
以下采用 loop 循环 include io32.inc .codestart: mov ecx100 xor eaxeaxagain: add eaxecx dec ecx loop again call dispuid exit 0 end start结果 2550 以下采用 while 循环 include io32.inc .codestart: mov ecx100 xor eaxeaxagain: cmp ecx0 jl done add eaxecx sub ecx2 jmp againdone: call dispuid exit 0 end start结果 2550 以下采用 do while 循环 include io32.inc .codestart: mov ecx100 xor eaxeaxagain: add eaxecx sub ecx2 cmp ecx0 jl done jmp againdone: call dispuid exit 0 end start结果 2550 6. Fibonacci numbers 的定义: f11f21 fn fn-1 fn-2 ngt3 编程输出 Fibonacci numbers 的前 30 项。
include io32.inc .data space byte 0输出空格 f1 dword 1 fn-1 f2 dword 1 fn-2 .codestart: mov ecx30 mov eaxf1 输出 f1 call disp dec ecx mov eaxf2 输出 f2 call disp dec ecxagain: mov eaxf1 add eaxf2 eax fn-1 fn-2 call disp mov ebxf2 mov f1ebx fn-1 fn-2 mov f2eax fn-2 eax loop again jmp donedisp proc push eax 保护寄存器 eax call dispuid mov eaxoffset space call dispmsg pop eax 恢复寄存器 eax retdisp endpdone: exit 0 end start结果:1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 Press any key to continue思考题:在不产生溢出的情况下 n 的最大值是多少? include io32.inc .data space byte 0 输出空格 maxn byte The Maximum n is 0 f1 dword 1 fn-1 f2 dword 1 fn-2 .codestart: xor ecxecx mov eaxf1 输出 f1 call disp inc ecx mov eaxf2 输出 f2 call disp inc ecxagain: mov eaxf1 add eaxf2 eax fn-1 fn-2 jc done 有进位时跳出 call disp mov ebxf2 mov f1ebx fn-1 fn-2 mov f2eax fn-2 eax inc ecx jmp againdisp proc 输出过程 push eax call dispuid mov eaxoffset space call dispmsg pop eax retdisp endpdone: call dispcrlf mov eaxoffset maxn call dispmsg mov eaxecx call dispuid call dispcrlf exit 0 end start运行结果:1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 2971215073The Maximum n is 47Press any key to continue 7. 有一个首地址为 array 的 20 个有符号的双字数组,编程分别求出正数的 和与负数的和并输出。
include io32.inc .data array dword -1 2 4 -8 6-4-9 8 4 0 -49 431-68 1-4 6 5 7 -51 sumofpos dword 保存正数的和 sumofneg dword 保存负数的和 .codestart: mov ecxlengthof array 数组元素个数 xor edxedx 数组下标again: mov eaxarrayedx4 取出下标为 edx 的数组元素 inc edx cmp eax0 jg positive jl negativecontinue: loop again jmp done 所有求和结束positive: add sumofposeax 正数求和 jmp continuenegative: add sumofnegeax 负数求和 jmp continuedone: mov eaxsumofpos call dispsid call dispcrlf mov eaxsumofneg call dispsid call dispcrlf exit 0 end start运行结果:78-194思考题:将求和改为求最大值与最小值。
include io32.inc .data array dword -12 4 -8 6-4-9 8 4 0 -49 431-68 1-4 6 5 7 -51 max dword 最大值 min dword 最小值 .codestart: mov eaxarray0 为 max 和 min 赋初值 mov maxeax mov mineax mov ecxlengthof array 数组元素个数 xor edxedx 数组下标again: mov eaxarrayedx4 取出下标为 edx 的数组元素 inc edx cmp eaxmax jg _max cmp eaxmin jl _mincontinue: loop again jmp done 数组遍历结束_max: mov maxeax 保存可能的最大值 jmp continue_min: mov mineax 保存可能的最小值 jmp continuedone: mov eaxmax call dispsid call dispcrlf mov eaxmin call dispsid call dispcrlf exit 0 end start运行结果:31-68 8. 有一个首地址为 string 的字符串,剔除 string 中所有的空格字符。
请从 字符串最后一个字符开始逐个向前判断、并进行处理。
include io32.inc .data string byte Let us have a try 0 测试字符串 .codestart: mov eaxoffset string 输出字符串 call dispmsg call dispcrlf mov esilengthof string esi 为数组长度 dec esi esi 指向最后一个元素again: mov alstringesi cmp al jz reject 剔除空格 dec esi jl done 循环结束 jmp againreject: mov ecxesi ecx 指向当前找到的空格reject1: 将后续字符依次往前移 mov alstringecx1 mov stringecxal test alal 遇到0 结尾 jz again 完成一个空格的剔除 inc ecx jmp reject1 继续前移后一个字符done: mov eaxoffset string call dispmsg call dispcrlf exit 0 end start运行结果: Let us have a try Letushaveatry 9. 有一个首地址为 string 的字符串,分别统计 string 中空格、英文字母、 数字和其它字符的个数并输出。
include io32.inc .data string byte Let us hava a try. 123Go0 测试字符串 countofspace dword 空格 countofletter dword 英文字母 countofdigit dword 数字 countofother dword 其他 .codestart: xor edxedx 字符数组下标again: mov alstringedx cmp al0 遇到0 结束 jz done cmp al 判断空格 jz space cmp ala 初步判断为小写字母 jge letter_lowercontinue_upper: cmp alA 初步判断为大写字母 jge letter_uppercontinue_digit: cmp al0 初步判断为数字 jge digit0continue_other: 其他字符 inc countofothercontinue: 增加下标号开始下一次循环 inc edx jmp againspace: inc countofspace jmp continueletter_lower: cmp alz 进一步判断是否为小写字母 jle lower jmp continue_upper 继续判断是否大写字母lower: 确定为小写字母 inc countofletter jmp continueletter_upper: cmp alZ 进一步判断是否为大写字母 jle upper jmp continue_digit 继续判断是否为数字upper: 确定为大写字母 inc countofletter jmp continuedigit0: cmp al9 进一步判断是否数字 jle digit9 jmp continue_other 只能是其他字符digit9: inc countofdigit jmp continue依次输出空格数、英文字母数、数字、其他、done: mov eaxcountofspace call dispuid call dispcrlf mov eaxcountofletter call dispuid call dispcrlf mov eaxcountofdigit call dispuid call dispcrlf mov eaxcountofother call dispuid call dispcrlf exit 0 end start运行结.