用彙編實現任意長度的兩個整數相加

來源:互聯網
上載者:User
;兩個整數是用非壓縮型的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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.