输入两个数,使用辗转相除法求最大公约数
程序运行:
代码:
datas segmentM_max_len db 0ffhM_len db 0M_string db 100 dup(?)M dw 0 N_max_len db 0ffhN_len db 0N_string db 100 dup(?)N dw 0inputM db 'input M=$'inputN db 0dh,0ah,'input N=$'output db 0dh,0ah,'R=$'error_number db 0dh,0ah,'error number$'
datas endsstacks segment stackdb 100h dup(?)stacks endscodes segmentassume cs:codes,ds:datas,ss:stacks
main proc far
start:push dsmov ax,0hpush axmov ax,datas ;初始化dsmov ds,ax;输入M提示lea dx,inputMmov ah,9int 21h;输入Mlea dx,M_max_lenmov ah,10int 21h;M转成十进制数lea si,M_lencall translate_to_numbermov M,ax;输入N提示lea dx,inputNmov ah,9int 21h;输入Nlea dx,N_max_lenmov ah,10int 21h;N转成十进制数lea si,N_lencall translate_to_numbermov N,ax ;辗转相除法mov ax,Mmov bx,Ncmp ax,0jle no_pluscmp bx,0 jle no_pluss:xor dx,dx div bx cmp dx,0 jz breakmov ax, bxmov bx,dxjmp s;输出R结果break: lea dx,outputmov ah,9int 21hmov ax,bx call decimaljmp exit;错误数据no_plus:lea dx,error_numbermov ah,9int 21hexit:retmain endp;字符串转换为十进制数
translate_to_number proc near ;si:lenght firstpush cx push dx push bx push si push di mov di,10mov ax,0mov cl,[si]mov ch,0 cmp cx,0 jz errinc sitran:mov bl,[si]inc sicmp bl,'0'jb errcmp bl,'9'ja err sub bl,30hxor bh,bh mul di add ax,bx loop tranjmp exiterr:lea dx,error_numbermov ah,9int 21h mov ax,4c00hint 21hexit:pop di pop si pop bx pop dxpop cx ret
translate_to_number endp;输出十进制数
decimal proc near push axpush cxpush dxpush bxmov bx,ax cmp ax,0jge plusmov dl,'-'mov ah,2int 21hneg bx mov ax,bx plus:mov cx,0mov bx,10 de:xor dx,dx div bx push dx inc cx cmp ax,0hjnz de de1:pop dx add dl,30hmov ah,2 int 21hloop de1 pop bx pop dx pop cx pop ax retdecimal endpcodes endsend main