现在的位置: 首页 > 综合 > 正文

用汇编实现任意长度的两个整数相加

2013年08月12日 ⁄ 综合 ⁄ 共 2793字 ⁄ 字号 评论关闭
;两个整数是用非压缩型的BCD码存储的
;任意长度的两个数的加法
;输入的非数字字符将被忽略
dseg segment
 msg1  db "Please input the first number:",0dh,0ah,"$"
 msg2 db "Please input the second number:",0dh,0ah,"$"
 remsg db "The reslut is:$"
 msg3 db "Any ket to quit$"
 b1_len dw 0
 buf_1 db 100 dup(0)
 b2_len dw 0
 buf_2 db 100 dup(0)
dseg ends
;########################################################
cseg segment
 assume cs:cseg,ds:dseg
;******************************************
main proc far  ;start of the program
 mov  dx,dseg
 mov  ds,dx
 lea  dx,msg1
 mov  ah,09
 int  21h
 
 lea  bx,buf_1 ;input the first number
 call input
 
 call newline
 lea  dx,msg2
 mov  ah,09
 int  21h
 
 lea  bx,buf_2 ;input the second number
 call input
 call  adder
 
 call newline
 lea  dx,remsg
 mov  ah,09
 int  21h
 lea  bx,buf_1
 call show
 
 call newline
 lea  dx,msg3
 mov  ah,09
 int  21h
 call ktq  ;any Key To Quit
exit:
 mov  ah,4ch
 int  21h
 ret
main endp
;******************************
newline proc near ;回车换行
 push ds
 push dx
 push ax
 
 mov  dl,0ah
 mov  ah,02
 int  21h
 
 mov  dl,0dh
 mov  ah,02
 int  21h
 
 pop  ax
 pop  dx
 pop  ds
 ret
newline endp
;************************
input proc near  ;bx:数组首地址,第一个元素方长度
 push ds
 push ax
 push dx
 ;----------------------------------------
 xor  si,si  ;clear si to zero
 inc  si
next:
 cmp  si,100
 je  over  ;over
 mov  ah,01
 int  21h
 cmp  al," "
 je  over  ;input over
 cmp  al,0dh
 je  over
 cmp  al,"0"  ;忽略其他字符
 jb  next
 cmp  al,"9"
 ja  next
 sub  al,30h  ;to 十进制
      ;  非压缩型BCD码
 mov  [bx+si],al
 inc  si
 jmp  next 
over:
 dec  si
 mov  word ptr [bx-1],si ;save length
 ;--------------------------------------
 pop  dx
 pop  ax
 pop  ds
 ret
input endp
;******************
show proc near  ;bx:数组首地址
 push ds
 push ax
 push dx
 ;-----------
 mov  cx,[bx-1] ;length of the array
 xor  si,si
 inc  si
nextsh:
 mov  ah,02
 mov  dl,byte ptr [bx+si]
 add  dl,30h  ;to ASSII
 int  21h
 inc  si
 loop nextsh
 ;-----------
 pop  dx
 pop  ax
 pop  ds
 ret
show endp
;*******************
;未完成
adder proc near ;buf_1,buf_2分别是数组的首地址
                ;结果放到buf_1数组中
                ;进位CF
 push ds
 push ax
 push dx
 ;----------------------------------------------
 mov  si,word ptr [buf_1-1]  ;si存放bx的长度
 mov  cx,si
 mov  di,word ptr [buf_2-1]  ;di存放bp的长度
 xor  dl,dl                  ;clear dl
 cmp  cx,di
 mov  word ptr [buf_1-1],cx
 jae  continue               ;cx存放两个数组中较大一个的长度
 mov  cx,word ptr [buf_2-1]
 mov  word ptr [buf_1-1],cx  ;修改结果的长度
continue:
 mov  al,dl
 xor  dl,dl                  ;clear dl to zero
 cmp  si,0
 je  si_z
 add  al,[buf_1+si]
 dec  si
 jmp  L_DI
si_z:
 add  al,0
L_DI:
 cmp  di,0
 je  di_z
 add  al,byte ptr [buf_2+di]
 dec  di
 aaa
 adc  dl,0
 jmp  conti
di_z:
 add  al,0
 aaa
 adc  dl,0  ;^^^^
conti:
 mov  bx,cx
 mov  [buf_1+bx],al
 loop continue
 ;----------------------------------
 pop  dx
 pop  ax
 pop  ds
 ret
adder endp
;*******************
ktq proc near
 mov  ah,08
 int  21h
 ret
ktq endp
;********************
cseg ends
;###############################################################
 end  main

抱歉!评论已关闭.