文章目录
- 以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 segment
string1 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 stack
dw 100 dup (?)
top label word
stack ends
code segment
assume cs:code,ds:data,ss:stack
main proc far
mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax
lea sp,top
mov bx,0 ;存放输入的二进制数x
mov cx,0 ;统计输入的合法字符数
;显示提示信息"please input x:"
lea dx,string1
mov ah,9
int 21h
L1:
mov ah,7
int 21h
cmp al,0dh ;回车,跳转到L2输入y
je L2
cmp al,30h
jb L1
cmp al,31h
ja L1
mov dl,al ;合法输入,回显
mov ah,2
int 21h
inc cx ;合法字符+1
rol bx,1 ;bx左移1位
ror dl,1 ;输入的字符右移1位
adc bx,0 ;带进位加法
cmp cx,16 ;判断输入的合法字符数,cx=16跳转到L2输入y
je L2
jmp L1 ;否则继续输入
L2:
cmp cx,0 ;判断x输入的合法字符数,如果cx=0,跳回继续输入
je L1
mov X,bx ;把输入的x存储到数据段的X
mov bx,0 ;bx清零
mov cx,0 ;cx清零
;显示提示信息"please input y:"
lea dx,string2
mov ah,9
int 21h
L3:
mov ah,7
int 21h
cmp al,0dh
je L4
cmp al,30h
jb L1
cmp al,31h
ja L1
mov dl,al
mov ah,2
int 21h
inc cx
rol bx,1
ror dl,1
adc bx,0
cmp cx,16
je L4
jmp L3
L4:
cmp cx,0
je L3
mov Y,bx ;把输入的y存储到数据段的Y
mov dl,0ah ;换行
mov ah,2
int 21h
mov dl,0dh ;回车
mov ah,2
int 21h
mov ax,X ;X+Y
mov dx,0
add ax,Y
adc dx,0 ;带进位加法
mov W,ax ;把相加的结果ax存储到数据段的W
mov W+2,dx ;把相加的结果dx存储到数据段的W+2
;显示提示信息"the result of x+y:"
lea dx,string3
mov ah,9
int 21h
mov bx,W+2 ;先输出W+2的16位二进制数
mov cx,16
L5:
mov dl,30h
rol bx,1
adc dl,0
mov ah,2
int 21h
loop L5
mov dl,' '
mov ah,2
int 21h
mov bx,W ;先输出W的16位二进制数
mov cx,16
L6:
mov dl,30h
rol bx,1
adc dl,0
mov ah,2
int 21h
loop L6
mov ah,4ch
int 21h
main endp
code ends
end 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 segment
string1 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 stack
dw 100 dup (?)
top label word
stack ends
code segment
assume cs:code,ds:data,ss:stack
main proc far
mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax
lea sp,top
mov bx,0 ;存放输入的二进制数x
mov cx,0 ;统计输入的合法字符数
mov bp,0 ;符号位,默认为0
;显示提示信息"please input x:"
lea dx,string1
mov ah,9
int 21h
L1:
mov ah,7
int 21h
cmp al,0dh ;回车,跳转到L2输入y
je L2
cmp al,30h
jb L1
cmp al,31h
ja L1
mov dl,al ;合法输入,回显
mov ah,2
int 21h
inc cx ;合法字符+1
rol bx,1 ;bx左移1位
ror dl,1 ;输入的字符右移1位
adc bx,0 ;带进位加法
cmp cx,16 ;判断输入的合法字符数,cx=16跳转到L2输入y
je L2
jmp L1 ;否则继续输入
L2:
cmp cx,0 ;判断x输入的合法字符数,如果cx=0,跳回继续输入
je L1
mov X,bx ;把输入的x存储到数据段的X
mov bx,0 ;bx清零
mov cx,0 ;cx清零
;显示提示信息"please input y:"
lea dx,string2
mov ah,9
int 21h
L3:
mov ah,7
int 21h
cmp al,0dh
je L4
cmp al,30h
jb L1
cmp al,31h
ja L1
mov dl,al
mov ah,2
int 21h
inc cx
rol bx,1
ror dl,1
adc bx,0
cmp cx,16
je L4
jmp L3
L4:
cmp cx,0
je L3
mov Y,bx ;把输入的y存储到数据段的Y
mov dl,0ah ;换行
mov ah,2
int 21h
mov dl,0dh ;回车
mov ah,2
int 21h
;带符号数X+Y
;把X拓展成带符号的32位二进制数
mov ax,X
cwd
mov cx,ax
mov bx,dx
;把Y拓展成带符号的32位二进制数
mov ax,Y
cwd
add ax,cx
adc dx,bx ;带进位加法
mov W,ax ;把相加的结果ax存储到数据段的W
mov W+2,dx ;把相加的结果dx存储到数据段的W+2
;显示提示信息"the result of x+y:"
lea dx,string3
mov ah,9
int 21h
mov bx,W+2 ;先输出W+2的16位二进制数
mov cx,16
L5:
mov dl,30h
rol bx,1
adc dl,0
mov ah,2
int 21h
loop L5
mov dl,' '
mov ah,2
int 21h
mov bx,W ;先输出W的16位二进制数
mov cx,16
L6:
mov dl,30h
rol bx,1
adc dl,0
mov ah,2
int 21h
loop L6
mov ah,4ch
int 21h
main endp
code ends
end main
以8进制输入,8进制输出
还没有解决有进位32位二进制以8进制输出
2024/4/24 22:08已解决
data segment
string1 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 stack
dw 100 dup (?)
top label word
stack ends
code segment
assume cs:code,ds:data,ss:stack
main proc far
mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax
lea sp,top
starting:
mov bx,0
mov si,8
mov cx,0
lea dx,string1
mov ah,9
int 21h
L1:
mov ah,7
int 21h
cmp al,0dh
je L2
cmp al,30h
jb L1
cmp al,37h
ja L1
mov dl,al
mov ah,2
int 21h
inc cx
and dx,7
mov ax,dx
xchg ax,bx
mul si
jc overflow
add bx,ax
jc overflow
cmp cx,6
je L2
jmp L1
L2:
cmp cx,0
je L1
mov X,bx
mov bx,0
mov cx,0
lea dx,string2
mov ah,9
int 21h
L3:
mov ah,7
int 21h
cmp al,0dh
je L4
cmp al,30h
jb L1
cmp al,37h
ja L1
mov dl,al
mov ah,2
int 21h
inc cx
and dx,7
mov ax,dx
xchg ax,bx
mul si
jc overflow
add bx,ax
jc overflow
cmp cx,6
je L4
jmp L3
L4:
cmp cx,0
je L3
mov Y,bx
mov ax,X
mov dx,0
add ax,Y
jc printc ;x+y超出了16位,跳转到有进位的输出
adc dx,0
mov W,ax
mov W+2,dx
lea dx,string3
mov ah,9
int 21h
;W+2为0,可以不输出(懒得删)
mov bx,W+2
mov dl,30h
rol bx,1
adc dl,0
mov ah,2
int 21h
mov cx,5
L5:
push cx
mov cl,3
rol bx,cl
mov dl,bl
and dl,7
add dl,30h
mov ah,2
int 21h
pop cx
loop L5
mov dl,' '
mov ah,2
int 21h
mov bx,W
mov dl,30h
rol bx,1
adc dl,0
mov ah,2
int 21h
mov cx,5
L6:
push cx
mov cl,3
rol bx,cl
mov dl,bl
and dl,7
add dl,30h
mov ah,2
int 21h
pop cx
loop L6
jcxz 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,0
mov W,ax
mov W+2,dx
lea dx,string3
mov ah,9
int 21h
mov ax,W
mov bx,W+2
rol bx,1
rol ax,1
adc bx,0
;16位二进制以6位8进制输出,先输出最高位
mov dl,30h
rol bx,1
adc dl,0
mov ah,2
int 21h
;再输出剩下的5位8进制
mov cx,5
L7:
push cx
mov cl,3
rol bx,cl
mov dl,bl
and dl,7
add dl,30h
mov ah,2
int 21h
pop cx
loop L7
mov dl,' '
mov ah,2
int 21h
;把最高位移走
mov bx,W
mov dl,30h
rol bx,1
;再输出剩下5位8进制
mov cx,5
L8:
push cx
mov cl,3
rol bx,cl
mov dl,bl
and dl,7
add dl,30h
mov ah,2
int 21h
pop cx
loop L8
jcxz exit
overflow:
lea dx,string4
mov ah,9
int 21h
jmp starting
exit:
mov ah,4ch
int 21h
main endp
code ends
end main
以10进制输入,10进制输出
data segment
string1 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 stack
dw 100 dup (?)
top label word
stack ends
code segment
assume cs:code,ss:stack,ds:data
main proc far
mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax
lea sp,top
starting:
mov bx,0
mov cx,0
mov si,10
;输入x
lea dx,string1
mov ah,9
int 21h
L1:
mov ah,7
int 21h
cmp al,0dh
je L2
cmp al,30h
jb L1
cmp al,39h
ja L1
mov dl,al
mov ah,2
int 21h
inc cx
and dx,0fh
mov ax,dx
xchg ax,bx
mul si
jc overflow
add bx,ax
jc overflow
jmp L1
L2:
cmp cx,0
je L1
mov X,bx
mov bx,0
mov cx,0
;输入y
lea dx,string2
mov ah,9
int 21h
L3:
mov ah,7
int 21h
cmp al,0dh
je L4
cmp al,30h
jb L3
cmp al,39h
ja L3
mov dl,al
mov ah,2
int 21h
inc cx
and dx,0fh
mov ax,dx
xchg ax,bx
mul si
jc overflow
add bx,ax
jc overflow
jmp L3
L4:
cmp cx,0
je L3
mov Y,bx
;计算X+Y
mov ax,X
mov dx,0
add ax,Y
adc dx,0
mov W,ax
mov W+2,dx
;以十进制输出X+Y
lea dx,string3
mov ah,9
int 21h
mov ax,W
mov dx,W+2
mov cx,0
L5:
div si
push dx
inc cx
cmp ax,0
je L6
mov dx,0
jmp L5
L6:
pop dx
add dl,30h
mov ah,2
int 21h
loop L6
jcxz exit
overflow:
lea dx,string4
mov ah,9
int 21h
jmp starting
exit:
mov ah,4ch
int 21h
main endp
code ends
end main
以16进制输入,16进制输出
data segment
string1 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 stack
dw 100 dup (?)
top label word
stack ends
code segment
assume cs:code,ss:stack,ds:data
main proc far
mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax
lea sp,top
;输入x
mov bx,0
mov cx,0
lea dx,string1
mov ah,9
int 21h
L1:
mov ah,7
int 21h
cmp al,0dh
je L5
cmp al,30h
jb L1
cmp al,39h
ja L2
mov dl,al
mov ah,2
int 21h
inc cx
sub dl,30h
jmp L4
L2:
cmp al,41h
jb L1
cmp al,46h
ja L3
mov dl,al
mov ah,2
int 21h
inc cx
sub dl,37h
jmp L4
L3:
cmp al,61h
jb L1
cmp al,66h
ja L1
mov dl,al
mov ah,2
int 21h
inc cx
sub dl,57h
L4:
and dx,0fh
push cx
mov cl,4
shl bx,cl
add bx,dx
pop cx
cmp cx,4
je L5
jmp L1
L5:
cmp cx,0
je L1
mov X,bx
;输入y
lea dx,string2
mov ah,9
int 21h
mov bx,0
mov cx,0
L6:
mov ah,7
int 21h
cmp al,0dh
je L10
cmp al,30h
jb L6
cmp al,39h
ja L7
mov dl,al
mov ah,2
int 21h
inc cx
sub dl,30h
jmp L9
L7:
cmp al,41h
jb L6
cmp al,46h
ja L8
mov dl,al
mov ah,2
int 21h
inc cx
sub dl,37h
jmp L9
L8:
cmp al,61h
jb L6
cmp al,66h
ja L6
mov dl,al
mov ah,2
int 21h
inc cx
sub dl,57h
L9:
and dx,0fh
push cx
mov cl,4
shl bx,cl
add bx,dx
pop cx
cmp cx,4
je L10
jmp L6
L10:
cmp cx,0
je L6
mov Y,bx
;计算x+y
mov ax,X
mov dx,0
add ax,Y
adc dx,0
mov W,ax
mov W+2,dx
;以16进制输出x+y
lea dx,string3
mov ah,9
int 21h
mov bx,W+2
cmp bx,0 ;如果W+2是0,不输出
je L11
mov dl,bl
add dl,30h
mov ah,2
int 21h
L11:
mov bx,W
mov cx,4
L12:
push cx
mov cl,4
rol bx,cl
mov dl,bl
and dl,0fh
cmp dl,9
ja L13
add dl,30h
jmp L14
L13:
add dl,37h
L14:
mov ah,2
int 21h
pop cx
loop L12
exit:
mov ah,4ch
int 21h
main endp
code ends
end main