汇编程序大作业云南大学软件学院实验报告指导教师 (签名):实验内容:1.设计一个功能完善的四则运算器:①程序有友好的运行界面;②可以方便的输入输出2,10,16进制数据(要求可以处理4位的数据);③可以方便的对数据进行四则运算;④要求程序能够处理基本的错误信息;⑤程序要求菜单控制;⑥其他程序简介:该程序采用了主程序、子程序、宏相结合的编写方式,通过主程序调用了加、减、乘、除的子程序,在这四个子程序中有分别调用了二、十、十六进制输入输出、进制间转化的子程序。
在数值转化为ASCII码的部分,定义了宏进行调整输出。
程序实现了16位的二进制、4位的十进制、4位的十六进制的加、减、乘、除运算,并可以通过用户的选择将其转化为其他相应的进制。
其中乘法运算的进制转化功能尚不完善,当运算结果超过4位时不能正确的将结果进行输出并进行进制转化。
子过程:addition:加法部分subtraction:减法部分multiplication:乘法部分division:除法部分b_in:二进制输入hd_in:十六进制、十进制输入b_out:二进制输出btod:二进制转化十进制并输出both:二进制转化十六进制并输出宏:btth:将数值转化为ASCII码保存算法说明:二进制计算时,要求输入0和1,从高到低为输入,将先输进的数*2+后输入的数,保存到寄存器中,如 1011则按((1*2+0)*2+1)*2+1十进制计算时,要求输入0-9的整数,从高到低输入,将先输进的数*10+后输进的数,保存到寄存器中,如9817 则按((9*10+8)*10+1)*10+7十进制计算时,要求输入0-F的整数,从高到低输入,将先输进的数*10H+后输进的数,保存到寄存器中,如AD17 则按((A*10H+D)*10H+1)*10H+7通过add、sub、mul、div等汇编指令进行计算。
二进制输出,从高位到低位按位依次保存到dl中,加30h后输出十进制输出,将结果分别除以10000、(上一步余数除)1000、(上一步余数除)100、(上一步余数除)10将商分别加30h后输出,将最后的余数加上30h后输出十六进制输出,从高到低每四位依次输出与9比较,大于9加37h调整,小于9加30h 调整程序流程及实验结果:主程序部分:提示用户进行运算符号的选择,并进行子程序部分的调用四则运算部分流程:四则运算的加、减、乘、除四部分子程序的流程大致相同,此处采用同一个流程图表示,该部分提示用户进行进制选择,跳转的相应位置并进行相应的运算,调用输入输出子程序,对结果进行进制转化二进制输入流程:该部分进行二进制数的输入,通过移位将先输入的数保存到高位,后输入的数保存到低位,将最终结果保存到bx中二进制输出:该部分将运算结果以2进制的形式输出,通过加30h将数值转化为ASCII码输出十、十六进制数的输入该部分实现十及十六进制数的输入,将最终结果保存到bx中;二进制转十进制并输出该部分通过将得到的二进制数求商取余的方式将最高位的取到商中并输出;二进制转十六进制:该部分通过移位的方式将二进制数转化为16进制输出,其中判断了所输出值与9的大小关系,大于9则加37h调整,小于9则加30h调整加法部分:二进制加法,并将结果转化为10进制二进制加法,并将结果转化为16进制:十进制加法,并将结果转化为二进制:十进制加法,并将结果转化为16进制:十六进制加法,并将结果转化为二进制:十六进制加法,并将结果转化为十进制减法部分:二进制减法,并将结果转化为十进制输出:二进制减法并将结果转化为十六进制输出:十进制减法,并将结果转化为二进制输出:十进制减法,并将结果转化为十六进制输出:十六进制减法,并将结果转化为二进制输出:十六进制减法,并将结果转化为10进制输出:乘法部分:二进制乘法,并结果转化为10进制输出:二进制乘法并将结果转化为16进制输出:十进制乘法并将结果转化为二进制输出十进制乘法,并将结果转化为16进制输出:十六进制乘法,并将结果转化为二进制输出:十六进制乘法,并将结果转化为十进制输出:除法部分:二进制除法,并将结果转化为十进制输出:二进制除法,并将结果转化为16进制输出:十进制除法,并将结果转化为二进制输出:十进制除法,并将结果转化为16进制输出:十六进制除法,并将结果转化为二进制输出:十六进制除法,并将结果转化为10进制输出:代码:data segmentnote1 db 'Input a-->add,b-->sub,c-->mul,d--div',0ah,0dh,'$'note2 db 0ah,0dh,'Input a-->binary,b-->decimal,c--hexadecimal',0ah,0dh,'$' note3 db 0ah,0dh,'Input addend' ,0ah,0dh,'$'note4 db 0ah,0dh,'Input another addend', 0ah,0dh,'$'note5 db 0ah,0dh,'The answer is',0ah,0dh,'$'note6 db 0ah,0dh,'Change the answer to a-->decimal,b-->hexadecimal,any other key-->exit',0ah,0dh,'$'note7 db 0ah,0dh,'Change the answer to a-->binary,b-->hexadecimal,any other key-->exit',0ah,0dh,'$'note8 db 0ah,0dh,'Change the answer to a-->binary,b-->decimal,any other key-->exit',0ah,0dh,'$'note9 db 0ah,0dh,'Input minuend', 0ah,0dh,'$'note10 db 0ah,0dh,'Input subtrahend', 0ah,0dh,'$'note11 db 0ah,0dh,'Input multiplicand' ,0ah,0dh,'$'note12 db 0ah,0dh,'Input another multiplicand' ,0ah,0dh,'$'note13 db 0ah,0dh,'Input dividend', 0ah,0dh,'$'note14 db 0ah,0dh,'Input divisor', 0ah,0dh,'$'num dw 0num1 dw 0num2 dw 0sig db ?data endscode segmentassume ds:data,cs:codestart:mov ax,datamov ds,axlea dx,note1mov ah,09hint 21hm1:mov ah,01hint 21hcmp al,'a'je adcmp al,'b'je subtcmp al,'c'je multcmp al,'d'je divijmp m1ad:call additionjmp over计算机组成原理与汇编语言程序设计(2013)subt:call subtractionjmp overmult:call multiplicationjmp overdivi:call divisionover:mov ah,01hint 21hmov ah,4chint 21haddition proclea dx,note2mov ah,09hint 21hmov ah,01hint 21hmov sig,alcmp al,'a'je b_addcmp al,'b'je d_addcmp al,'c'je h_addjmp a_endb_add:lea dx,note3mov ah,09hint 21hcall b_inmov num,bxlea dx,note4mov ah,09hint 21hcall b_inlea dx,note5mov ah,09hint 21hmov ax,numadd bx,axcall b_out;此处存在bug,若产生进位不能进行输出,需提示用户只进行15位的运算lea dx,note6mov ah,09hint 21hmov ah,01h计算机组成原理与汇编语言程序设计(2013)int 21hcmp al,'a'je btdcmp al,'b'je bthjmp a_endbtd:lea dx,note5mov ah,09hint 21hcall btodjmp a_endbth:lea dx,note5mov ah,09hint 21hcall btohjmp a_endd_add:lea dx,note3mov ah,09hint 21hcall hd_inmov num,bxlea dx,note4mov ah,09hint 21hcall hd_inmov ax,numadd bx,axpush bxlea dx,note5mov ah,09hint 21hcall btodlea dx,note7mov ah,09hint 21hmov ah,01int 21hcmp al,'a'je dtbcmp al,'b'je dthjmp a_enddtb:lea dx,note5mov ah,09h计算机组成原理与汇编语言程序设计(2013)int 21hpop bxcall b_outjmp a_enddth:lea dx,note5mov ah,09hint 21hpop bxcall btohjmp a_endh_add:lea dx,note3mov ah,09hint 21hcall hd_inmov num,bxlea dx,note4mov ah,09hint 21hcall hd_inmov ax,numadd bx,axlea dx,note5mov ah,09hint 21hcall btoh;此处存在bug,当产生进位是,不能对进位进行输出lea dx,note8mov ah,09hint 21hmov ah,01hint 21hcmp al,'a'je htbcmp al,'b'je htdjmp a_endhtb:lea dx,note5mov ah,09hint 21hcall b_outjmp a_endhtd:lea dx,note5mov ah,09hint 21hcall btod计算机组成原理与汇编语言程序设计(2013)jmp a_enda_end:retaddition endpsubtraction proclea dx,note2mov ah,09hint 21hmov ah,01hint 21hmov sig,alcmp al,'a'je b_subcmp al,'b'je d_subcmp al,'c'je h_subjmp s_endb_sub:lea dx,note9mov ah,09hint 21hcall b_inmov num,bxlea dx,note10mov ah,09hint 21hcall b_inmov ax,numxchg ax,bxsub bx,axlea dx,note5mov ah,09hint 21hcall b_outlea dx,note6mov ah,09hint 21hmov ah,01hint 21hcmp al,'a'je btdcmp al,'b'je bthjmp s_end计算机组成原理与汇编语言程序设计(2013)d_sub:lea dx,note9mov ah,09hint 21hcall hd_inmov num,bxlea dx,note10mov ah,09hint 21hcall hd_inmov ax,numxchg ax,bxsub bx,axpush bxlea dx,note5mov ah,09hint 21hcall btodlea dx,note7mov ah,09hint 21hmov ah,01hint 21hcmp al,'a'je dtbcmp al,'b'je dthjmp s_endh_sub:lea dx,note9mov ah,09hint 21hcall hd_inmov num,bxlea dx,note10mov ah,09hint 21hcall hd_inmov ax,numxchg ax,bxsub bx,axlea dx,note5mov ah,09hint 21hcall btohlea dx,note8计算机组成原理与汇编语言程序设计(2013)mov ah,09hint 21hmov ah,01hint 21hcmp al,'a'je htbcmp al,'b'je htdjmp s_ends_end:retsubtraction endpmultiplication proclea dx,note2mov ah,09hint 21hmov ah,01hint 21hmov sig,alcmp al,'a'je b_mulcmp al,'b'je d_mulcmp al,'c'je h_mulb_mul:lea dx,note11mov ah,09hint 21hcall b_inmov num,bxlea dx,note12mov ah,09hint 21hcall b_inmov ax,nummul bxmov num1,dxmov num2,axlea dx,note5mov ah,09hint 21hmov num1,dxmov bx,dxcall b_out计算机组成原理与汇编语言程序设计(2013)mov ax,num2mov bx,axcall b_outlea dx,note6mov ah,09hint 21hmov ah,01hint 21hcmp al,'a'je m_btdcmp al,'b'je m_bthjmp m_endm_btd:lea dx,note5;此处错误,尚需修改mov ah,09hint 21hmov dx,num1mov bx,dxcall btodmov ax,num2mov bx,axcall btodjmp m_endm_bth:lea dx,note5mov ah,09hint 21hmov dx,num1mov bx,dxcall btohmov ax,num2mov bx,axcall btohd_mul:lea dx,note11mov ah,09hint 21hcall hd_inmov num,bxlea dx,note12mov ah,09hint 21hcall hd_inmov ax,nummul bxpush ax计算机组成原理与汇编语言程序设计(2013)lea dx,note5mov ah,09hint 21hpop axpush axmov bx,axcall btodlea dx,note7mov ah,09hint 21hmov ah,01hint 21hcmp al,'a'je m_dtbcmp al,'b'je m_dthm_dtb:lea dx,note5mov ah,09hint 21hpop axmov bx,axcall b_outjmp m_endm_dth:lea dx,note5mov ah,09hint 21hpop axmov bx,axcall btohh_mul:lea dx,note11mov ah,09hint 21hcall hd_inmov num,bxlea dx,note12mov ah,09hint 21hcall hd_inmov ax,nummul bxpush axlea dx,note5mov ah,09hint 21h计算机组成原理与汇编语言程序设计(2013)pop axpush axmov bx,axcall btohlea dx,note8mov ah,09hint 21hmov ah,01hint 21hcmp al,'a'je m_htbcmp al,'b'je m_htdm_htb:lea dx,note5mov ah,09hint 21hpop axmov bx,axcall b_outjmp m_endm_htd:lea dx,note5mov ah,09hint 21hpop axmov bx,axcall btodm_end:retmultiplication endpdivision proclea dx,note2mov ah,09hint 21hmov ah,01hint 21hmov sig,alcmp al,'a'je b_divcmp al,'b'je d_divcmp al,'c'je h_divjmp d_endb_div:lea dx,note13计算机组成原理与汇编语言程序设计(2013)mov ah,09hint 21hcall b_inmov num,bxlea dx,note14mov ah,09hint 21hcall b_inmov ax,numcwdxor dx,dxdiv bxmov num1,axmov num2,dxlea dx,note5mov ah,09hint 21hmov bx,num1call b_outmov cx,6l:mov dl,'.'mov ah,02hint 21hloop lmov bx,num2call b_outlea dx,note6mov ah,09hint 21hmov ah,01hint 21hcmp al,'a'je d_btdcmp al,'b'je d_bthjmp d_endd_btd:lea dx,note5mov ah,09hint 21hmov bx,num1call btodmov cx,6l4:mov dl,'.'mov ah,02hloop l4mov bx,num2 call btodjmp d_endd_bth:lea dx,note5 mov ah,09hint 21hmov bx,num1 call btohmov cx,6l5:mov dl,'.'mov ah,02hint 21hloop l5mov bx,num2 call btohjmp d_endd_div:lea dx,note13 mov ah,09hint 21hcall hd_inmov num,bxlea dx,note14 mov ah,09hint 21hcall hd_inmov ax,numcwdxor dx,dxdiv bxmov num1,ax mov num2,dx lea dx,note5 mov ah,09hint 21hmov bx,num1 call btodmov cx,6l2:mov dl,'.'mov ah,02hint 21hloop l2mov bx,num2lea dx,note7 mov ah,09hint 21hmov ah,01hint 21hcmp al,'a'je d_dtbcmp al,'b'je d_dthjmp d_endd_dtb:lea dx,note5 mov ah,09hint 21hmov bx,num1 call b_outmov cx,6l6:mov dl,'.'mov ah,02hint 21hloop l6mov bx,num2 call b_outjmp d_endd_dth:lea dx,note5 mov ah,09hint 21hmov bx,num1 call btohmov cx,6l7:mov dl,'.'mov ah,02hint 21hloop l7mov bx,num2 call btohjmp d_endh_div:lea dx,note13 mov ah,09hint 21hcall hd_inmov num,bxlea dx,note14 mov ah,09hcall hd_in mov ax,num cwdxor dx,dxdiv bxmov num1,ax mov num2,dx lea dx,note5 mov ah,09h int 21hmov bx,num1 call btohmov cx,6l3:mov dl,'.' mov ah,02h int 21hloop l3mov bx,num2 call btohlea dx,note8 mov ah,09h int 21hmov ah,01h int 21hcmp al,'a' je d_htbcmp al,'b' je d_htdjmp d_endd_htb:lea dx,note5 mov ah,09h int 21hmov bx,num1 call b_out mov cx,6l8:mov dl,'.' mov ah,02h int 21hloop l8mov bx,num2 call b_out jmp d_endd_htd:lea dx,note5计算机组成原理与汇编语言程序设计(2013)mov ah,09hint 21hmov bx,num1call btodmov cx,6l9:mov dl,'.'mov ah,02hint 21hloop l9mov bx,num2call btodjmp d_endd_end:retdivision endpb_in procxor dx,dxmov bx,2mov cx,16bin2:mov ah,1int 21hxor ah,ahsub al,30hadd dx,axmov ax,dxcmp cx,1je bin1mul bxbin1:mov dx,axloop bin2mov bx,dxretb_in endpb_out procmov ch,8mov cl,1mov dh,bhbot1:rol dx,cland dl,01hadd dl,30hmov ah,02hint 21hdec chja bot1mov ch,8mov dh,bl bot2:rol dx,cland dl,01h add dl,30h mov ah,02h int 21hdec chcmp ch,0ja bot2retb_out endpbtth macroand dx,0div bxmov cx,dxadd al,30h mov dl,almov ah,02h int 21hmov ax,cx endmbtod procmov ax,bxmov bx,10000 btthmov bx,1000 btthmov bx,100 btthmov bx,10btthadd al,30h mov dl,almov ah,02h int 21hretbtod endpbtoh procxor dx,dxmov cl,4mov dh,bh bth1:rol dx,cl cmp dl,9ja bth3add dl,30h jmp bth4 bth3:add dl,37h bth4:mov ah,2int 21hxor dl,dl dec chcmp ch,0ja bth1mov dh,bl mov ch,2mov cl,4 bth2:rol dx,cl cmp dl,9ja bth5add dl,30h jmp bth6 bth5:add dl,37h bth6:mov ah,2int 21hxor dl,dl dec chcmp ch,0ja bth2retbtoh endphd_in procxor dx,dx cmp sig,'c' jne din3mov bx,16 jmp din4 din3:mov bx,10 din4:mov cx,4 din1:mov ah,01h int 21hxor ah,ahja hin1 sub al,30h jmp hin2 hin1:sub al,37h hin2:add dx,ax mov ax,dx cmp cx,1 je din2mul bxdin2:mov dx,ax loop din1 mov bx,dx rethd_in endpcode ends end start。