文章目录
- 以2进制输入,2进制输出(无符号)
- 以2进制输入,2进制输出(带符号)
- 以8进制输入,8进制输出
- 以10进制输入,10进制输出
- 以16进制输入,16进制输出
仅供参考
X、Y的输入可以是任何进制。
以2进制输入,2进制输出(无符号)
由于X、Y都是16位的数,X+Y会超出16位,用16位+16位=(dx,ax)32位存储相加的结果。
输入:
由于寄存器数量的限制,我们把输入的X、Y分别放入数据段X和Y,相加的结果(dx,ax)由低低高高,ax放入数据段W,dx放入数据段W+2。
输出:
先输出数据段的W+2,再输出W,总共32位二进制数。
data segmentstring1 db "please input x:",0ah,0dh,'$'string2 db 0ah,0dh,"please input y:",0ah,0dh,'$'string3 db "the result of x+y:",0ah,0dh,'$'X dw ?Y dw ?W dw ?,?
data ends
stack segment stackdw 100 dup (?)top label word
stack ends
code segmentassume cs:code,ds:data,ss:stack
main proc farmov ax,datamov ds,axmov ax,stackmov ss,axlea sp,topmov bx,0 ;存放输入的二进制数xmov cx,0 ;统计输入的合法字符数;显示提示信息"please input x:"lea dx,string1mov ah,9int 21hL1: mov ah,7int 21hcmp al,0dh ;回车,跳转到L2输入yje L2cmp al,30hjb L1cmp al,31hja L1mov dl,al ;合法输入,回显mov ah,2int 21hinc cx ;合法字符+1rol bx,1 ;bx左移1位ror dl,1 ;输入的字符右移1位adc bx,0 ;带进位加法cmp cx,16 ;判断输入的合法字符数,cx=16跳转到L2输入yje L2jmp L1 ;否则继续输入L2: cmp cx,0 ;判断x输入的合法字符数,如果cx=0,跳回继续输入je L1mov X,bx ;把输入的x存储到数据段的Xmov bx,0 ;bx清零mov cx,0 ;cx清零;显示提示信息"please input y:"lea dx,string2mov ah,9int 21hL3: mov ah,7int 21hcmp al,0dhje L4cmp al,30hjb L1cmp al,31hja L1mov dl,almov ah,2int 21hinc cxrol bx,1ror dl,1adc bx,0cmp cx,16je L4jmp L3L4: cmp cx,0je L3mov Y,bx ;把输入的y存储到数据段的Ymov dl,0ah ;换行mov ah,2int 21hmov dl,0dh ;回车mov ah,2int 21hmov ax,X ;X+Ymov dx,0add ax,Yadc dx,0 ;带进位加法mov W,ax ;把相加的结果ax存储到数据段的Wmov W+2,dx ;把相加的结果dx存储到数据段的W+2;显示提示信息"the result of x+y:"lea dx,string3mov ah,9int 21hmov bx,W+2 ;先输出W+2的16位二进制数mov cx,16
L5: mov dl,30hrol bx,1adc dl,0mov ah,2int 21hloop L5mov dl,' 'mov ah,2int 21hmov bx,W ;先输出W的16位二进制数mov cx,16
L6: mov dl,30h rol bx,1adc dl,0mov ah,2int 21hloop L6mov ah,4chint 21h
main endp
code endsend main
以2进制输入,2进制输出(带符号)
在X+Y的时候,使用符号拓展cwd将ax拓展成32位(dx,ax)
类型转换指令:
CBW
AL拓展成AX,AL的最高位为0,(AH)=00h;AL的最高位为1,(AH)=0ffh;
CWD
AX拓展成(DX,AX),AX的最高位为0,(DX)=00h;AH的最高位为1,(DX)=0ffh;
x=-3;y=6
x=-1;y=-1
由于输入的时候默认bx=0,不足16位以0补全,即只有输入16位二进制数才能改变符号位,否则默认为正数:
data segmentstring1 db "please input x:",0ah,0dh,'$'string2 db 0ah,0dh,"please input y:",0ah,0dh,'$'string3 db "the result of x+y:",0ah,0dh,'$'X dw ?Y dw ?W dw ?,?
data ends
stack segment stackdw 100 dup (?)top label word
stack ends
code segmentassume cs:code,ds:data,ss:stack
main proc farmov ax,datamov ds,axmov ax,stackmov ss,axlea sp,topmov bx,0 ;存放输入的二进制数xmov cx,0 ;统计输入的合法字符数mov bp,0 ;符号位,默认为0;显示提示信息"please input x:"lea dx,string1mov ah,9int 21hL1: mov ah,7int 21hcmp al,0dh ;回车,跳转到L2输入yje L2cmp al,30hjb L1cmp al,31hja L1mov dl,al ;合法输入,回显mov ah,2int 21hinc cx ;合法字符+1rol bx,1 ;bx左移1位ror dl,1 ;输入的字符右移1位adc bx,0 ;带进位加法cmp cx,16 ;判断输入的合法字符数,cx=16跳转到L2输入yje L2jmp L1 ;否则继续输入L2: cmp cx,0 ;判断x输入的合法字符数,如果cx=0,跳回继续输入je L1mov X,bx ;把输入的x存储到数据段的Xmov bx,0 ;bx清零mov cx,0 ;cx清零;显示提示信息"please input y:"lea dx,string2mov ah,9int 21hL3: mov ah,7int 21hcmp al,0dhje L4cmp al,30hjb L1cmp al,31hja L1mov dl,almov ah,2int 21hinc cxrol bx,1ror dl,1adc bx,0cmp cx,16je L4jmp L3L4: cmp cx,0je L3mov Y,bx ;把输入的y存储到数据段的Ymov dl,0ah ;换行mov ah,2int 21hmov dl,0dh ;回车mov ah,2int 21h;带符号数X+Y;把X拓展成带符号的32位二进制数mov ax,Xcwdmov cx,axmov bx,dx;把Y拓展成带符号的32位二进制数mov ax,Ycwdadd ax,cxadc dx,bx ;带进位加法mov W,ax ;把相加的结果ax存储到数据段的Wmov W+2,dx ;把相加的结果dx存储到数据段的W+2;显示提示信息"the result of x+y:"lea dx,string3mov ah,9int 21hmov bx,W+2 ;先输出W+2的16位二进制数mov cx,16
L5: mov dl,30hrol bx,1adc dl,0mov ah,2int 21hloop L5mov dl,' 'mov ah,2int 21hmov bx,W ;先输出W的16位二进制数mov cx,16
L6: mov dl,30h rol bx,1adc dl,0mov ah,2int 21hloop L6mov ah,4chint 21h
main endp
code endsend main
以8进制输入,8进制输出
还没有解决有进位32位二进制以8进制输出
2024/4/24 22:08已解决
data segmentstring1 db "please iput x:",0dh,0ah,'$'string2 db 0dh,0ah,"please iput y:",0dh,0ah,'$'string3 db 0dh,0ah,"the result of x+y:",0dh,0ah,'$'string4 db 0dh,0ah,"overflow!",0dh,0ah,'$'X dw ?Y dw ?W dw ?,?
data ends
stack segment stackdw 100 dup (?)top label word
stack ends
code segmentassume cs:code,ds:data,ss:stack
main proc farmov ax,datamov ds,axmov ax,stackmov ss,axlea sp,topstarting: mov bx,0mov si,8mov cx,0lea dx,string1mov ah,9int 21h
L1: mov ah,7int 21hcmp al,0dhje L2cmp al,30hjb L1cmp al,37hja L1mov dl,almov ah,2int 21hinc cxand dx,7mov ax,dxxchg ax,bx mul si jc overflow add bx,ax jc overflowcmp cx,6je L2jmp L1L2:cmp cx,0je L1mov X,bxmov bx,0mov cx,0lea dx,string2mov ah,9int 21hL3: mov ah,7int 21hcmp al,0dhje L4cmp al,30hjb L1cmp al,37hja L1mov dl,almov ah,2int 21hinc cxand dx,7mov ax,dxxchg ax,bx mul si jc overflow add bx,ax jc overflowcmp cx,6je L4jmp L3L4:cmp cx,0je L3mov Y,bxmov ax,Xmov dx,0add ax,Yjc printc ;x+y超出了16位,跳转到有进位的输出adc dx,0mov W,axmov W+2,dxlea dx,string3mov ah,9int 21h;W+2为0,可以不输出(懒得删)mov bx,W+2 mov dl,30hrol bx,1adc dl,0mov ah,2int 21hmov cx,5
L5: push cxmov cl,3rol bx,clmov dl,bland dl,7add dl,30hmov ah,2int 21hpop cxloop L5mov dl,' 'mov ah,2int 21hmov bx,Wmov dl,30hrol bx,1adc dl,0mov ah,2int 21hmov cx,5
L6: push cxmov cl,3rol bx,clmov dl,bland dl,7add dl,30hmov ah,2int 21hpop cxloop L6jcxz exit;x+y超出了16位,即W+2不等于0,要把W的最高位移到W+2,一起输出3位二进制数
;由于最大为177777(8)+177777(8),dx=1h
;把W+2左移1位加上W的最高位
;W+2以6位8进制进行输出
;W把最高位移出去再以5位8进制进行输出
printc:adc dx,0mov W,axmov W+2,dxlea dx,string3mov ah,9int 21hmov ax,W mov bx,W+2rol bx,1rol ax,1adc bx,0;16位二进制以6位8进制输出,先输出最高位 mov dl,30hrol bx,1adc dl,0mov ah,2int 21h;再输出剩下的5位8进制mov cx,5
L7: push cxmov cl,3rol bx,clmov dl,bland dl,7add dl,30hmov ah,2int 21hpop cxloop L7mov dl,' 'mov ah,2int 21h;把最高位移走mov bx,Wmov dl,30hrol bx,1;再输出剩下5位8进制 mov cx,5
L8: push cxmov cl,3rol bx,clmov dl,bland dl,7add dl,30hmov ah,2int 21hpop cxloop L8jcxz exitoverflow: lea dx,string4mov ah,9int 21hjmp startingexit: mov ah,4chint 21h
main endp
code endsend main
以10进制输入,10进制输出
data segmentstring1 db "please iput x:",0dh,0ah,'$'string2 db 0dh,0ah,"please iput y:",0dh,0ah,'$'string3 db 0dh,0ah,"the result of x+y:",0dh,0ah,'$'string4 db 0dh,0ah,"overflow!",0dh,0ah,'$'X dw ?Y dw ?W dw ?,?
data ends
stack segment stackdw 100 dup (?)top label word
stack ends
code segmentassume cs:code,ss:stack,ds:data
main proc farmov ax,datamov ds,axmov ax,stackmov ss,axlea sp,topstarting: mov bx,0mov cx,0mov si,10
;输入x lea dx,string1mov ah,9int 21h
L1: mov ah,7int 21hcmp al,0dhje L2cmp al,30hjb L1cmp al,39hja L1mov dl,almov ah,2int 21hinc cxand dx,0fhmov ax,dxxchg ax,bxmul sijc overflowadd bx,axjc overflowjmp L1L2:cmp cx,0je L1mov X,bxmov bx,0mov cx,0;输入y lea dx,string2mov ah,9int 21h
L3:mov ah,7int 21hcmp al,0dhje L4cmp al,30hjb L3cmp al,39hja L3mov dl,almov ah,2int 21hinc cxand dx,0fhmov ax,dxxchg ax,bxmul sijc overflowadd bx,axjc overflowjmp L3L4:cmp cx,0je L3mov Y,bx;计算X+Ymov ax,Xmov dx,0add ax,Yadc dx,0mov W,axmov W+2,dx;以十进制输出X+Ylea dx,string3mov ah,9int 21hmov ax,Wmov dx,W+2mov cx,0
L5: div sipush dxinc cxcmp ax,0je L6mov dx,0jmp L5L6:pop dxadd dl,30hmov ah,2int 21hloop L6jcxz exitoverflow:lea dx,string4mov ah,9int 21hjmp starting exit:mov ah,4chint 21hmain endp
code endsend main
以16进制输入,16进制输出
data segmentstring1 db "please iput x:",0dh,0ah,'$'string2 db 0dh,0ah,"please iput y:",0dh,0ah,'$'string3 db 0dh,0ah,"the result of x+y:",0dh,0ah,'$'string4 db 0dh,0ah,"overflow!",0dh,0ah,'$'X dw ?Y dw ?W dw ?,?
data ends
stack segment stackdw 100 dup (?)top label word
stack ends
code segmentassume cs:code,ss:stack,ds:data
main proc farmov ax,datamov ds,axmov ax,stackmov ss,axlea sp,top;输入xmov bx,0mov cx,0lea dx,string1mov ah,9int 21h
L1: mov ah,7int 21hcmp al,0dhje L5cmp al,30hjb L1cmp al,39hja L2mov dl,almov ah,2int 21hinc cxsub dl,30hjmp L4L2:cmp al,41hjb L1cmp al,46hja L3mov dl,almov ah,2int 21hinc cxsub dl,37hjmp L4L3:cmp al,61hjb L1cmp al,66hja L1mov dl,almov ah,2int 21hinc cxsub dl,57h L4: and dx,0fhpush cxmov cl,4shl bx,cladd bx,dxpop cxcmp cx,4je L5jmp L1L5:cmp cx,0je L1mov X,bx;输入y lea dx,string2mov ah,9int 21hmov bx,0mov cx,0L6:mov ah,7int 21hcmp al,0dhje L10cmp al,30h jb L6cmp al,39hja L7mov dl,almov ah,2int 21hinc cxsub dl,30hjmp L9L7:cmp al,41hjb L6cmp al,46hja L8mov dl,almov ah,2int 21hinc cxsub dl,37hjmp L9L8:cmp al,61hjb L6cmp al,66hja L6mov dl,almov ah,2int 21hinc cxsub dl,57hL9:and dx,0fhpush cxmov cl,4shl bx,cladd bx,dxpop cxcmp cx,4je L10jmp L6L10:cmp cx,0je L6mov Y,bx;计算x+y mov ax,Xmov dx,0add ax,Yadc dx,0mov W,axmov W+2,dx;以16进制输出x+y lea dx,string3mov ah,9int 21hmov bx,W+2cmp bx,0 ;如果W+2是0,不输出je L11mov dl,bladd dl,30hmov ah,2int 21h
L11: mov bx,Wmov cx,4
L12: push cxmov cl,4rol bx,clmov dl,bland dl,0fhcmp dl,9ja L13add dl,30hjmp L14L13:add dl,37hL14:mov ah,2int 21hpop cxloop L12exit:mov ah,4chint 21hmain endp
code endsend main