汇编语言(十七)之判断三个数是否相等

汇编语言(十七)之判断三个数是否相等

输入三个数,计算相等数的个数

程序运行:

www.zeeklog.com  - 汇编语言(十七)之判断三个数是否相等

代码:


datas segment
       
       a      db  0ffh, 0 ,100 dup(?)
	   b      db  0ffh, 0 ,100 dup(?)
       d      db  0ffh, 0 ,100 dup(?)	
	   
       ARRAY  dw  1,2,2
	   
	   
	 inputA          db 'input a=$'
	 inputB          db 0dh,0ah,'input b=$'
	 inputD          db 0dh,0ah,'input d=$'
     output          db 0dh,0ah,'result=$'
     error_number    db 0dh,0ah,'error number$'

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
	   mov ah,10
	   int 21h
	   ;a转成十进制数
	   lea si,a+1
	   call translate_to_number
	   mov ARRAY[0],ax
	   
	   ;输入b提示
	   lea dx,inputB
	   mov ah,9
	   int 21h
	   ;输入b
	   lea dx,b
	   mov ah,10
	   int 21h
	   ;b转成十进制数
	   lea si,b+1
	   call translate_to_number
	   mov ARRAY[2],ax
	   
	   ;输入d提示
	   lea dx,inputD
	   mov ah,9
	   int 21h
	   ;输入d
	   lea dx,d
	   mov ah,10
	   int 21h
	   ;a转成十进制数
	   lea si,d+1
	   call translate_to_number
	   mov ARRAY[4],ax
	   
	   
	   lea dx,output
	   mov ah,9
	   int 21h
	   
       mov ax,ARRAY[0]
	   mov bx,ARRAY[2]
	   cmp ax,bx                 ;判断ARRAY[0]与ARRAY[2]是否相等
	   jnz able_two              ;若不相等,跳转
	   cmp ax,ARRAY[4]           ;判断ARRAY[0]与ARRAY[4]是否相等  
	   jnz two                    ;若不等,跳转
       mov dl,'2'                ;输出三个数相等的结果
	   mov ah,2
	   int 21h
	   jmp exit
	   
	able_two:
	   cmp ax,ARRAY[4]           ;判断ARRAY[0]与ARRAY[4]是否相等  
	   jz  two                   ;若相等,跳转
	   cmp bx,ARRAY[4]           ;若不等,比较ARRAY[2]与ARRAY[4]
	   jz two                    ;若相等,跳转
	   mov dl,'0'                ;若不等,输出三个数不等的结果
	   mov ah,2
	   int 21h
	   jmp exit
       
	 two:
	   mov dl,'1'               ;输出两个数相等的结果
	   mov ah,2
	   int 21h
	 
  exit:	 
       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
	
	mov bx,ax 
	cmp ax,0
	jge plus
	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,0h
	  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