汇编语言(十六)之三数值求和
输入A、B、C三个数,如果存在一个数为0,则全部清零,否则求和输出
程序运行:
代码:
datas segment
A dw 1
B dw 0
D dw 3
S dw 0
a_string db 0ffh, 0 ,100 dup(?)
b_string db 0ffh, 0 ,100 dup(?)
d_string db 0ffh, 0 ,100 dup(?)
inputA db 'input A=$'
inputB db 0dh,0ah,'input B=$'
inputD db 0dh,0ah,'input D=$'
error_number db 0dh,0ah,'error number$'
outputA db 0dh,0ah,'A=$'
outputB db 0dh,0ah,'B=$'
outputD db 0dh,0ah,'D=$'
outputS db 0dh,0ah,'S=$'
datas ends
stacks segment stack
db 100h dup(?)
stacks ends
codes segment
assume cs:codes,ds:datas,ss:stacks
main proc far
start:
push ds
mov ax,0h
push ax
mov ax,datas ;初始化ds
mov ds,ax
;输入a提示
lea dx,inputA
mov ah,9
int 21h
;输入a
lea dx,a_string
mov ah,10
int 21h
;a转成十进制数
lea si,a_string+1
call translate_to_number
mov A,ax
;输入b提示
lea dx,inputB
mov ah,9
int 21h
;输入b
lea dx,b_string
mov ah,10
int 21h
;b转成十进制数
lea si,b_string+1
call translate_to_number
mov B,ax
;输入d提示
lea dx,inputD
mov ah,9
int 21h
;输入d
lea dx,d_string
mov ah,10
int 21h
;d转成十进制数
lea si,d_string+1
call translate_to_number
mov D,ax
mov ax,A
cmp ax,0 ;判断A是否为0
jz clear ;若为0,则清零
mov bx,B
cmp bx,0 ;判断B是否为0
jz clear ;若为0,则清零
mov dx,D
cmp dx,0 ;判断D是否为0
jz clear ;若为0,则清零
add ax,bx ;求和
add ax,dx
mov S,ax ;把求和保存在S
jmp exit
clear:
mov ax,0 ;清零
mov A,ax
mov B,ax
mov D,ax
exit:
lea dx,outputA ;输出A、B、D、S
mov ah,9
int 21h
mov ax,A
call decimal
lea dx,outputB
mov ah,9
int 21h
mov ax,B
call decimal
lea dx,outputD
mov ah,9
int 21h
mov ax,D
call decimal
lea dx,outputS
mov ah,9
int 21h
mov ax,S
call decimal
ret
main endp
;字符串转换为十进制数
translate_to_number proc near
;si:lenght first
push cx
push dx
push bx
push si
push di
mov di,10
mov ax,0
mov cl,[si]
mov ch,0
cmp cx,0
jz err
inc si
tran:
mov bl,[si]
inc si
cmp bl,'0'
jb err
cmp bl,'9'
ja err
sub bl,30h
xor bh,bh
mul di
add ax,bx
loop tran
jmp exit
err:
lea dx,error_number
mov ah,9
int 21h
mov ax,4c00h
int 21h
exit:
pop di
pop si
pop bx
pop dx
pop cx
ret
translate_to_number endp
decimal proc near
push ax
push cx
push dx
push bx
cmp ax,0
jge plus
mov bx,ax
mov dl,'-'
mov ah,2
int 21h
neg bx
mov ax,bx
plus:
mov cx,0
mov bx,10
de:
xor dx,dx
div bx
push dx
inc cx
cmp ax,0
jnz de
de1:
pop dx
add dl,30h
mov ah,2
int 21h
loop de1
pop bx
pop dx
pop cx
pop ax
ret
decimal endp
codes ends
end main