常见汇编程序源代码示例.docx
《常见汇编程序源代码示例.docx》由会员分享,可在线阅读,更多相关《常见汇编程序源代码示例.docx(33页珍藏版)》请在冰豆网上搜索。
![常见汇编程序源代码示例.docx](https://file1.bdocx.com/fileroot1/2023-1/31/8f3d209e-2f78-4d65-be82-9b2e99d2ca57/8f3d209e-2f78-4d65-be82-9b2e99d2ca571.gif)
常见汇编程序源代码示例
包含14个常见的汇编程序源代码,所有代码在VC6.0中调试通过;
汇编程序釆用《微机原理接口与技术》(钱晓婕)一书中所用的框架。
1.编写程序,计算下面函数的值并输出。
2
2.输入一个年份(调用readuid子程序),判断是否是闰年2
3.输入三个无符号整数(调用readuid子程序),判断并输出这三个数是否能构成
一个三角形的三条边。
若这三个数能构成一个三角形的三条边,输出三角形的形状:
斜三角形、等腰三角形、等边三角形。
3
4.采用无条件和条件转移指令构造while和dowhile循环结构,完成下面的求
和任务并输出sum(sum为双字)。
5
5.编写程序,求0到100间所有偶数和并输出。
要求采用loop、while#dowhile
三种不同的循环结构完成。
6
6.Fibonaccinumbers的定义:
8
fl=l,f2=lzfn=fn-1+fn-2n>=38
编程输出Fibonaccinumbers的前30项。
8
7.有一个首地址为array的20个有符号的双字数组,编程分别求出正数的和与
负数的和并输出。
10
8.有一个首地址为string的字符串,剔除string中所有的空格字符。
请从字符
串最后一个字符开始逐个向前判断、并进行处理。
12
9.有一个首地址为string的字符串,分别统计string中空格、英文字母、数字
和其它字符的个数并输出。
13
10.palindrome(回文)是指正读和反读都一样的数或文本。
例如:
11、121.12321等,编写程序,求10到10000之间所有回文数并输出。
要求每行输出10个数。
15
11.编写程序,求出所有满足勾股定理且边长不大于500的直角三角形。
.17
12.编写一个求n!
的子程序,利用它求1!
+2!
+引+4!
+5!
+6!
+7!
+8!
的和并输出。
22
13.编写一个判断闰年的子程序,利用它求出2010年到2060年之间所有的闰
年并输出。
25
14.编写一个求解双字型有符号数数组元素的平均值子程序,并验证它的正确性。
26
1.编写程序,计算下面函数的值并输出。
2x(x<0)
s=<3x(0<=x<=10)
4xx>10
ineludeio32.inc
.code
start:
callreadsid
empeax,0
jlsmall
empeax,10
jlemid
jmplarge
small:
imuleax,2
jmpdone
mid:
imuleax,3
jmpdone
large:
imuleax,4
jmpdone
done:
calldispsid
exit0
endstart
2・输入一个年份(调用readuid子程序),判断是否是闰年.
ineludeio32.inc
.data
yes_msgbyte'isleap:
13,10,0
no_msgbyte'noleap',13,10,0
.code
start:
callreaduid
movedx,0
movecx,4
divecx
empedx,0
jefirst
jmpsecond
first:
movedx,0
movecx,100
divecx
jneleap
jmpsecond
second:
movedx,0
movecx,400
divecx
jeleap
jmpnoleap
leap:
moveax,offsetyes_msg
calldispmsg
jmpdone
noleap:
moveax,offsetno_msg
calldispmsg
jmpdone
done:
exit0
endstart
3・输入三个无符号整数(调用readuid子程序),判断并输出这三个数是否能构成一个三角形的三条边。
若这三个数能构成一个三角形的三条边,输出三角形的形状:
斜三角形、等腰三角形、等边三角形。
ineludeio32.inc
.data
msg^dengyaobyte'dengyao:
13,10,0;等腰三角形
msg_dengbianbyte'dengbian',13,10,0;等边三角形
msg_zhijiaobyte'zhijiao',13,10,0;直角三角形
msg_xiesanjiaobyte'xiesanjiaoxing:
13J0,0;斜三角形
msg^wrongbyte'wrong:
13,10,0;无法构成三角形
sqrdword0
.code
;在ebx,ecx,edx分别保存三条边的长度
start:
callreaduid;读取第一个数和第二个数到ebx、ecx
movebx,eax
callreaduid
movecx,eaxcmpebx,ecxjggreatjmpnext
great:
xchgebx,ecx
next:
callreaduid
movedx,eaxcmpebx,edxjgthirdsmallcmpecx,edxjgthirdmidjmporder;如果第三个数最小thirdsmall:
moveax,edx
movedx,ecx
movecx,ebx
movebx,eax
jmporder
;如果第三个数在最中间thirdmid:
xchgecx,edx
jmporder
;确保ebx<=ecx
;读取第三个数到edx
;如果第三个数最小
;如果第三个数在最中间
;执行至此三个数(ebx,ecxzedx)已经从小到人排序
order:
moveaxzebx
addeax,ecx
cmpeax,edx;最小的两边之和大于第三边
jlewrong
cmpebx,ecxjeb_equal_ccmpecx,edxjec_equal_djmpgeneralb_equal_c:
cmpecx,edxjedengbianjmpdengyaoc_equal_d:
cmpebx,ecx
;第一条边等于第二条边
;第二条边等于第三条边
;非等腰(等边)三角形
;继续比较,确认是否为等边三角形
;继续比较,确认是否为等边三角形
jedengbian
jmpdengyao
;执行至此,能否构成三角形,以及三角形是否等边等腰已经确定,输出
;判断是否为直角三角形
general:
moveax,ebx
imuleax,eax
movsqr;eax
moveax,ecx
imuleax,eax
addsqr,eax
moveax,edx
imuleax,eax
cmpsqr;eax
jezhijiao
jmpxiesanjiao
xiesanjiao:
moveax,offsetmsg_xiesanjiao
calldispmsg
jmplast
zhijiao:
moveax,offsetmsg_zhijiao
calldispmsg
jmplast
dengbian:
moveax,offsetmsg_dengbian
calldispmsg
jmplast
dengyao:
moveax,offsetmsg_dengyao
calldispmsg
jmplast
wrong:
moveax,offsetmsg_wrong
calldispmsg
jmplast
;输出完毕,运行结束
last:
exit0
endstart
4.采用无条件和条件转移指令构造while和dowhile循环结构,完成下面的求和任务并输出sum(sum为双字)。
ineludeio32.inc
.data
sumdword0
.code
start:
callreaduid
movecx,eax
getsum:
cmpecx,0
jenext
addsum,ecx
dececx
jmpgetsum
next:
moveax,sum
calldispuid
calldispcrlf
exit0
endstart
思考题:
假设sum为双字无符号整数,在和不溢出的情况下求出n的最大值;输出sum和n的值。
ineludeio32.inc
.data
sumdword0
.code
start:
movecx,l
getsum:
moveax,sumaddsum,ecxjcnextincecx
jmpgetsumnext:
;备份sum的值
;进位的话跳出循环,此时的eax中保存最大sum,而ecx=n+l
calldispuidcalldispcrlf
;输出sum
dececx
moveax,ecx
;输出n
calldispuidcalldispcrlfexit0endstart
5.编写程序,
求0到100间所有偶数和并输出。
要求采用loop.While
和dowhile三种不同的循环结构完成。
•以下采用loop循坏
ineludeio32.inc
.code
start:
movecx,100
xoreax,eax
again:
addeax,ecx
dececx
loopagain
calldispuid
exit0
endstart
;结果2550
•以下采用while循环
ineludeio32.inc
.code
start:
movecx,100
xoreax,eax
again:
empecx,0
jldone
addeax,ecx
subecx,2
jmpagain
done:
calldispuid
exit0
endstart
;结果2550
•以下采用dowhile循坏
ineludeio32.inc
.code
start:
movecx,100
xoreax,eax
again:
addeax,ecx
subecx,2
empecx,0
jldonejmpagaindone:
calldispuid
exit0
endstart
;结果2550
6.Fibonaccinumbers的定义:
fl=l,f2=l,fn=fn-1+fn-2n>=3编程输出Fibonaccinumbers的前30项。
ineludeio32.inc
.data
spacebyte':
0;输出空格
fldword1;fn-l
f2dword1;fn-2
.code
start:
;输出fl
;输出f2
movecx,30moveax,flcalldispdececxmoveax,f2calldisp
dececx
again:
moveax,fladdeaxzf2calldispmovebx,f2movfl,ebxmovf2,eaxloopagainjmpdone
;eax=fnJ+fn・2
;fn-l=fn-2
;fn・2=eax
dispproc
pusheax;保护寄存器eax
calldispuid
moveax,offsetspace
calldispmsg
popeax;恢复寄存器eax
ret
dispendp
done:
exit0
endstart
;结果:
;1123581321345589144233377610987159725844181676510946177112;86574636875025121393196418317811514229832040Pressanykeytocontinue
思考题:
在不产生溢出的情况下n的最大值是多少?
ineludeio32.inc
.data
spacebyte1\0
maxnbyte'TheMaximumnis\0fldword1f2dword1
.code
start:
xorecx,ecxmoveax,flcalldispincecxmoveax,f2calldispincecx
again:
moveax,fladdeax,f2jcdonecalldispmovebx,f2movfl,ebxmovf2,eaxincecxjmpagain
dispproc
pusheaxcalldispuid
moveax,offsetspace
calldispmsg
popeax
;输出空格
;fn-l
;fn-2
;输出fl
;输出f2
;eax=fn-1+fn・2;有进位时跳出
;fn-l=fn-2
;fn・2=eax
;输出过程
ret
dispendpdone:
calldispcrlf
moveax,offsetmaxncalldispmsgmoveax,ecxcalldispuidcalldispcrlf
exit0
endstart
;运行结果:
;1123581321345589144233377610987159725844181676510946177112
;86574636875025121393196418317811514229832040134********30935245785702;88792274651493035224157817390881696324598610233415516558014126791429643;3494437701408733113490317018363119032971215073
;TheMaximumnis47
;Pressanykeytocontinue
7.有一个首地址为array的20个有符号的双字数组,编程分别求出正数的和与负数的和并输出。
ineludeio32.inc
.data
arraydword-1,2,4,-8,6,-4,-9,&4,0,
-49,4,31,-6&1,-4,6,5,7,-51
sumofposdword?
sumofnegdword?
.code
start:
movecxjengthofarrayxoredx,edx
;保存正数的和
;保存负数的和
;数组元素个数
;数组下标
;取出下标为edx的数组元素
;所有求和结束
again:
moveax,array[edx*4]incedx
empeax,0
jgpositivejlnegative
continue:
loopagain
jmpdonepositive:
addsumofposzeax;正数求和
jmpcontinue
negative:
addsumofneg,eax;负数求和
jmpcontinue
done:
moveax,sumofpos
calldispsid
calldispcrlf
moveax,sumofneg
calldispsid
calldispcrlf
exit0
endstart
运行结果:
78
-194
思考题:
将求和改为求最大值与最小值。
ineludeio32.inc
.data
arraydword-1Z2Z4,-8,6,-4,-9,&4,0,
-49,4,31,-6&1,-4,6,5,7,-51maxdword?
;最大值
mindword?
;最小值
.code
start:
moveax,array[O]
movmax,eax
movmin,eax
movecxjengthofarrayxoredx,edx
again:
moveax,array[edx*4]incedx
empeax,max
jg.max
;为max和min赋初值
;数组元素个数
;数组下标
;取出下标为edx的数组元素
empeax,minjl_min
continue:
loopagain
_max:
movmax,eaxjmpcontinue_min:
movmin,eaxjmpcontinuedone:
moveax,maxcalldispsidcalldispcrlfmoveax,mincalldispsidcalldispcrlfexit0endstart运行结果:
31
-68
&有一个首地址为string的字符串,剔除string中所有的空格字符。
请从字符串最后一个字符开始逐个向前判断、并进行处理。
ineludeio32.inc
.data
stringbyte'Letushaveatry!
:
0.code
start:
moveax,offsetstring
calldispmsg
calldispcrlf
movesijengthofstring
decesi
again:
moval,string[esi]
empal/'
jzrejectdecesi
jldonejmpagain
;测试字符串
;输出字符串
;esi为数组长度
;esi指向最后一个元素
湯U除空格
;循坏结束
reject:
movecx,esi
rejectl:
movalstringlecx+1]movstring[ecx],altestalzaljzagain
incecx
jmprejectl
done:
moveax,offsetstringcalldispmsgcalldispcrlf
exit0
endstart
;ecx指向当前找到的空格
;将后续字符依次往前移
;遇到\0结尾
;完成一个空格的剔除
;继续前移后一个字符
;运行结果:
;Letushaveatry!
;Letushaveatry!
9.有一个首地址为string的字符串,分别统计string中空格、英文字母、数字和其它字符的个数并输出。
ineludeio32.inc
.data
stringbyte'Letushavaatry.123,Go!
:
0;测试字符串
countofspacedword?
;空格
countofletterdword?
;英文字母
countofdigitdword?
;数字
countofotherdword?
;其他
.codestart:
;字符数组卜•标
xoredx,edx
again:
moval,string[edx]
empal,0jzdoneempal/'jzspace
empal/a*jgeletterjowercontinue_upper:
;遇到\0结束
;判断空格
;初步判断为小写字母
cmpal/A1
jgeletter_uppercontinue_digit:
cmpal/01
jgedigitO
continue_other:
inccountofother
continue:
incedx
jmpagainspace:
inccountofspacejmpcontinue
letterjower:
cmpal/z1jlelowerjmpcontinue_upperlower:
inccountofletterjmpcontinue
letter_upper:
cmpal/Z*jleupperjmpcontinue_digitupper:
inccountofletter
jmpcontinue
digitO:
cmpal/91jledigit9jmpcontinue_otherdigit9:
inccountofdigitjmpcontinue
;依次输出空格数、英文字母数、done:
moveax,countofspace
calldispuid
calldispcrlf
moveax,countofletter
;初步判断为人写字母
;初步判断为数字
;其他字符
;增加下标号,开始下一次循坏
;进一步判断是否为小写字母
;继续判断是否人写字母;确定为小写字母
;进一步判断是否为人写字母
;继续判断是否为数字
;确定为大写字母
;进一步判断是否数字
;只能是其他字符
数字、其他、
calldispuid
calldispcrlf
moveax,countofdigit
calldispuid
calldispcrlf
moveax,countofother
calldispuid
calldispcrlf
exit0
endstart
;运行结果:
;5
;15
;3
;5
10.palindrome(回文)是指正读和反读都一样的数或文本。
例如:
11、121.12321等,编写程序,求10到10000之间所有回文数并输出。
要求每行输出10个数。
提示:
采用div指令把整数分解为单个的数字,并将它们组合成一个新的整数。
ineludeio32.inc
.data
stringbyte0,0,0,0,0
chushudword10backdword?
countdword?
.code
start:
moveax,10
outlp:
;保存转换后的字符串(逆序方式)
;除数10,用于提取个位数字
;备份eax寄存器的值
;计算以输出的总数,用于每10个换行
;eax从10到10000
movecx,0movback,eax
;ecx清零,用于记录字符串实际长度
;备份eax寄存器的值
DigitToString:
;将数字转换成字符串
xoredx,edxdivchushuadddl,30hmovstring[ecx],dlincecx
testeax,