资源描述:
《汇编计算数的阶乘-可计算1000万内的数的阶乘.docx》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、本程序作者cabbage功能:计算1亿以内数的阶乘E-mail:@qq.com;**********************求阶乘程序,十进制输入一个数,十进制输出其结果*********************STROUTMACRO;宏,DOS功能调用,设置字符串输出入口参数MOVAH,09HINT21HENDMCHAR_INMACRO;宏,DOS功能调用,设置字符输入入口参数MOVAH,01HINT21HENDMDATASSEGMENTPUBLICDATA1DB10HDUP(0)LEN1DW?LEN2DW?CONST1DW30CONST2DW40INDEXDB10HDUP
2、(0)STR0DB'********************FACTORIAL**********************$'STR1DB0AH,0DH,'Pleaseinputthenum:$'STR2DB0AH,0DH,'Error!$'STR3DB0AH,0DH,'Theresultis:$'STR4DB0AH,0DH,'****************************************************$'STR5DB0AH,0DH,'Thankyouforusing!$'DATA3DB0FF24HDUP(0)DATASENDSDATAS1SEGM
3、ENTPUBLICDB0FFFFHDUP(0)DATAS1ENDSDATAS2SEGMENTPUBLICDATA2DB0FFFFHDUP(0)DATAS2ENDSDATAS3SEGMENTPUBLICDB0FFFFHDUP(0)DATAS3ENDSSTACKSSEGMENTSTACKDW2000HDUP(?)TOPLABELWORDSTACKSENDSCODES1SEGMENTASSUMECS:CODES1,DS:DATAS,SS:STACKS,ES:DATAS2START:MOVAX,DATASMOVDS,AXMOVAX,STACKSMOVSS,AXMOVAX,DATAS2
4、MOVES,AXMOVSP,OFFSETTOPMOVLEN1,OFFSETDATA2MOVLEN2,OFFSETDATA3MOVDX,OFFSETSTR0STROUTs1:CALLZERO;清零DATA1,DATA2,DATA3,INDEX,为保证运行结果不受前次影响XORAX,AXXORBX,BXXORCX,CXXORDX,DX;清零AX,BX,CX,DX等寄存器,为保证运行结果不受前次影响MOVDX,OFFSETSTR1STROUTXORSI,SIdo1:CHAR_INCMPAL,0DHJZd1CMPAL,'0'JBerrorCMPAL,'9'JAerrorSUBAL,'
5、0'MOVDS:BYTEPTR[SI],AL;输入原始数据,按位输入,高位先输INCSIJMPdo1d1:CMPSI,0JZexitMOVCX,SIXORDI,DIdo2:MOVAL,DS:BYTEPTR[SI-1]MOVES:BYTEPTRDATA2[DI],AL;先给DATA2中赋值,使其等于输入的数据INCDIDECSILOOPdo2MOVCX,DIXORSI,SIXORDI,DIdo3:MOVAL,ES:BYTEPTRDATA2[DI]MOVDS:BYTEPTR[SI],ALINCDI;再将DATA1中的数据实现,高位存放在地址较大的存储单元INCSILOOPdo3s
6、2:XORAX,AXCALLFARPTRFAC;调用阶乘计算函数CMPSI,1;当还没有计算到1时,再次进入阶乘函数JNZs2MOVAL,DS:BYTEPTR[SI-1]CMPAL,1JZs3JMPs2s3:CALLFARPTRRESULT;输出结果JMPs1error:MOVDX,OFFSETSTR2STROUTJMPs1exit:MOVDX,OFFSETSTR5STROUTMOVAH,4CHINT21HCODES1ENDS;*********************************************************CODES2SEGMENTASSUM
7、ECS:CODES2,DS:DATAS,SS:STACKS,ES:DATAS2;递归子过程,计算阶乘,没当递归次数大于等于5000时,返回再次进入,降低栈对递归的限制FACPROCFARINCAXPUSHAXdo6:MOVAL,ES:BYTEPTRDATA2[DI-1]DECDICMPAL,0JZdo6INCDICMPDI,CONST2JBs4CALLFARPTRDZEROs4:CMPSI,1JNZs5MOVAL,DS:BYTEPTR[SI-1]CMPAL,1JZs9JMPs6s5:MOVCX,SIDE