按下列要求編程:
(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
運行示例: