按下列要求编程:
(1)输入两个小于100的十进制正整数。
(2)求出这两个数的所有公约数。
(3)求出这两个数的平方差,若是负的要输出负号。
(4)计算两个数各占和的百分比,并且按照“ %”的格式输出(小数点后保留两位)。
(5)数据的输入和结果的输出都要有必要的提示,且提示独占一行。
(6)要使用到子程序。
源代码:
data segment
hh db 0dh,0ah,'$'
m1 db 'please enter a number:',0dh,0ah,'$'
m2 db 'please enter another number:',0dh,0ah,'$'
m3 db 'gong yue shu:',0dh,0ah,'$'
m4 db 'ping fang cha:',0dh,0ah,'$'
m5 db 'num1/sum is:',0dh,0ah,'$'
m6 db 'num2/sum is:',0dh,0ah,'$'
sum dw 0
x dw 0
y dw 0
z dw 0
sign db 0h
s db 6 dup(0h),'$'
data ends
code segment
assume cs:code,ds:data
main proc far
start:
mov ax,data
mov ds,ax
mov ax,offset m1
call print
call enternumber
mov bx,ax
mov x,bx
mov sum,bx
mov ax,offset m2
call print
call enternumber
mov cx,ax
mov y,cx
add sum,cx
call find
mov ax,offset m3
call print
mov ax,cx
mov bl,al
mov bh,1
l1:
mov al,bl
xor ah,ah
div bh
cmp ah,0
jnz next2
mov al,bh
aam
mov cx,ax
add ch,30h
mov dl,ch
mov ah,2
int 21h
add cl,30h
mov dl,cl
mov ah,2
int 21h
mov dx,offset hh
mov ah,09h
int 21h
next2:
inc bh
cmp bl,bh
jl done
jmp l1
done:
mov ax,offset m4
call print
call pfc
mov ax,offset hh
call print
mov ax,offset m5
call print
mov al,byte ptr x
mov bh,100
mul bh
mov bl,byte ptr sum
div bl
mov bl,al
call disp10
mov dl,'%'
mov ah,02h
int 21h
mov ax,offset hh
call print
mov ax,offset m6
call print
mov al,byte ptr y
mov bh,100
mul bh
mov bl,byte ptr sum
div bl
mov bl,al
call disp10
mov dl,'%'
mov ah,02h
int 21h
mov ah,4ch
int 21h
main endp
print proc near
mov dx,ax
mov ah,09h
int 21h
ret
print endp
enternumber proc near
mov ah,01h
int 21h
mov dh,al
sub dh,30h
mov ah,01h
int 21h
cmp al,0dh
jz next1
sub al,30h
shl dh,1
mov dl,dh
shl dh,1
shl dh,1
add dh,dl
add dh,al
next1:
mov cl,dh
mov dx,offset hh
mov ah,09h
int 21h
mov al,cl
xor ah,ah
ret
enternumber endp
find proc near
cmp bx,cx
jnl first
xchg bx,cx
first:
xor dx,dx
mov ax,bx
div cx
cmp dx,0
jz equal
mov bx,cx
mov cx,dx
jmp first
equal:
ret
find endp
pfc proc near; 计算平方差
work:
mov al,byte ptr x
mul al
mov z,ax
mov al,byte ptr y
mul al
cmp z,ax
jb n1
sub z,ax
jmp n2
n1:
sub ax,z
mov z,ax
mov sign,'-'
n2:
mov cx,5h
mov di,4h
mov bx,0ah
mov ax,z
w2:
mov dx,0
div bx
add dl,30h
mov s[di],dl
dec di
cmp ax,0h
jz w2exit
loop w2
w2exit:
mov al,sign
mov s[di],al
mov di,0
w3:
inc di
cmp s[di],0h
jz w3
lea dx,s[di]
mov ah,09h
int 21h
ret
pfc endp
disp10 proc near ;计算占和百分比
mov ah,0
mov al,bl
mov bh,100
div bh
mov bl,ah
mov dl,al
cmp dl,0
jz jump1
add dl,30h
mov ah,02
int 21h
jump1:
mov al,bl
mov ah,0
mov bh,10
div bh
mov bl,ah
mov dl,al
cmp dl,0
jz jump2
add dl,30h
mov ah,02
int 21h
jump2:
mov dl,bl
add dl,30h
mov ah,02h
int 21h
ret
disp10 endp
code ends
end start