输入两个数,如果第一个数为偶数第二个数为奇数,互换位置;如果两个数都是奇数,则两个数都加一
程序运行:
代码:
datas segmentA dw 0 B dw 0a_string db 0ffh, 0 ,100h dup(?)b_string db 0ffh, 0 ,100h dup(?)inputA db 'input A=$'inputB db 0dh,0ah,'input B=$'error_number db 0dh,0ah,'error number$'outputA db 0dh,0ah,'A=$'outputB db 0dh,0ah,'B=$'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;输入a提示lea dx,inputAmov ah,9int 21h;输入alea dx,a_stringmov ah,10int 21h;a转成十进制数lea si,a_string+1call translate_to_numbermov A,ax;输入b提示lea dx,inputBmov ah,9int 21h;输入blea dx,b_stringmov ah,10int 21h;b转成十进制数lea si,b_string+1call translate_to_numbermov B,ax;将A、B移至ax,bxmov ax,Amov bx,Btest ax,1 ;判断ax是否为偶数jz even_number ;若为偶数,跳转test bx,1 ;判断bx是否为偶数jz next ;若为偶数,跳转inc ax ;否则,ax++,bx++ inc bx jmp next ;跳转even_number:test bx,1 ;判断bx是否为偶数jz next ;若为偶数,跳转xchg ax,bx ;将奇数移至axnext:mov A,ax ;保存ax,bxmov B,bx lea dx,outputA ;输出A、Bmov ah,9int 21hmov ax,Acall decimallea dx,outputBmov ah,9int 21hmov ax,Bcall decimalretmain 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 endpdecimal proc nearpush axpush cxpush dxpush bxcmp ax,0jge plusmov bx,axmov dl,'-'mov ah,2int 21hneg bxmov ax,bxplus:mov cx,0mov bx,10de:xor dx,dxdiv bxpush dxinc cxcmp ax,0jnz dede1:pop dxadd dl,30hmov ah,2 int 21h loop de1 pop bxpop dxpop cxpop axret decimal endpcodes endsend main