Use assembly to add two integers of any length

Source: Internet
Author: User
The two integers are stored with non-compressed BCD codes.
; Addition of two numbers of any length; input non-numeric characters will be ignored
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 $"
Bw.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 21 h
 
Lea BX, buf_1; input the first number
Call Input
 
Call newline
Lea dx, msg2
MoV ah, 09
Int 21 h
 
Lea BX, buf_2; input the second number
Call input call Adder
 
Call newline
Lea dx, remsg
MoV ah, 09
Int 21 h
Lea BX, buf_1
Call show
 
Call newline
Lea dx, msg3
MoV ah, 09
Int 21 h
Call ktq; any key to quit
Exit:
MoV ah, 4ch
Int 21 h
RET
Main endp
;******************************
Newline proc near; press enter to wrap
PUSH DS
Push DX
PUSH AX
 
MoV DL, 0ah
MoV ah, 02
Int 21 h
 
MoV DL, 0dh
MoV ah, 02
Int 21 h
 
Pop ax
Pop DX
Pop DS
RET
Newline endp
;************************
Input proc near; Bx: array first address, length of the first element
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 21 h
CMP Al ,""
Je over; input over
CMP Al, 0dh
Je over
CMP Al, "0"; ignore other characters
JB next
CMP Al, "9"
Ja next
Sub Al, 30 h; to decimal
; Non-compressed BCD code
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: the first address of the array.
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, 30 h; To assii
Int 21 h
INC Si
Loop nextsh
;-----------
Pop DX
Pop ax
Pop DS
RET
Show endp
;*******************
; Unfinished
Adder proc near; buf_1 and buf_2 are the first addresses of arrays respectively.
The result is put into the buf_1 array.
; Carry cf
PUSH DS
PUSH AX
Push DX
;----------------------------------------------
MoV Si, word PTR [buf_1-1]; Si storage BX Length
MoV CX, Si
MoV Di, word PTR [buf_2-1]; Di stores the length of BP
Xor dl, DL; clear DL
Cmp cx, Di
MoV word PTR [buf_1-1], CX
Jae continue; CX stores the larger length of the two Arrays
MoV CX, word PTR [buf_2-1]
MoV word PTR [buf_1-1], CX; modify the length of the result
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
Rochelle 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 21 h
RET
Ktq endp
;********************
Cseg ends
; ######################################## #######################
End main

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.