(汇编语言)从键盘输入2个4位10进制数,求其和,将结果以10进制数的形式在屏幕输出。

  • BCD码实现
data segment
	a db 5
	   db ?
	   db 5 dup(0)
	b db 5,?,5 dup(0)
data ends
stack segment para stack
	db 10 dup(0)
stack ends
code segment
	assume cs:code,ds:data
start:	mov ax,data
	mov ds,ax
	lea dx,a
	mov ah,0ah
	int 21h
	lea dx,b
	mov ah,0ah
	int 21h
	mov si,5
	mov cx,5
	mov a[1],0
	mov b[1],0
	clc
lop:	mov al,a[si]
	adc al,b[si]
	aaa
	mov a[si],al
	dec si
	loop lop
	mov si,1
	mov cx,5
lop1:	add a[si],30h
	inc si
	loop lop1
	mov a[si],"$"
	mov si,0
	mov cx,5
lop2:	inc si
	cmp a[si],30h
	loopz lop2
	cmp cx,0
	jz over
next:	lea dx,a[si]
	mov ah,9
	int 21h
over:	mov ah,4ch
	int 21h
code ends
end start
  • 十六进制与ASCII码转换
data segment
	asc1 db 5
	        db ?
	        db 5 dup(0)
	asc2 db 5
	        db ?
	        db 5 dup(0)
	org 10h
	num1 dw 0
	num2 dw 0
	org 20h
	ans dw 0
	org 30h 
	output db 5 dup(0),"$"
	org 40h
	weight dw 10000,1000,100,10,1
	error db "input error!$"
	swp db 0dh,0ah,"$"
data ends
stack segment para stack
	dw 10 dup(0)
stack ends
code segment
	assume cs:code,ds:data
start:	mov ax,data
	mov ds,ax
	lea dx,asc1
	mov ah,0ah
	int 21h
	lea si,asc1+2
	call check 
	jc err_1
	lea dx,asc2
	mov ah,0ah
	int 21h
	lea si,asc2+2
	call check
	jc err_1
	mov si,offset asc1+2
	mov di,offset num1
	mov bx,offset weight+2
	mov cx,4
	call AscToNum
	mov si,offset asc2+2
	mov di,offset num2
	call AscToNum
	mov ax,num1
	add ax,num2
	mov ans,ax
	call NumToAsc
	lea dx,swp
	mov ah,9
	int 21h

	lea si,output
	mov cx,5
lop3:	cmp byte ptr [si],"0"
	jnz outp
	inc si
	loop lop3
	dec si
outp:	mov dx,si
	mov ah,9
	int 21h
	jmp over
err_1:	lea dx,error
	mov ah,9
	int 21h
over:	mov ah,4ch
	int 21h
check proc
	push si 
	push cx
	mov cx,4
lop0:	cmp byte ptr [si],"0"
	jb err
	cmp byte ptr [si],"9"
	ja err
	inc si
	loop lop0
	clc
	jmp return
err:	stc
return:	pop cx
	pop si
	ret
check endp
AscToNum proc
	push si
	push bx
	push cx
	push ax
	mov ax,0
lop1:	sub byte ptr [si],30h  
	mov al,[si]
	mul word ptr [bx]
	add word ptr[di],ax
	mov ax,0
	inc si
	inc bx
	inc bx
	loop lop1
	pop ax 
	pop cx
	pop bx
	pop si
	ret
AscToNum endp
NumToAsc proc
	push si
	push bx
	push ax
	push dx
	push cx
	mov cx,5
	lea bx,weight
	lea si,output
	mov dx,0
lop2:	mov ax,ans
	div word ptr [bx]
	add al,30h
	mov [si],al
	mov ah,0
	mov ans,dx
	mov dx,0
	inc bx
	inc bx
	inc si
	loop lop2
	pop cx
	pop dx
	pop ax
	pop bx
	pop si
	ret
NumToAsc endp
	
code ends
end start

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值