;program name decimal adder
,
supports
254
bit result
;author 郭真林 0505115班16号
;date 4 : 52 PM 6 / 6 / 2007
showMeg Macro message ;dispaly tip for user
push dx
push ax
mov dx , offset message
mov ah , 09
int 21h
pop ax
pop dx
EndM
. model small
. stack
. data
intip db ' Please input two number: '
db ' for example: 120+300 ' , 0dh , 0ah
db ' enter q exit program ' , 0dh , 0ah , ' $ '
erorMeg db ' Sorry,Your input is invalid! ' , 0dh , 0ah , ' $ '
rtMeg db ' The result is:$ '
crlf db 0dh , 0ah , ' $ '
bitLeng equ 255 ;set bit length needed
buffer db bitLeng ; ' + ' need occupy a bit
db 0
db bitLeng dup( 0 )
aResult db bitLeng dup( 0 )
db 0dh , 0ah , ' $ '
. code
. startup
. REPEAT
showMeg intip
mov dx , seg buffer ;get user ' s expression string
mov ds,dx
mov dx,offset buffer
mov ah,0ah
int 21h
showMeg crlf ;CR_LF after user ' s input
mov si , offset buffer
mov di , offset aResult
xor bx , bx
call checkInput ;valid user ' s input
.IF bl == 0
showMeg erorMeg
.ELSEIF bl == 1
showMeg rtMeg
mov dx,di ;show the result to user
mov ah,09h
int 21h
.ENDIF
.UNTIL bl == 2
.exit 0
checkInput proc ;check user ' s input . in para : si , di; out para : bl 0 : f 1 : T 2 : exit , di : start address of result
local caFlag : byte , heighF : byte
mov byte ptr caFlag , 0
mov byte ptr heighF , 0
push dx
push cx
push ax
push si
mov bx , lengthof aResult ;recalculate the lenght of result
add di , bx ;locate the bottom of result
dec di
mov al , [si + 1 ] ;get the length of user ' s input string in practice
.IF al == 1
mov al,[si+2]
.IF al == ' q ' || al == ' Q '
jmp exitMP
.ENDIF
jmp invalid
.ELSEIF al == 0
jmp invalid
.ENDIF
mov cx,0
.WHILE cl < [si+1] ;get the poisition of ' + '
mov bx,cx
mov al,[bx+si+2]
.IF al == ' + '
.IF caFlag != 0
jmp invalid
.EndIF
mov caFlag, cl
inc caFlag
.ELSEIF !(al >= ' 0 ' && al <= ' 9 ' )
jmp invalid
.ELSE
and byte ptr[bx+si+2],0fh ;convert ASCII to number
.EndIF
inc cl
.ENDW
mov al,[si+1] ;the lenght of user ' s input
. IF (caFlag == 0 ) || (caFlag == 1 ) || (caFlag == al) ; ' + ' at the first or last poisition or not existing it
jmp invalid
. ENDIF
mov cl , byte ptr [si + 1 ] ; .....+... ;
sub cl , caFlag
mov dl , caFlag
dec dl
. IF cl > dl
mov dl , cl ;the max lenght of number
mov cl , caFlag ;the min lenght of number
dec cl
. EndIF
mov ch , 1
. WHILE ch <= dl
mov bx , 0
mov bl , caFlag
sub bl , ch
mov dh , caFlag ;the lenght of first number
dec dh
. IF ch <= cl || dh > cl
mov al , [bx + si + 1 ] ;get first char
. ELSE
mov al , 0
. ENDIF
mov bx , si
inc bx
mov bh , 0
mov bl , [bx]
sub bl , ch
inc bl
mov dh , [si + 1 ] ;the lenght of second number
sub dh , caFlag
. IF ch <= cl || dh > cl
mov ah , [bx + si + 1 ] ;get second char
. ELSE
mov ah , 0
. ENDIF
add al , heighF ;add Carry from low byte
mov byte ptr heighF , 0
daa
test al , 10h
jz noCarry1
mov heighF , 1
noCArry1 : add al , ah ;add two number
daa ;BCD adjusment
test al , 10h
jz noCarry2
mov heighF , 1
noCArry2 : or al , 30h ;convert to ASCII char
mov [di] , al ;save result
dec di
inc ch
. ENDW
inc di ;must incrase 1 , because sub more 1 above
. IF heighF != 0
dec di
mov byte ptr [di] , ' 1 '
. EndIF
mov al , [di]
. WHILE al == ' 0 ' ; delete 0 front of result
inc di
mov al , [di]
. ENDW
mov al , 0dh ; if result is 0 , then di should be sub 1 , and set 0
. IF [di] == al
dec di
mov byte ptr [di] , 30h
. EndIf
mov bl , 1
jmp overP
invalid : mov bl , 0
jmp overP
exitMP : mov bl , 2
overP : pop si
pop ax
pop cx
pop dx
ret
checkInput endp
end
;author 郭真林 0505115班16号
;date 4 : 52 PM 6 / 6 / 2007
showMeg Macro message ;dispaly tip for user
push dx
push ax
mov dx , offset message
mov ah , 09
int 21h
pop ax
pop dx
EndM
. model small
. stack
. data
intip db ' Please input two number: '
db ' for example: 120+300 ' , 0dh , 0ah
db ' enter q exit program ' , 0dh , 0ah , ' $ '
erorMeg db ' Sorry,Your input is invalid! ' , 0dh , 0ah , ' $ '
rtMeg db ' The result is:$ '
crlf db 0dh , 0ah , ' $ '
bitLeng equ 255 ;set bit length needed
buffer db bitLeng ; ' + ' need occupy a bit
db 0
db bitLeng dup( 0 )
aResult db bitLeng dup( 0 )
db 0dh , 0ah , ' $ '
. code
. startup
. REPEAT
showMeg intip
mov dx , seg buffer ;get user ' s expression string
mov ds,dx
mov dx,offset buffer
mov ah,0ah
int 21h
showMeg crlf ;CR_LF after user ' s input
mov si , offset buffer
mov di , offset aResult
xor bx , bx
call checkInput ;valid user ' s input
.IF bl == 0
showMeg erorMeg
.ELSEIF bl == 1
showMeg rtMeg
mov dx,di ;show the result to user
mov ah,09h
int 21h
.ENDIF
.UNTIL bl == 2
.exit 0
checkInput proc ;check user ' s input . in para : si , di; out para : bl 0 : f 1 : T 2 : exit , di : start address of result
local caFlag : byte , heighF : byte
mov byte ptr caFlag , 0
mov byte ptr heighF , 0
push dx
push cx
push ax
push si
mov bx , lengthof aResult ;recalculate the lenght of result
add di , bx ;locate the bottom of result
dec di
mov al , [si + 1 ] ;get the length of user ' s input string in practice
.IF al == 1
mov al,[si+2]
.IF al == ' q ' || al == ' Q '
jmp exitMP
.ENDIF
jmp invalid
.ELSEIF al == 0
jmp invalid
.ENDIF
mov cx,0
.WHILE cl < [si+1] ;get the poisition of ' + '
mov bx,cx
mov al,[bx+si+2]
.IF al == ' + '
.IF caFlag != 0
jmp invalid
.EndIF
mov caFlag, cl
inc caFlag
.ELSEIF !(al >= ' 0 ' && al <= ' 9 ' )
jmp invalid
.ELSE
and byte ptr[bx+si+2],0fh ;convert ASCII to number
.EndIF
inc cl
.ENDW
mov al,[si+1] ;the lenght of user ' s input
. IF (caFlag == 0 ) || (caFlag == 1 ) || (caFlag == al) ; ' + ' at the first or last poisition or not existing it
jmp invalid
. ENDIF
mov cl , byte ptr [si + 1 ] ; .....+... ;
sub cl , caFlag
mov dl , caFlag
dec dl
. IF cl > dl
mov dl , cl ;the max lenght of number
mov cl , caFlag ;the min lenght of number
dec cl
. EndIF
mov ch , 1
. WHILE ch <= dl
mov bx , 0
mov bl , caFlag
sub bl , ch
mov dh , caFlag ;the lenght of first number
dec dh
. IF ch <= cl || dh > cl
mov al , [bx + si + 1 ] ;get first char
. ELSE
mov al , 0
. ENDIF
mov bx , si
inc bx
mov bh , 0
mov bl , [bx]
sub bl , ch
inc bl
mov dh , [si + 1 ] ;the lenght of second number
sub dh , caFlag
. IF ch <= cl || dh > cl
mov ah , [bx + si + 1 ] ;get second char
. ELSE
mov ah , 0
. ENDIF
add al , heighF ;add Carry from low byte
mov byte ptr heighF , 0
daa
test al , 10h
jz noCarry1
mov heighF , 1
noCArry1 : add al , ah ;add two number
daa ;BCD adjusment
test al , 10h
jz noCarry2
mov heighF , 1
noCArry2 : or al , 30h ;convert to ASCII char
mov [di] , al ;save result
dec di
inc ch
. ENDW
inc di ;must incrase 1 , because sub more 1 above
. IF heighF != 0
dec di
mov byte ptr [di] , ' 1 '
. EndIF
mov al , [di]
. WHILE al == ' 0 ' ; delete 0 front of result
inc di
mov al , [di]
. ENDW
mov al , 0dh ; if result is 0 , then di should be sub 1 , and set 0
. IF [di] == al
dec di
mov byte ptr [di] , 30h
. EndIf
mov bl , 1
jmp overP
invalid : mov bl , 0
jmp overP
exitMP : mov bl , 2
overP : pop si
pop ax
pop cx
pop dx
ret
checkInput endp
end